代码之家  ›  专栏  ›  技术社区  ›  Philip Shangguan

让詹金斯不要在出现故障时跳过阶段

  •  0
  • Philip Shangguan  · 技术社区  · 1 年前

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

    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.yml 在中定义的主机列表上运行 google/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}
    

    对于那些已经启动的服务器,我可以通过剧本看到在它们上运行的查询 运行查询.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能在之后执行接下来的两个阶段 运行查询.yml

    如何使代码不跳过 stage('task2') stage('task3') ?

    0 回复  |  直到 1 年前
        1
  •  1
  •   M B    1 年前

    你可以用 catchError 块:

    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"
                }
            }       
        }       
    }
    

    您可以更改 buildResult stageResult 以确定当阶段失败时会发生什么。无论你在那里使用什么选项,构建都将继续,不会因为之前的失败而跳过。你也可以对其他阶段这样做。