Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit code to set build unstable - Jenkins DSL Scripting

I am trying to utilize Exit code to set build unstable in job -> publishers -> postBuildScripts -> steps -> shell -> advance option to set my build unstable based on a condition. I have the script below.

...
postBuildScripts {
                onlyIfBuildSucceeds(false)
                steps {
                  shell('echo "Before exit 1"\n' +
                        'if [ ! condition ]; then\n' +
                        'echo failed-condition\n' +
                        'exit 1\n' +
                        'fi'
                       )
                }
            }
...

On executing the above DSL script, I get as below in jenkins enter image description here

With the above script exit 1, the build fails. But I wanted to make it unstable and I DO NOT want to use markBuildUnstable(true). I wanted to mark build unstable based on certain exit codes only. I can do it by setting the exit code manually to 1 like below enter image description here After this, the build is marked unstable.

I'm looking for script to set this field through scripts and not manually as I have many jobs.

Can someone please help me on this with suggestions?

like image 761
Siddarth Avatar asked Sep 12 '18 06:09

Siddarth


2 Answers

I was able to get this to work using the "raw" configure interface. When I was trying it, if I had a steps { shell () } anywhere else, it would overwrite and lose the settings, so I had to specify the command option as well. I was under the impression that << would append and not overwrite, but I've never used Node before.

def final my_script = readFileFromWorkspace('my_script.sh') // Seed workspace
freeStyleJob("jobname") {
  ...
    configure { project ->
        project / builders << 'hudson.tasks.Shell' {
          command my_script
          unstableReturn 2
        }
    }
  ...
}
like image 182
Aaron D. Marasco Avatar answered Oct 04 '22 04:10

Aaron D. Marasco


You can also use the Dynamic DSL:

job('example') {
  steps {
    shell {
      command('echo TEST')
      unstableReturn(2)
    }
  }
} 
like image 32
daspilker Avatar answered Oct 04 '22 04:10

daspilker