简体   繁体   中英

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 在此处输入图片说明

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 在此处输入图片说明 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?

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
        }
    }
  ...
}

You can also use the Dynamic DSL :

job('example') {
  steps {
    shell {
      command('echo TEST')
      unstableReturn(2)
    }
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM