Git Variables in Jenkins Workflow plugin

前端 未结 7 1538
故里飘歌
故里飘歌 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:01

    You can define your jobs (extracting git info from last commit) inside node for execution in a queue.

    node {
    
      //Code checkout from SCM (here - `git`)
      checkout scm
    
      stage("GIT INFO"){
        echo ":::::::::::GIT_SHORT_COMMIT::::::::::::::::::::::::"
    
        GIT_SHORT_COMMIT = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
        //echo in jenkins console
        echo GIT_SHORT_COMMIT
        //wanted to send these info to build artifacts, append to any file
        sh("echo ${GIT_SHORT_COMMIT} > GIT_SHORT_COMMIT")
    
        //Similar proceed for other git info's 
        echo ":::::::::::GIT_COMMITTER_EMAIL::::::::::::::::::::::::"
    
        GIT_COMMITTER_EMAIL = sh(returnStdout: true, script: "git show -s --pretty=%ae").trim()
        sh("echo ${GIT_COMMITTER_EMAIL} > GIT_COMMITTER_EMAIL-${GIT_COMMITTER_EMAIL}")
    
    
    
        echo ":::::::::::GIT_COMMITTER_NAME::::::::::::::::::::::::"
    
        GIT_COMMITTER_NAME = sh(returnStdout: true, script: "git show -s --pretty=%an").trim()
        sh("echo ${GIT_COMMITTER_NAME} > GIT_COMMITTER_NAME-${GIT_COMMITTER_NAME}")
      }
    

    After your job is finished, you will see three additional file from above task in your workspace :

    . |-- GIT_COMMITTER_EMAIL_email@gmail.com |-- GIT_COMMITTER_NAME-username |-- GIT_SHORT_COMMIT_

提交回复
热议问题