Continuous Integration pipeline - jenkins

I am looking to trigger the on_failure step in my pipeline. I have a very simple script.
2 resources and 1 job. The job has a run step in which I would like to trigger failure manually. I have tried many things and they all leaded to an error.
Is there an shell script exit code that could make the task to fail and not being errored

Triggering post → failure and not failing the build is not possible:
failure
Only run the steps in post if the current Pipeline’s or stage’s run has a "failed" status, typically denoted by red in the web UI.
However, you can do the following:
def status
pipeline {
agent any
stages {
stage('Failing stage') {
steps {
script {
status = sh script: 'exit 99', returnStatus: true
}
}
}
}
post {
always {
script {
if ( status == 99 )
echo 'Script failed...'
else
echo 'Script succeeded...'
}
}
}
}

Example post -> failure
post {
always {
cleanWs()
}
success {
sendEmail('SUCCESSFUL')
}
unstable {
sendEmail('UNSTABLE')
}
failure {
sendEmail('FAILED')
}
}

Related

Conditional post section in Jenkins pipeline

Say I have a simple Jenkins pipeline file as below:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
send_slack_message("Built tag")
}
}
}
post {
failure {
send_slack_message("Error building tag")
}
}
}
Since there's a lot non-tag builds everyday, I don't want to send any slack message about non-tag builds. But for the tag builds, I want to send either a success message or a failure message, despite of which stage it failed.
So for the above example, I want:
When it's a tag build, and stage 'Test' failed, I shall see a "Error building tag" message. (This is a yes in the example)
When it's a tag build, and all stages succeeded, I shall see a "Built tag" message. (This is also a yes in the example)
When it's not a tag build, no slack message will ever been sent. (This is not the case in the example, for example, when the 'Test' stage fails, there's will be a "Error building tag" message)
As far as I know, there's no such thing as "conditional post section" in Jenkins pipeline syntax, which could really help me out here. So my question is, is there any other way I can do this?
post {
failure {
script {
if (isTagBuild) {
send_slack_message("Error building tag")
}
}
}
}
where isTagBuild is whatever way you have to differentiate between a tag or no tag build.
You could also apply the same logic, and move send_slack_message("Built tag") down to a success post stage.
In the postbuild step you can also use script step inside and use if. And inside this if step you can add emailext plugin.
Well, for those who just want some copy-pastable code, here's what I ended-up with based on #eez0's answer.
pipeline {
agent any
environment {
BUILDING_TAG = 'no'
}
stages {
stage('Setup') {
when {
buildingTag()
}
steps {
script {
BUILDING_TAG = 'yes'
}
}
}
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
}
}
}
post {
failure {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#dc3545', message: "Error publishing")
}
}
}
success {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#28a745', message: "Published")
}
}
}
}
}
As you can see, I'm really relying on Jenkins built-in buidingTag() function to help me sort things out, by using an env-var as a "bridge". I'm really not good at Jenkins pipeline, so please leave comments if you have any suggestions.

Declarative pipeline to check the build step = Failure then trigger next build step, but not fail the job.

I am trying to fail a build step in Jenkinsfile with failed results = failure. Once the step is failed it triggers my rollback job. Tried many different things, but had no luck. Any help would be greatly appreciated.
pipeline {
agent any
stages {
stage('Git Checkout') {
steps {
script {
git 'somegit-repo'
sh'''
mvn package
'''
echo currentBuild.result
catchError {
build 'rollback'
}
}
}
}
}
One way is to use a shell script and with exit 1 statement
e.g.
sh "exit 1"
Or you can use error step
error('Failing build because...')
See https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal
Use a try catch block
node {
stage("Run scripts") {
try {
<some command/script>
} catch (error) {
<rollback command/script>
}
}
}
Thank you so much. This seems to work!
stages {
stage("some test") {
steps{
script {
git 'mygitrepo.git'
try {
sh''' mvn test '''
} catch (error) {
script {
def job = build job: 'rollback-job'
}
}
}
}
}
If you check the cleaning and notifications page
You can do a post step and get rid of all the try/catch stuff and get a cleaner Jenkinsfile
pipeline {
agent any
stages {
stage('No-op') {
steps {
sh 'ls'
}
}
}
post {
always {
echo 'One way or another, I have finished'
deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}

How to catch manual UI cancel of job in Jenkinsfile

I've tried to find documentation about how in a Jenkinsfile pipeline catching the error that occurs when a user cancels a job in jenkins web UI.
I haven't got the postor try/catch/finally approaches to work, they only work when something fails within the build.
This causes resources not to be free'd up when someone cancels a job.
What I have today, is a script within a declarative pipeline, like so:
pipeline {
stage("test") {
steps {
parallell (
unit: {
node("main-builder") {
script {
try { sh "<build stuff>" } catch (ex) { report } finally { cleanup }
}
}
}
)
}
}
}
So, everything within catch(ex) and finally blocks is ignored when a job is manually cancelled from the UI.
Non-declarative approach:
When you abort pipeline script build, exception of type org.jenkinsci.plugins.workflow.steps.FlowInterruptedException is thrown. Release resources in catch block and re-throw the exception.
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
def releaseResources() {
echo "Releasing resources"
sleep 10
}
node {
try {
echo "Doing steps..."
} catch (FlowInterruptedException interruptEx) {
releaseResources()
throw interruptEx
}
}
Declarative approach (UPDATED 11/2019):
According to Jenkins Declarative Pipeline docs, under post section:
cleanup
Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.
So that should be good place to free resources, no matter whether the pipeline was aborted or not.
def releaseResources() {
echo "Releasing resources"
sleep 10
}
pipeline {
agent none
stages {
stage("test") {
steps {
parallel (
unit: {
node("main-builder") {
script {
echo "Doing steps..."
sleep 20
}
}
}
)
}
post {
cleanup {
releaseResources()
}
}
}
}
}
You can add a post trigger "cleanup" to the stage:
post {
cleanup {
script { ... }
sh "remove lock"
}
}

Post failure block in Jenkinsfile is not working

I'm trying a post failure action with a parallel step but it never works.
This is my Jenkinsfile:
pipeline {
agent any
stages {
stage("test") {
steps {
withMaven(
maven: 'maven3', // Maven installation declared in the Jenkins "Global Tool Configuration"
mavenSettingsConfig: 'maven_id', // Maven settings.xml file defined with the Jenkins Config File Provider Plugin
mavenLocalRepo: '.repository')
{
// Run the maven build
sh "mvn --batch-mode release:prepare -Dmaven.deploy.skip=true" --> it will always fail
}
}
}
stage("testing") {
steps {
parallel (
phase1: { sh 'echo phase1' },
phase2: { sh "echo phase2" }
)
}
}
}
post {
failure {
echo "FAIL"
}
}
}
But the post failure action here is a bit useles... I don´t see it any place.
Thanks to all!
Regards
I've found the issue, after several hours of searching. What you are missing (and I was missing too) is the catchError section.
pipeline {
agent any
stages {
stage('Compile') {
steps {
catchError {
sh './gradlew compileJava --stacktrace'
}
}
post {
success {
echo 'Compile stage successful'
}
failure {
echo 'Compile stage failed'
}
}
}
/* ... other stages ... */
}
post {
success {
echo 'whole pipeline successful'
}
failure {
echo 'pipeline failed, at least one step failed'
}
}
You should wrap every step that can potentially fail into a catchError function. What this does is:
If an error occurs...
... set build.result to FAILURE...
... and continue the build
The last point is important: your post{ } blocks did not get called because your entire pipeline was aborted before they even had a chance to execute.
Just in case someone else also made the same stupid mistake I did, don't forget the post block needs to be inside the pipeline block.
i.e. This is apparently valid, but (obviously) won't work:
pipeline {
agent { ... }
stages { ... }
}
// WRONG!
post {
always { ... }
}
This is what's correct:
pipeline {
agent { ... }
stages { ... }
post {
always { ... }
}
}

Abort a build immediately with success status in Jenkins pipeline

Is there any pipeline step available to abort a build on certain cases with success status?
There is an error step available to abort a build with failure status. But I don't know about the success status.
As was said in other replies, there isn't a step to abort in this way. As Christopher suggested you can use try-catch around the aborting code and use error(). I think you will need to track the abort status of your build - you could define an abort method globally in the pipeline to set this status and raise an error so it will abort other steps in your stage.
If you used the declarative pipeline you can use a 'when' declaration with an expression in later stages so they don't execute when the abort status is set.
I am interested in this problem myself so I worked out an example of a pipeline that does this here:
/**
* Tracking if the build was aborted
*/
Boolean buildAborted = false
/**
* Abort the build with a message
*/
def abortBuild = { String abortMessage ->
buildAborted = true
error(abortMessage)
}
pipeline {
agent any
parameters {
string(name: 'FailOrAbort', defaultValue: 'ok', description: "Enter 'fail','abort' or 'ok'")
}
stages {
stage('One') {
steps {
echo "FailOrAbort = ${params.FailOrAbort}"
script {
try {
echo 'Doing stage 1'
if(params.FailOrAbort == 'fail') {
echo "This build will fail"
error("Build has failed")
}
else if(params.FailOrAbort == 'abort') {
echo "This build will abort with SUCCESS status"
abortBuild("This build was aborted")
}
else {
echo "This build is a success"
}
echo "Stage one steps..."
}
catch(e) {
echo "Error in Stage 1: ${e.getMessage()}"
if(buildAborted) {
echo "It was aborted, ignoring error status"
}
else {
error(e.getMessage())
}
}
}
}
post {
failure {
echo "Stage 1 failed"
}
}
}
stage('Two') {
when {
expression {
return !buildAborted
}
}
steps {
echo "Doing stage 2"
}
}
stage('Three') {
when {
expression {
return !buildAborted
}
}
steps {
echo "Doing stage 3"
}
}
}
post {
always {
echo "Build completed. currentBuild.result = ${currentBuild.result}"
}
failure {
echo "Build failed"
}
success {
script {
if(buildAborted) {
echo "Build was aborted"
} else {
echo 'Build was a complete success'
}
}
}
unstable {
echo 'Build has gone unstable'
}
}
}
As a side note there is a property 'currentBuild.result' you can adjust in the pipeline but once set to 'FAILURE' it cannot be cleared back to 'SUCCESS' - the Jenkins model doesn't allow it AFAIK.
No, the normal process of a pipeline is to go from start to end.
What you could however do is to test for your success status and just not call the rest of your code, in an if or something like that. Functions could help you achieve that quite easily, e.g. :
node() {
// Part 1
def isSuccess = part1();
if(!isSuccess) {
part2()
}
}
// Part 2
def function part2() {
// Part 2 code
}
However, you should be careful with that kind of things, maybe it highlights the fact that your pipeline is not properly designed. If that is not what you want, please provide more details, like a use case.
First of all, I'm not aware of such a step.
But you could use the error step to abort the build with a certain message, if it should succeed. Catch this error in a try{}catch(){} block, check for the message and set the build status to success.

Resources