Jenkins pipeline branch name returns null

后端 未结 7 688
滥情空心
滥情空心 2021-01-03 22:25

I\'m trying to get the name of my branch for a jenkins groovy script. I cannot get the current branch name. I try the following:

stage(\'Check out code\')
ch         


        
相关标签:
7条回答
  • 2021-01-03 23:21
    git.exe checkout -f 33b531b2f1caaf8b64d968e437306f39d2dba1da
    

    That would make the git repo enter a detached HEAD mode, which, by its very nature, has no branch.

    From Jenkinsfile:

    The checkout step will checkout code from source control; scm is a special variable which instructs the checkout step to clone the specific revision which triggered this Pipeline run.

    So the ${env.BRANCH_NAME} is null.

    As mentioned in "Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT", you can get the SHA1 you just checked out with the groovy syntax (to be adapted in a Jenkins pipeline DSL):

    sh 'git rev-parse HEAD > commit'
    def commit = readFile('commit').trim()
    
    0 讨论(0)
提交回复
热议问题