首页 > 解决方案 > Jenkins Pipeline - Jenkins SCM 步骤。结帐返回不参考提供的参数的无效数据

问题描述

我正在为我的项目设置 CI/CD,并决定使用 Jenkins Pipeline。我基本上需要什么:

  1. 从选定的分支构建(从下拉列表中选择)
  2. 有条件地执行一些阶段(取决于分支)

我将管道配置为从 scm 获取 Jenkinsfile: 在此处输入图像描述 然后我决定编写 cutom scm checkout 如下:

stage('Checkout specific branch') {
        steps {
            echo "Checking out ${params.branchToBuild}..."
            script {
                def actualBranchCheckout = checkout([$class: 'GitSCM',
                                        branches: [
                                                [name: "${params.branchToBuild}"]
                                        ],
                                        doGenerateSubmoduleConfigurations: false,
                                        extensions: [[$class: 'LocalBranch', localBranch: "**"]],
                                        submoduleCfg: [],
                                        userRemoteConfigs: [
                                                [credentialsId: "${env.REPO_SSH_CREDS_ID}", url: "${env.REPO_SSH_URL}"]
                                        ]
                ])
            }
        }
    }

我有几个担忧。GIT_ 环境变量在签出 Jenkinsfile 的分支时设置。

GIT_BRANCH=origin/master
GIT_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_LOCAL_BRANCH=master, GIT_PREVIOUS_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2

这很好,但是当我执行结帐时,并没有使用实际值进行更新并且保持不变。好的,该方法本身返回的地图可能包含我需要的内容,因此我可以手动重置它们,对吗?但是在查看开发分支时它们结果是相同的:

以下是日志:

Obtained Jenkinsfile from git git@bitbucket.org:team/repo.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Repo/repo_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential jenkins-bitbucket
Cloning the remote Git repository
Cloning repository git@bitbucket.org:team/repo.git
 > git init /var/lib/jenkins/workspace/Repo/repo_pipeline # timeout=10
Fetching upstream changes from git@bitbucket.org:team/repo.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url git@bitbucket.org:team/repo.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url git@bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git@bitbucket.org:team/repo.git
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 31e727bf81b3379c9aa80224b8f941bc867acd77 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 31e727bf81b3379c9aa80224b8f941bc867acd77
Commit message: "print out variables"
 > git rev-list --no-walk 11a5800b6ca31bc81545fa4874d73fa275c820c2 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout specific branch)
[Pipeline] echo
Checking out develop...
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential jenkins-bitbucket
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git@bitbucket.org:team/repo.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse origin/develop^{commit} # timeout=10
Checking out Revision 11a5800b6ca31bc81545fa4874d73fa275c820c2 (origin/develop)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 11a5800b6ca31bc81545fa4874d73fa275c820c2
 > git branch -a -v --no-abbrev # timeout=10
 > git checkout -b develop 11a5800b6ca31bc81545fa4874d73fa275c820c2
Commit message: "another try"
First time build. Skipping changelog.
[Pipeline] echo
[GIT_BRANCH:origin/master, GIT_COMMIT:31e727bf81b3379c9aa80224b8f941bc867acd77, GIT_LOCAL_BRANCH:master, GIT_PREVIOUS_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_PREVIOUS_SUCCESSFUL_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_URL:git@bitbucket.org:team/repo.git]

我还有另一种选择手动完成所有操作,例如git rev-parse --abbrev-ref HEAD分支等。有谁知道如何解决这个问题/这是一个已知问题还是我做错了什么?

标签: jenkinsjenkins-pipelinejenkins-plugins

解决方案


推荐阅读