首页 > 解决方案 > git checkouts 多次,尽管在 Jenkins 管道中只签出一次

问题描述

我有一个 Jenkins 管道代码,它有 4 个阶段,即

1. CHECKOUT     <--- git checkout only happens here
2. DEV
3. DEV2
3. DEV3

我只在第一阶段做 git checkout 即CHECKOUT

下面是我的管道代码

stages {
      stage('checkout') {           
            steps{
                script {
                                    def git_params = checkout([$class: 'GitSCM'])

                                    println "*/${GIT_BRANCH}"

                                    // Loading environment variables
                                    checkout([
                                        $class: 'GitSCM',
                                        branches: [
                                            [name: "*/${GIT_BRANCH}"]
                                        ],
                                        extensions: [
                                            [$class: 'WipeWorkspace'],
                                        ],
                                        userRemoteConfigs: [[credentialsId: MYAPP_GIT_CREDENTIAL_ID, url: MYAPP_GIT_REPOSITORY]]
                                    ])


                                    PIPELINE_NODE = NODE_NAME
                                    dir('temp1'){
                                        checkout([
                                            $class: 'GitSCM',
                                            branches: [
                                                [name: '*/master']
                                            ],
                                            userRemoteConfigs: [[credentialsId: MY_GIT_CREDENTIAL_ID, url: MY_GIT_REPOSITORY]]
                                        ])
                                    }
                                    stash name: "temp1", includes: "temp1/*"
                                    
                                    //Load variable file from GitSCM inorder to get Maven variable
                                    
                                load "temp1/${APP_NAME}_${STARTENV}_EnvFile"
                                //load "temp1/${APP_NAME}_*_EnvFile"
                                sh 'chmod 755 temp1/git_version.sh'
                                env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
                                sh 'echo REL#=$APP_VERSION'
                                
                                }
                    }
                    
            }
            
        }
        
        
    stage('DEV'){ 
        agent any
            when {
                branch 'develop'
            }
                steps{
                    script {
                            env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
                            }
                    }
            }
            
    stage('DEV2'){ 
        agent any
            when {
                branch 'develop2'
            }
                steps{
                    script {
                            env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
                            }
                    }
            }

    stage('DEV3'){ 
        agent any
            when {
                branch 'develop3'
            }
                steps{
                    script {
                            env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
                            }
                    }
            }               
    

    
            

但是,在输出中,您可以看到结帐可以在日志中多次看到,而我预计它只会显示一次。尽管跳过了 DEV 和 DEV2 阶段,但 git checkout 在跳过后会被记录,如下面的输出所示。

......
Stage "DEV" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (DEV2)
[Pipeline] node
Running on JENKINS_SLAVE5-SLAVE-01 in /apps/jenkins/workspace/ASERVE_release_2.0.4@2
[Pipeline] {
[Pipeline] checkout
The recommended git tool is: /bin/git
using credential JENKINS_CREDEN
Fetching changes from the remote Git repository
Fetching without tags
Checking out Revision a547d4a01537e9869ae3fccc8debf66a9343f723 (release/2.0.4)
Commit message: "VelocityAdapter_Jenkinsfile edited online with Bitbucket"
 > /bin/git config remote.origin.url https://bitbucket.mycomp.com/scm/mhmwp/adapter.git # timeout=10
 > /bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /bin/git config core.sparsecheckout # timeout=10
 > /bin/git checkout -f a547d4a01537e9869ae3fccc8debf66a9343f723 # timeout=10
[Pipeline] withEnv
[Pipeline] {
 > /bin/git rev-parse --is-inside-work-tree # timeout=10
 > /bin/git config remote.origin.url https://bitbucket.mycomp.com/scm/mhmwp/adapter.git # timeout=10
Fetching upstream changes from https://bitbucket.mycomp.com/scm/mhmwp/adapter.git
 > /bin/git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials JENKINS_CREDEN
 > /bin/git fetch --no-tags --progress https://bitbucket.mycomp.com/scm/mhmwp/adapter.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /bin/git config core.sparsecheckout # timeout=10
 > /bin/git checkout -f a547d4a01537e9869ae3fccc8debf66a9343f723 # timeout=10
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
Stage "DEV2" skipped due to when conditional
........
......

你能建议吗?

标签: jenkinsjenkins-pipeline

解决方案


推荐阅读