使Jenkins在发生故障时不要跳过阶段

unguejic  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(261)

我有一个Jenkins管道文件,它有3个阶段:

stage('run SQL') {
    steps { 
        script {
                echo "Now run SQL on hosts"
                ansiblePlaybook become: true,
                colorized: true,
                credentialsId: 'me',
                installation: 'ansible',
                inventory: 'google/ansible/ansible-root/hosts',
                vaultCredentialsId: 'AnsibleVaultPassword',
                playbook: "ansible//playbooks/run-query.yml"
        }       
    }       
}       

stage('task2') {
    steps { 
        script {
                echo "Now run task2"
                ansiblePlaybook become: true,
                colorized: true,
                credentialsId: 'me',
                installation: 'ansible',
                inventory: 'google/ansible/ansible-root/hosts',
                vaultCredentialsId: 'AnsibleVaultPassword',
                playbook: "ansible//playbooks/task-2.yml"
        }       
    }       
}

stage('task3') {
    steps { 
        script {
                echo "Now run task3"
                ansiblePlaybook become: true,
                colorized: true,
                credentialsId: 'me',
                installation: 'ansible',
                inventory: 'google/ansible/ansible-root/hosts',
                vaultCredentialsId: 'AnsibleVaultPassword',
                playbook: "ansible//playbooks//task-3.yml"
        }       
    }       
}

Ansible剧本run-query.ymlgoogle/ansible/ansible-root/hosts文件中定义的主机列表上运行,其中一些主机可能会关闭。当我运行Jenkinsfile时,我得到如下输出:

14:50:11  [0;32ok: [app-server1]
14:50:15  [1;31fatal: [app-server2]: UNREACHABLE! => {"changed": false, "msg": "Data could not be sent to remote host \"app-server2\". Make sure this host can be reached over ssh: ssh: connect to host app-server2 port 22: Connection timed out\r\n", "unreachable": true}

对于那些已启动的服务器,我可以看到剧本run-query.yml在其上运行查询。
输出的最后一部分如下所示:

14:51:19  FATAL: command execution failed
14:51:19  hudson.AbortException: Ansible playbook execution failed
14:51:19    at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:262)
...

14:51:20  Stage "task2" skipped due to earlier failure(s)
14:51:20  [Pipeline] }
14:51:20  [Pipeline] // stage
14:51:20  [Pipeline] stage
14:51:20  [Pipeline] { (create the report)
14:51:20  Stage "task3" skipped due to earlier failure(s)

预计一些服务器会关闭,我希望Jenkins可以在run-query.yml之后执行接下来的两个阶段。
如何使代码不跳过stage('task2')stage('task3')

ev7lccsx

ev7lccsx1#

可以使用catchError块 Package 舞台:

stage('run SQL') {
    steps { 
        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
            script {
                echo "Now run SQL on hosts"
                ansiblePlaybook become: true,
                colorized: true,
                credentialsId: 'me',
                installation: 'ansible',
                inventory: 'google/ansible/ansible-root/hosts',
                vaultCredentialsId: 'AnsibleVaultPassword',
                playbook: "ansible//playbooks/run-query.yml"
            }
        }       
    }       
}

您可以更改buildResultstageResult以确定阶段失败时会发生什么。无论您在那里使用什么选项,生成都将继续,而不会由于早期的失败而跳过。您也可以对其他阶段执行此操作。

相关问题