Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing SCM (Git) variables on a Jenkins Pipeline job [duplicate]

Tags:

Here's my pipeline code:

node ('master') {     git url: "$GIT_REPO_URL", branch: "$GIT_BRANCH"     echo env.GIT_COMMIT     echo env.GIT_BRANCH     echo env.GIT_REVISION } 

The build results looks like:

Started by user anonymous [Pipeline] Allocate node : Start Running on master in /var/lib/jenkins/jobs/test/workspace [Pipeline] node { [Pipeline] git  > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository  > git config remote.origin.url https://acme/scm/app.git # timeout=10 Fetching upstream changes from https://acme/scm/app.git  > git --version # timeout=10  > git fetch --tags --progress https://acme/scm/app.git +refs/heads/*:refs/remotes/origin/*  > git rev-parse refs/remotes/origin/master^{commit} # timeout=10  > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision fb455725db1b768ff63e627a087d2771099af7c4 (refs/remotes/origin/master)  > git config core.sparsecheckout # timeout=10  > git checkout -f fb455725db1b768ff63e627a087d2771099af7c4 # timeout=10  > git branch -a -v --no-abbrev # timeout=10  > git branch -D master # timeout=10  > git checkout -b master fb455725db1b768ff63e627a087d2771099af7c4  > git rev-list fb455725db1b768ff63e627a087d2771099af7c4 # timeout=10 [Pipeline] echo null [Pipeline] echo null [Pipeline] echo null [Pipeline] } //node [Pipeline] Allocate node : End [Pipeline] End of Pipeline Finished: SUCCESS 

The env variables env.GIT_COMMIT, env.GIT_BRANCH are not populated. Are those values available in another variables?

like image 244
Henrique Gontijo Avatar asked Mar 08 '16 17:03

Henrique Gontijo


People also ask

How can you clone a git repository via Jenkins?

Configuring Git with Jenkins Now open your project and go to configure. Step 2: Give the repository Url in Source Code Management, repository Url can be fetched by clicking on clone and download option of Github and you have to select the SSH Url. Also, add credentials there of Jenkins.

How do I pass a git branch as parameter in Jenkins?

If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.

How are variables defined in Jenkins pipeline?

Jenkins pipeline environment variables: You can define your environment variables in both — global and per-stage — simultaneously. Globally defined variables can be used in all stages but stage defined variables can only be used within that stage. Environment variables can be defined using NAME = VALUE syntax.


1 Answers

Here is an example of how you can get GIT_COMMIT (ref: Jenkins GitHub):

// These should all be performed at the point where you've // checked out your sources on the agent. A 'git' executable // must be available. // Most typical, if you're not cloning into a sub directory shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() 

You can extend it to expose GIT_BRANCH as well. This script is from the workflow examples git repo managed by cloudbees. Maybe you can send a pull request if you add capability to retrieve GIT_BRANCH variable.

like image 159
Chida Avatar answered Sep 20 '22 12:09

Chida