get git commit SHA within Jenkins pipeline with JGit - jenkins

I have a Jenkins with the Github organization plugin that scans my organization and build all the branches/PRs of all the repositories within the organization that have a Jenkinsfile.
It works great, but I would like to retrieve for each build the commit SHA, in order to tag Docker images with both the branch name and the commit SHA.
Getting the branch name works great with env.BRANCH_NAME, however I cannot find any way to get the commit SHA.
The catch is that we are using JGit, so I cannot use git log to retrieve it.
I tried having a look at what is contained in the ENV using sh 'printenv', but there's nothing of any use there.
I also tried the following:
def checkoutResults = checkout scm
echo 'checkout results: ' + checkoutResults
but this yields the following result:
checkout results: [:]
Even though I cannot get the revision from my pipeline, Jenkins is getting it alright, as I can see in the logs:
...
Obtained Jenkinsfile from 98062e5f651ca698f4303c3bb8d20665ce491294
...
Checking out Revision 98062e5f651ca698f4303c3bb8d20665ce491294 (docker)
I am running the following versions:
Jenkins 2.73.3
Git plugin plugin 3.3.0
Pipeline: SCM Step plugin 2.6
Would appreciate any help in retrieving the commit SHA / revision in this particular situation.

The git plugin did not include the fix until the 3.3.2 release. You'll need to update to at least git plugin 3.3.2.
The current git plugin release as of 23 Nov 2017 is 3.6.4. It includes significant additions and changes for multi-branch pipelines.
There is also a known bug in the reporting of NAME and EMAIL values which has a regression test that confirms the bug affects all implementations (git and jgit). You can use that regression test as an example of using those values if needed.
If you cannot update from git plugin 3.3.0 to 3.3.2, you may be able to use the JGit classes from within the pipeline script to perform the same type of query as was offered with command line git in another answer to the question. I have never done it, but I believe it is possible.

I created a small groovy function
def getCommitSha(){
return sh(returnStdout: true, script: 'git rev-parse HEAD')
}
you can add it to your pipeline , or to your shared library if you are using one ( if not it's a good time to start ... :-) )

Related

Jenkins Multibranch Pipeline: script / jenkinsfile as svn external

I have a multibranch pipeline in Jenkins. I want to include my script file (jenkinsfile) as svn file external into my development branches to organize the script centralized for all branches. Unfortunately the scan of the multibranch pipeline isn't able to find the script file as it is only looking inside the declared branch and not in the included svn external locations.
Has anyone an idea how can I fix this?
Below is an example of my svn structure, job config and further information.
SVN:
root/
scripts/
jenkinsfile
code/
version1/
branchX/
...
version11/
branchY/
...
SVN external property for branchX, branchY, etc.
Local path: jenkinsfile
URL: ^/scripts/jenkinsfile
Revision Peg: 12345
Multibranch job configuration:
Subversion
Project Repository Base: http://.../root/code/
Include branches: version1/branchX, version11/branchY
Build configuration
Mode: by Jenkinsfile
Script path: jenkinsfile
Log message of scan in multibranch pipeline:
...
Checking candidate branch /code/version1/branchX#HEAD
‘jenkinsfile’ not found
Does not meet criteria
...
I already tried to disable the lightweight checkout of the subversion scm plugin according to this advice:
Multibranch pipeline with jenkinsfile in svn:external
(I've added -Djenkins.scm.impl.subversion.SubversionSCMFileSystem.disable=true under <service><arguments>... in jenkins.xml)
But jenkins is still not able to find the script. And in fact if I put my script directly in e.g. branchX the disabled lightweight checkout leads to a double checkout into my workspace (first one to read the script file and second one as it's my first stage in the script itself).
Maybe my whole setup is wrong too or not the ideal way of doing?
I would be pleased about your help and tips. Thanks and Greetings!
If you are working on a linux or bsd(osx) system, you could create a hard-link from root/scripts/jenkinsfile to root/code/version#/branchX/jenkinsfile for each active branch
That way, each branch will have its own jenkinsfile available locally, enabling you to use the lightweight checkout, and any change you introduce to the jenkinsfile in any location will be available to all other branches (the file system will keep a single copy of the file, regardless of being accessible form many different locations).
The bash command to create such link will be
ln root/scripts/jenkinsfile root/code/version#/branchX/jenkinsfile
You will need to remember to create a new link each time a branch is created, or automate that using hooks

Unable to do git commit in jenkins dsl

Is it possible to do git commit using jenkins dsl.I tried:
shell() - doesn't recognize git command
#library()- i have working library that does take care of git commit. It is working in jenkins pipeline job but not in dsl(throws error at first line where #libary() or library "libname" is used)
sshAgent(credentials){} - didn't work in dsl
I tried to use git plugins but there is no commit for existing plugins.
The shell command will work if your build agent contains a Git installation. Either pre-install Git on your agents or use the Global Tool Configuration to setup a Git installation and then use the Tool Environment Plugin to get a pointer to that installation.
If you setup a Git installation called GIT2 in Global Tool Configuration, the following should work depending on your OS and the installation method.
job('example') {
wrappers {
toolenv('GIT2')
}
steps {
shell('$GIT2_HOME/bin/git commit -am "test"')
}
}

Jenkins Environment variables giving null values as output

I am trying to access github env.CHANGE_AUTHOR environment variable from groovy script in jenkins multibranch pipeline.
While some of the environment variables are giving correct output (for example env.JOB_NAME,env.BRANCH_NAME), others like env.CHANGE_AUTHOR_DISPLAY_NAME ,env.CHANGE_AUTHOR_EMAIL are giving null values.
Has anybody come across this issue before? What can be the problem?
I've just tested with the github org plugin which uses the multi branch plugin, created a PR and the CI job it does has those env vars present. Using a Jenkinsfile:
node {
echo "${env.getEnvironment()}"
}
In my Jenkins PR build console I see amongst others:
CHANGE_AUTHOR:rawlingsj, CHANGE_AUTHOR_DISPLAY_NAME:James Rawlings, CHANGE_AUTHOR_EMAIL:rawlingsj80#gmail.com, CHANGE_ID:1, CHANGE_TARGET:master, CHANGE_TITLE:test msg, CHANGE_URL:https://github.com/rawlingsj/multi-branch-test/pull/1
Just a wild guess.. do you have your git config user.name and git config user.email set on the commit in the PR? If so it's worth mentioning which version of the multi branch plugin you're using and upgrade to the latest if its old.
I think this is related to an existing bug where the git environment variables are always null:
https://issues.jenkins-ci.org/browse/JENKINS-36436
It looks like it was very recently fixed in this PR:
https://github.com/jenkinsci/git-plugin/pull/492

Get git branch name with gradle git plugin on jenkins slave

I'm doing sonar integration and would like to pass git branch as a parameter. It will be run on Jenkins server.
Before I was using next line of code to get current git branch:
def workingBranch = """git rev-parse --abbrev-ref HEAD""".execute().text.trim()
After I replaced it with:
grgit.branch.current.fullName
But this always gives me "HEAD". How to achieve same functionality?
I am doing something very similar. As it turns out, the Git plugin in Jenkins is tuned in several ways to minimize the git clone and checkout. There are 2 ways that I've found to deal with this.
The Easy Way
Use Jenkins' built-in environment variables, as you already suggested in the comments.
def workingBranch = System.getenv("GIT_BRANCH") ?: grgit.branch.current.fullName
The Job Configuration Way
You can also set up the job to checkout the branch as a local branch, rather than a detached HEAD. This is under "Additional Behaviors", named "Check out to specific local branch". There are many other questions that detail the settings for that, and/or the Declarative Pipeline approach, depending on what your needs.
Declarative Pipeline custom checkout
Configuring local branch option

What is the branch name variable for Jenkins multibranch pipelines?

I need to know which branch is being built in my Jenkins multibranch pipeline in order for it to run steps correctly.
We are using a gitflow pattern with dev, release, and master branches that all are used to create artifacts. The dev branch auto deploys, the other two do not. Also there are feature, bugfix and hotfix branches. These branches should be built, but not produce an artifact. They should just be used to inform the developer if there is a problem with their code.
In a standard build, I have access to the $GIT_BRANCH variable to know which branch is being built, but that variable isn't set in my multibranch pipeline. I have tried env.GIT_BRANCH too, and I tried to pass $GIT_BRANCH as a parameter to the build. Nothing seems to work. I assumed that since the build knows about the branch being built (I can see the branch name at the top of the console output) that there is something that I can use - I just can't find any reference to it.
The env.BRANCH_NAME variable contains the branch name.
As of Pipeline Groovy Plugin 2.18, you can also just use BRANCH_NAME
(env isn't required but still accepted.)
There is not a dedicated variable for this purpose yet (JENKINS-30252). In the meantime you can take advantage of the fact that the subproject name is taken from the branch name, and use
env.JOB_NAME.replaceFirst('.+/', '')
This has now been resolved, see Krzysztof Krasoń's answer.
There are 2 branches to consider in a Jenkins multibranch pipeline job:
The Jenkins job branch - env.BRANCH_NAME. This may have the same name as a git branch, but might also be called PR-123 or similar
The git branch - env.GIT_BRANCH. This is the actual branch name in git.
So a job might have BRANCH_NAME=PR-123 and GIT_BRANCH=my-scm-branch-name
Jenkins documentation has a list of all the env variable for your perusal here
Another way is using the git command to obtain the branch name on the current jenkins pipeline. For example, you can add the following snippet to print the branch name in your Jenkinsfile.
...
script {
def BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
echo ${BRANCH}
}
...
I found this stackoverflow post example useful: Git Variables in Jenkins Workflow plugin
sh '//...
git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
git_branch = readFile('GIT_BRANCH').trim()
echo git_branch
//...
'

Resources