1

The following Jenkinsfile:

pipeline {
  agent any

  stages {
    stage( "1" ) {
      steps {
        script {
          def br = "demo_branch"
          def clone_url = "ssh://git@server.com:7999/spc/my_prj.git"
          def cred_id = "ssh_cred_id"
          def relative_dir = "my_prj"
          checkout([$class: 'GitSCM',
                    branches: [[name: br]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'RelativeTargetDirectory',
                                  relativeTargetDir: relative_dir]],
                    submoduleCfg: [],
                    userRemoteConfigs: [[url: clone_url,
                                        credentialsId: cred_id]]])

        }
      }
    }
  }

  post {
    always {
      script {
        echo "the end"
      }
    }
  }
}

...results in the directory /home/jenkins/workspace/job/my_prj/ being created in my Jenkins server, and that directory contains my cloned git repo.

I would like to cd to this directory, run the command git rev-parse --abbrev-ref HEAD and get the value "demo_branch", but instead I get the value "HEAD". I am pretty certain that this is because the Jenkinsfile checkout command is checking out refs/heads/demo_branch instead of demo_branch. I.e.:

user@server:/home/user# cd /home/jenkins/workspace/job/my_prj/
user@server:/home/jenkins/workspace/job/my_prj# git rev-parse --abbrev-ref HEAD
HEAD
user@server:/home/jenkins/workspace/job/my_prj# git checkout demo_branch
Switched to branch 'demo_branch'
Your branch is up-to-date with 'origin/demo_branch'.
user@server:/home/jenkins/workspace/job/my_prj# git rev-parse --abbrev-ref HEAD
demo_branch

Is there a way I can get the Jenkins checkout command to check out demo_branch instead of refs/heads/demo_branch?

torek
  • 448,244
  • 59
  • 642
  • 775
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
  • #1 Do you known the branch name before the git clone? #2 Is your requirement just change of branch? – JRichardsz Sep 12 '21 at 16:35
  • @JRichardsz - #1 For the purpose of this question, let's say that the branch name is the fixed string "demo_branch"...but if it makes a difference: in reality, it's in a variable, e.g. `def branch = "demo_branch"` as in the post. #2 The requirement is that I should be able to `cd` to the directory created by the `checkout` command, be able to run `git rev-parse --abbrev-ref HEAD` and get the value "demo_branch" or whatever is the value of variable `branch`. – StoneThrow Sep 12 '21 at 16:47
  • #1 if branch name is already known, you could try with: `git clone myrepo.git -b demo_branch` #2 Could you try with a public repo in github? If repository has several branches , you could list with `git branch -a`. Verify if it returns **refs/heads/aaa refs/heads/bbb** or just **aaa bbb** – JRichardsz Sep 12 '21 at 17:05
  • I tried with a public repository. Branch name is returned as expected: https://i.ibb.co/yksQnpP/git-rev-parse.png – JRichardsz Sep 12 '21 at 17:16
  • @JRichardsz - when you say "you could try with: `git clone myrepo.git -b demo_branch`", that means `sh(script: 'git clone myrepo.git -b demo_branch')` -- is that correct? I'm actually pretty confident that will work, but I was specifically wondering whether there's a way to get this "equivalent functionality" with the Jenkins `checkout` function, whether there's some flag, or some different branch pattern, etc. that I need to specify. Because I have a preference of using plugin commands over `sh` invocations -- whether that preference is justified is something I'm happy to be educated on. – StoneThrow Sep 12 '21 at 19:53
  • #1 yes you are right. With git clone... it works. If you want yo avoid shell commands, you could use the git plugin: git( ....) . #2 Usually branch name is a parameter of pipeline driven to push events + webhooks. Example: some developer push something to a specific branch, so branch is known. If branch is known, jenkins could clone that branch and starts the build, test, deploy, etc. If you have a repository that receive pushes that no body knows, in that case your jenkins needs a routine to extract the expected branch, that could be problematic. I didn't use checkout feature – JRichardsz Sep 12 '21 at 20:42
  • Check this https://stackoverflow.com/questions/9767919/in-jenkins-how-to-checkout-a-project-into-a-specific-directory-using-git It appears that checkout(since svn) is a kind of legacy because git feature is enough. Also chekcout is used to clone in a custom folder. Is your main goal clone a specific branch of git repository using pure plugins? is branch known before of clone operation? or do you need a regex or another complex operation to determine the branch to be cloned? – JRichardsz Sep 12 '21 at 20:49

0 Answers0