在 jenkinsfile 中指定多个 repos 时触发特定的 Jenkins 作业

Trigger specific Jenkins job when multiple repos specified in jenkinsfile

我为三个 github 存储库(app1、app2 和 app3)中的每一个创建了单独的 Jenkins 作业。 然后是部署和测试存储库。

app1 repo(with webhook enabled) => app1 JenkinsJob
app2 repo(with webhook enabled) => app2 JenkinsJob
app3 repo(with webhook enabled) => app3 JenkinsJob
deployment repo(webhook NOT enabled)
test repo(webhook NOT enabled)

下面是 app1 的 jenkinsfile。(app2 和 app3 只有 GIT_REPO_URL1 不同)。

pipeline {
    environment {
        GIT_REPO_URL1 = 'https://github.com/app1.git'
        GIT_REPO_URL2 = 'https://github.com/deployment.git'
        GIT_REPO_URL3 = 'https://github.com/test.git'
        GIT_REPO_AUTH_CRED = 'abcdefg-123123-321123-qwead-asdqwe123'
    }   agent any
    stages {
        stage('GitClone1') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: 'refs/heads/master']], extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: false, reference: '', trackingSubmodules: false]], userRemoteConfigs: [[credentialsId: "${GIT_REPO_AUTH_CRED}", url: "${GIT_REPO_URL1}"]]])
                echo '========++++++++ GitClone Completed ========++++++++'
            }
        }
        stage('GitClone2') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: 'refs/heads/master']], extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: false, reference: '', trackingSubmodules: false]], userRemoteConfigs: [[credentialsId: "${GIT_REPO_AUTH_CRED}", url: "${GIT_REPO_URL2}"]]])
                echo '========++++++++ GitClone Completed ========++++++++'
            }
        }
        stage('GitClone3') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: 'refs/heads/master']], extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: false, reference: '', trackingSubmodules: false]], userRemoteConfigs: [[credentialsId: "${GIT_REPO_AUTH_CRED}", url: "${GIT_REPO_URL3}"]]])
                echo '========++++++++ GitClone Completed ========++++++++'
            }
        }
    } }

app1/app2/app3 上的代码交付分别触发 app1/app2/app3 jenkins 作业。

现在的问题是, 当我将代码更改推送到 https://github.com/deployment.git or https://github.com/test.git 存储库时,app1、app2 和 app3 的 Jenkins 管道会自动触发。

预期修复: 应用程序 jenkins 作业应仅通过向应用程序存储库交付代码来触发。

checkout 步骤中的 GitSCM class 有一个专门的附加行为,用于为已配置的存储库禁用触发器:

Don't trigger a build on commit notifications

If checked, this repository will be ignored when the notifyCommit-URL is accessed regardless of if the repository matches or not.

此属性可以通过使用 class IgnoreNotifyCommit.
的扩展在管道中配置 因此,要实现您想要的效果,只需将此配置添加到不应触发作业的每个存储库的 checkout 选项中。
例如:

 checkout([$class: 'GitSCM', branches: [[...]], extensions: [[$class: 'IgnoreNotifyCommit'],[$class: 'SubmoduleOption',...]], userRemoteConfigs: [[...]]])

我通过在代码中添加 changelog: falsepoll: false 得到它。

完整列表如下。 有人可能会发现这很有用。

checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${GIT_CRED}", url: "${GIT_REPO_URL2}"]]]