4

我对使用 JenkinsFile 和 GIT 插件的 Jenkins 多分支管道有疑问。

问题是每次推送到暂存分支也会触发 master 的管道。期望的行为是推送到暂存分支仅触发暂存管道,推送到主分支仅触发主管道

这是我的 JenkinsFile

#!/usr/bin/env bash
pipeline {
    agent any
    triggers {
        pollSCM('*/1 * * * *')
    }
    environment {
        GCLOUD_PATH="/var/jenkins_home/GoogleCloudSDK/google-cloud-sdk/bin"
    }
    stages {
        stage('Git Checkout'){
            steps{
              // Clean Workspace
              cleanWs()
              // Get source from Git
              git branch: 'staging', 
                credentialsId: ****', 
                url: 'git@github.com:***/****.git'
            }    
        }
        stage('Update Staging') {
            when {
                branch 'staging'
            }
            environment{
                INSTANCE="***"        
            }
            steps {
                sshagent(credentials : ['****']) {
                    sh 'ssh -tt -o StrictHostKeyChecking=no jenkins@"${INSTANCE}" sudo /opt/webapps/****/deploy.sh firstinstance'
                }
            }
        }
        stage('Update Production') {
            when {
                branch 'master'
            }
            environment{
                gzone="us-central1-a"
            }
            steps {
                    sh '''
                        #!/bin/bash
                        echo "${BRANCH_NAME}"
                        export instances=$("${GCLOUD_PATH}"/gcloud compute instances list --filter="status:(running) AND tags.items=web" --format="value(name)")
                        FIRST=1
                        for instance in ${instances}
                        do
                            echo "### Running Instance: ${instance} ###"
                            if [[ $FIRST == 1 ]]; then
                                echo "first instance"
                                ${GCLOUD_PATH}/gcloud compute ssh jenkins@${instance} --zone ${gzone} '--ssh-flag=-tt -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no' --command="echo first"
                            else
                                ${GCLOUD_PATH}/gcloud compute ssh jenkins@${instance} --zone ${gzone} '--ssh-flag=-tt -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no' --command="sudo uptime"
                            fi
                            FIRST=0
                        done
                    '''

            }
        }
    }
    post {
        success {
            cleanWs()
        }
    }

}

我将分享一些日志:这是主分支的日志

http://34.69.57.212:8080/job/tinytap-server/job/master/2/pollingLog/  returns
Started on Dec 10, 2019 1:42:00 PM
Using strategy: Specific revision
[poll] Last Built Revision: Revision 12ecdbc8d2f7e7ff1f578b135ea0b23a28d7672d (master)
using credential ccb9a735-04d9-4aab-8bab-5c86fe0f363c
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git ls-remote -h -- https://github.com/tinytap/tinytap-web.git # timeout=10
Found 222 remote heads on https://github.com/tinytap/tinytap-web.git
[poll] Latest remote head revision on refs/heads/master is: 12ecdbc8d2f7e7ff1f578b135ea0b23a28d7672d - already built by 1
Using strategy: Default
[poll] Last Built Revision: Revision f693e358ce14bc5dfc6111e62ed88e6dd1d0dfc9 (refs/remotes/origin/staging)
using credential 17f45a89-da78-4969-b18f-cb270a526347
 > git --version # timeout=10
using GIT_SSH to set credentials jenkins key
 > git ls-remote -h -- git@github.com:tinytap/tinytap-web.git # timeout=10
Found 222 remote heads on git@github.com:tinytap/tinytap-web.git
[poll] Latest remote head revision on refs/heads/staging is: 907899a0e7e131e9416ee65aad041c8da111e2fe
Done. Took 1 sec 
Changes found 

这是 master 分支的日志,但只有 staging 有一个新的提交:

http://34.69.57.212:8080/job/tt-server/job/master/3/pollingLog/    returns
Started on Dec 10, 2019 1:55:00 PM
Using strategy: Specific revision
[poll] Last Built Revision: Revision 12ecdbc8d2f7e7ff1f578b135ea0b23a28d7672d (master)
using credential ****-****-****-****-5c86fe0f363c
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git ls-remote -h -- https://github.com/tt/tt-web.git # timeout=10
Found 222 remote heads on https://github.com/tt/tt-web.git
[poll] Latest remote head revision on refs/heads/master is: 12ecdbc8d2f7e7ff1f578b135ea0b23a28d7672d - already built by 2
Using strategy: Default
[poll] Last Built Revision: Revision 907899a0e7e131e9416ee65aad041c8da111e2fe (refs/remotes/origin/staging)
using credential ****-****-****-****-cb270a526347
 > git --version # timeout=10
using GIT_SSH to set credentials jenkins key
 > git ls-remote -h -- git@github.com:tt/tt-web.git # timeout=10
Found 222 remote heads on git@github.com:tt/tt-web.git
[poll] Latest remote head revision on refs/heads/staging is: eab6e8bc6d8586084e9fe9856dec7fd8b31dd098
Done. Took 0.98 sec 
Changes found 

即使 master 分支上的 head 没有更改,请注意“发现更改”

詹金斯版 2.190.1 Git 插件版本 4.0.0 Git 客户端插件版本 2.9.0

4

2 回答 2

2

我使用这个插件 - https://github.com/lachie83/jenkins-pipeline它对我来说很好用。您需要为每个分支设置单独的 if 块,然后在其中设置 stage 块。下面的例子:

#!/usr/bin/groovy

@Library('https://github.com/lachie83/jenkins-pipeline@master')

def pipeline = new io.estrado.Pipeline()
def cloud = pipeline.getCloud(env.BRANCH_NAME)
def label = pipeline.getPodLabel(cloud)

// deploy only the staging branch
if (env.BRANCH_NAME == 'staging') {
    stage ('deploy to k8s staging') {
      //Deploy to staging
    }
}
// deploy only the master branch
if (env.BRANCH_NAME == 'master') {
    stage ('deploy to k8s production') {
      //Deploy to production
    }
}
于 2019-12-27T19:09:34.637 回答
0

我认为您的 Jenkinsfile 中有一些逻辑遗漏。按照目前的情况,您轮询 SCM 以获取更改。如果检测到任何更改,第一阶段“Git Checkout”将检查暂存分支(始终)。然后你有另一个阶段,如果分支正在“暂存”(它是,因为它被硬编码以检查上面的分支)等,你有另一个阶段。这将是首先要修复的 - 如果检测到 SCM 更改,检查正确的分支. 如何 - 有几个选项。我通常在“选项”中使用“skipDefaultCheckout()”以及在我的第一个管道阶段显式结帐:

        steps {
            sshagent(['github-creds']) {
                git branch: "${env.BRANCH_NAME}", credentialsId: 'github-creds', url: 'git@github.com:x/y.git'
            }
        }

第二件事是您尝试将处理两个不同的分支压缩到一个 Jenkinsfile 中。这不是应该做的。Jenkins 将使用来自给定分支的 Jenkinsfile - 只需确保 Jenkinsfile 上的暂存包含您希望它包含的内容,与 master 上的 Jenkinsfile 相同。

希望能帮助到你。

于 2019-12-27T10:50:54.553 回答