在Jenkinsfile中隔离存储库分支的名称

时间:2018-05-01 22:51:40

标签: git jenkins continuous-integration jenkins-pipeline

  

为了将提交分支的名称隔离到可以在日志中打印出来的变量,需要对下面的Jenkinsfile进行哪些具体更改? In在下面的例子中,分支的名称是GWS-43-getissueforcommit

以下是具体细节:

<小时/>的 Jenkinsfile: <小时/>

以下Jenkinsfile在运行checkout(scm).GIT_ASKPASS命令产生的输出中打印出分支名称,但是当后续代码行(sh "echo 'The repo Ask Pass is: ${repoAskPass}'")丢失此信息时尝试打印出命令的封装结果:

node {
    // Clean workspace before doing anything
    deleteDir()
    try {
        stage ('Clone') {
            def repoAskPass = checkout(scm).GIT_ASKPASS
            sh "echo 'The repo Ask Pass is: ${repoAskPass}'"
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}


产生的日志:

生成的日志输出为:

General SCM<1s
    Cloning the remote Git repository
    Cloning with configured refspecs honoured and without tags
    Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git init /var/jenkins_home/workspace/ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA # timeout=10
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git --version # timeout=10
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
     > git config --add remote.origin.fetch +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit # timeout=10
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    No valid HEAD. Skipping the resetting
     > git clean -fdx # timeout=10
    Fetching without tags
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
    Checking out Revision 375b17c4e7453d802d94659836db436553cc7f0c (GWS-43-getissueforcommit)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 375b17c4e7453d802d94659836db436553cc7f0c
     > git branch -a -v --no-abbrev # timeout=10
     > git checkout -b GWS-43-getissueforcommit 375b17c4e7453d802d94659836db436553cc7f0c
    Commit message: "isolate ASKPASS"
     > git rev-list --no-walk 268c468a96de0fb27b6f205658a169b38871b581 # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
     > git reset --hard # timeout=10
     > git clean -fdx # timeout=10
    [Bitbucket] Notifying commit build result

echo 'The repo Ask Pass is: null'— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo The repo Ask Pass is: null
    The repo Ask Pass is: null


重申问题:

  

如何修改上述Jenkins文件以便在日志中输出以下行:

The name of the branch containing the commit for this build is: 
GWS-43-getissueforcommit

2 个答案:

答案 0 :(得分:4)

Starting app-scripts server: --address 0.0.0.0 --port 8100 --livereload-port 35729 --dev-logger-port 53703 --nobrowser - Ctrl+C to cancel [01:44:47] watch started ... [01:44:47] build dev started ... [01:44:47] clean started ... [01:44:47] clean finished in 6 ms [01:44:47] copy started ... [01:44:47] deeplinks started ... [01:44:47] deeplinks finished in 21 ms [01:44:47] transpile started ... [01:44:50] transpile finished in 3.19 s [01:44:50] preprocess started ... [01:44:50] preprocess finished in less than 1 ms [01:44:50] webpack started ... [01:44:50] copy finished in 3.37 s [01:44:55] webpack finished in 5.10 s [01:44:55] sass started ... Without `from` option PostCSS could generate wrong source map and will not find Browserslist config. Set it to CSS file path or to `undefined` to prevent this warning. [01:44:56] sass finished in 974 ms [01:44:56] postprocess started ... [01:44:56] postprocess finished in 8 ms [01:44:56] lint started ... [01:44:56] build dev finished in 9.44 s [01:44:57] watch ready in 9.52 s [01:44:57] dev server running: http://localhost:8100/ [OK] Development server running! Local: http://localhost:8100 External: http://111.111.1.111:8100 DevApp: foo@8100 on DESKTOP-11QG111 用于获取用户凭据 - 它与分支无关。

Here是插件设置的变量列表,您可以使用它们。从这里您可以看到GIT_ASKPASS为您提供了远程分支,GIT_BRANCH为您提供了签出的本地分支。

GIT_LOCAL_BRANCH

这将打印远程分支名称以及原始前缀(通常是可取的)。但如果你想要它没有前缀:

def branch = checkout(scm).GIT_BRANCH
sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"

答案 1 :(得分:0)

鉴于您提到Jenkinsfile,我猜您正在使用Multibranch项目。如果是,则分支名称始终可用作环境变量:env.BRANCH