How to perform actions for failed builds in Jenkinsfile

后端 未结 3 1561
心在旅途
心在旅途 2021-01-01 18:21

Is there a way to perform cleanup (or rollback) if the build in Jenkinsfile failed?

I would like to inform our Atlassian Stash instance that the build failed (by doi

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 18:58

    I manage to solve it by using try:finally. In case of this stage raises an error the stage will be red and finally run the code but if the stage is okay, the stage will be green and finally will run too.

    stage('Tests'){
        script{
            try{
                sh """#!/bin/bash -ex
                    docker stop \$(docker ps -a -q)
                    docker rm \$(docker ps -a -q)
                    export DOCKER_TAG=${DOCKER_TAG}
                    docker-compose -p ${VISUAL_TESTING_PROJECT_TAG} build test
                    docker-compose -p ${VISUAL_TESTING_PROJECT_TAG} up --abort-on-container-exit --exit-code-from test
            """     
            }
            finally{
                sh """#!/bin/bash -ex
                export DOCKER_TAG=${DOCKER_TAG}
                docker-compose -p ${VISUAL_TESTING_PROJECT_TAG} down
                """
            }
        }
    
    }
    

提交回复
热议问题