如何运行会触发管道外的其他作业

时间:2018-10-11 16:56:06

标签: jenkins jenkins-plugins jenkins-pipeline

我有3个工作正在准备中。每当有人由于内部帐户锁定而失败时,这些人都必须触发构建后操作。在POst构建操作中,我提到了构建失败时触发。我编写了一个机器人测试来解锁该帐户,并编写了一个外壳脚本来调用该测试。 我在构建后动作中的两个作业中都调用了这个模板,并在同一节点上构建它。但是我发现这个构建后动作保持为待处理状态,詹金斯正在触发下游项目。当前工作失败时,如何让Jenkins运行构建后动作?

如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用种子作业的传播属性。

简单的例子:

Map jobResults = [:]

pipeline {

    agent any

    stages {
        stage('Build seedjob 1') {
            steps {
                script {
                    String seedJobName = 'testjob1'

                    def seedJob = build job: seedJobName, propagate: false
                    jobResults[seedJobName] = seedJob.result

                    echo "Result of ${seedJobName}: ${seedJob.result}"
                }
            }
        }

        stage('Build seedjob 2') {
            steps {
                script {
                    String seedJobName = 'testjob2'

                    def seedJob = build job: seedJobName, propagate: false
                    jobResults[seedJobName] = seedJob.result

                    echo "Result of ${seedJobName}: ${seedJob.result}"
                }
            }
        }
    }

    post {
        success { 
            script {
                if(jobResults['testjob1'] == 'FAILURE') {
                    echo "Running another job"
                    build job: 'another-job1', propagate: true
                }

                if(jobResults['testjob2'] == 'FAILURE') {
                    echo "Running another job"
                    build job: 'another-job2', propagate: true
                }

            }
        }
    }
}