3psilon
3psilon

Reputation: 135

what is git branch name in jenkins pipeline when invoked by gitlab webhook

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'
    // ...
  }
}

Upvotes: 2

Views: 5060

Answers (3)

Jack Chen
Jack Chen

Reputation: 680

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

Reference: gitlab-plugin

Upvotes: 3

secilusta
secilusta

Reputation: 86

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

Upvotes: 1

ben5556
ben5556

Reputation: 3018

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

Upvotes: 1

Related Questions