能够抑制运行在剧本之后的詹金斯管道中的BadExitStatusException错误

时间:2019-04-09 09:08:50

标签: jenkins ansible

我有一个Jenkins管道,可以对几台服务器运行ansible剧本

node {
stage('Get Playbook') {
    dir('ansible-dir') {
       git branch: 'master',
       credentialsId: 'creds',
       url: 'ssh://git@gitserver.com'
    }
}

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ansible', usernameVariable: 'username', passwordVariable: 'password']]){

    def remote = [:]
    remote.name = 'Remote Ansible Server'
    remote.host = 'server.domain.com'
    remote.user = username
    remote.password = password
    remote.allowAnyHosts = true

    stage('Copy dir to remote server'){
        sshPut remote: remote, from: '/localdir', into: '/home/remote/jenkins'
    }

stage('Run ansible playbook'){
        sshCommand remote: remote, command: 'ANSIBLE_CONFIG=/home/remote/jenkins/ansible.cfg ansible-playbook /home/remote/jenkins/playbook.yml -vvv'
    }

    stage('Copy report to local machine') {
        sshGet remote: remote, from: '/home/remote/jenkins/reports', into: '/home/user/reports', override: true
    }
}

我遇到的问题是“将报告复制到本地计算机”阶段并不总是运行。有时,服务器在播放摘要中会失败,这本身并不是问题,因为在剧本中生成的报告说明了这一点,并具有日志记录来标识它。

问题在于,当服务器在播放回顾中确实失败时,詹金斯会给出以下错误。

org.hidetake.groovy.ssh.session.BadExitStatusException: Command returned exit status 2: ANSIBLE_CONFIG=/home/remote/jenkins/ansible.cfg ansible-playbook /home/remote/jenkins/playbook.yml -vvv

这可防止管道中的下一个阶段运行。有没有办法抑制此错误输出,以便管道继续运行?

1 个答案:

答案 0 :(得分:0)

通过将运行剧本的阶段放在尝试中来解决这个问题

try {
    stage('Copy dir to remote server'){
    sshPut remote: remote, from: '/localdir', into: '/home/remote/jenkins'
} catch(e) {
    build_ok = false
    echo e.toString()
}

这使它可以继续进行到下一个阶段。