2

I can invoke a simple Jenkins pipeline from gitlab merge requests using a webhook. Now I'd like to know what is the source branch to make the checkout against it. Example: if I push code to develop branch, in my pipeline script i'd checkout develop branch. Thanks.

node {
  stage('Build') {
    def mybranch = '?' // get branch name from gitlab webhook
    git branch: mybranch,
    credentialsId: 'mycredential',
    url: 'myurl'
    // ...
  }
}
3psilon
  • 135
  • 3
  • 11

3 Answers3

3

You can get the current branch name via env.gitlabBranch.

Reference: gitlab-plugin

Jack Chen
  • 624
  • 5
  • 14
1

You can parameterize your pipeline and use the webhook payload data to fill in the branch value as described here.

ben5556
  • 2,915
  • 2
  • 11
  • 16
1

GitLab plugin creates a lot of useful environment variable. You can see them in here. I think the one you need is CI_COMMIT_REF_NAME

secilusta
  • 86
  • 4