How to receive local Git branch name with Jenkins Git plugin?

前端 未结 12 745
闹比i
闹比i 2020-12-12 17:52

I am using Branch Specifier option of Jenkins Git plugin (v2.0) to run build on specific branch, e.g. 1.4.

${GIT_BRANCH} in this case conta

相关标签:
12条回答
  • 2020-12-12 18:41

    As of March 2016 (Git Plugin v2.4.3) this is now supported natively. Simply put ** as the value for Check out to specific local branch in the Git Plugin, instead of any macro/variable.

    The description for the field details the new behavior:

    If selected, and its value is an empty string or "**", then the branch name is computed from the remote branch without the origin. In that case, a remote branch origin/master will be checked out to a local branch named master, and a remote branch origin/develop/new-feature will be checked out to a local branch named develop/newfeature.

    This was introduced by @roostergx & @NickVolynkin mentioned when it was released. I'm just documenting here to hopefully help folks in the future.

    0 讨论(0)
  • 2020-12-12 18:42

    You can strip the prefix from the variable pretty easily: ${GIT_BRANCH##origin/}

    Although this is not a very general solution, it's quite simple and I haven't seen a repository cloned by Jenkins, which would use something else than origin for the remote name.

    Update: Actually, ${GIT_BRANCH#*/} will work for any origin and even for branches containing slashes. The # does non-greedy glob matching, ## enables greedy matching. See Bash Reference Manual for details.

    0 讨论(0)
  • 2020-12-12 18:42

    I ran into this problem as well to day. It seems to be that when I change the value of Check out to specific local branch, I need to delete the local repo on the build server before triggering a new build.

    0 讨论(0)
  • 2020-12-12 18:43

    I managed to do that with a couple steps:

    1. Add Execute shell pre step with these:

      git_branch_local=$(echo $GIT_BRANCH | sed -e "s|origin/||g") echo GIT_BRANCH_LOCAL=$git_branch_local > build.properties

    2. Add an Inject environment variables pre step for build.properties file

    then you should have a GIT_BRANCH_LOCAL environment variable that doesn't have origin/

    0 讨论(0)
  • 2020-12-12 18:43

    In a pipeline you can do it like this :

    // get arround a git scm anoying feature
    stage ('Bootstrap') {
        sh "echo GIT_BRANCH_LOCAL=\\\"$GIT_BRANCH\\\" | sed -e 's|origin/||g' | tee version.properties"
    }
    
    stage('Checkout') {
        load('version.properties')
        checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "${GIT_BRANCH_LOCAL}"]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/webofmars/grails-website.git']]])
    }
    
    0 讨论(0)
  • 2020-12-12 18:44

    After long time I finally got the solution for my case to get local git name (for SonarQube) without "origin/" in my Jenkins freestyle project (not Jenkinsfile). I am using Windows environment.

    In Jenkins:

    • At "Source Code Management" section: add "Check out to specific local branch" and at "Branch name" enter: **
    • At "Build" section: I used and added "SonarScanner for MSBuild - Begin Analysis", where at "Additional arguments" I added: /d:sonar.branch.name=${GIT_LOCAL_BRANCH}

    So only with this combination of ** and ${GIT_LOCAL_BRANCH} it worked for me.

    0 讨论(0)
提交回复
热议问题