How to catch manual UI cancel of job in Jenkinsfile

妖精的绣舞 提交于 2019-12-03 12:45:45
Travenin

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..."
        sleep 20
    } catch (FlowInterruptedException interruptEx) {
        releaseResources()
        throw interruptEx
    }
}

Declarative approach:

The same, but within a script {} block in the steps of the stage. Not the neatest solution but the one that I've tested and got working.

You can add a post trigger "cleanup" to the stage:

post {
    cleanup {
        script { ... }
        sh "remove lock"
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!