Git Variables in Jenkins Workflow plugin

前端 未结 7 1494
故里飘歌
故里飘歌 2020-12-08 04:17

I\'d like to access git variables such as GIT_COMMIT and GIT_BRANCH when I have checked out a repository from git further down in the build stream.

7条回答
  •  有刺的猬
    2020-12-08 05:02

    This is what I'm doing, based on the example provided in the Jenkins examples repo:

    node {
        git url: 'https://git.com/myproject.git'
    
        sh 'git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
        git_branch = readFile('GIT_BRANCH').trim()
        echo git_branch
    
        sh 'git rev-parse HEAD > GIT_COMMIT'
        git_commit = readFile('GIT_COMMIT').trim()
        echo git_commit
    }
    

    Edit you can do this shorter via

    git_commit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
    

提交回复
热议问题