Jenkins Environment variables giving null values as output - jenkins

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

Related

How to checkout a spezific Changeset in a Jenkins Pipeline build using using the buildin cm command

I'm currently having a hard time figuring out how to check out a specific changeset using the build in cm command in a Jenkins Pipeline build. It seems that the changeset = 1234 parameter is ignored.
I tried the statement:
cm repository: item['Repo'], changeset: Stable_CS, server: item['server'], useUpdate: false, workspaceName: item['Repo']
Stable_CS is a variable that is filled with another cm command using the bat pipeline statement. It contains a string like 1234
Thanks very much in advance
I'm afraid that Plastic SCM plugin for Jenkins doesn't currently support checking out specific changesets, neither in freestyle projects nor pipelines. It can only target branches, as seen in the Plugin wiki page.

get git commit SHA within Jenkins pipeline with JGit

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 ... :-) )

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

How can I access the last built revision according to jenkins?

Near the top of my build console I see a "Last Built Revision:" w/ a revision #. How can I access this last built rev # in my build script? I'm using Gradle, but I don't think that matters here. Does Jenkins provide the last built rev # in a system property? Surely this must be trivial to access from my build script...
You can directly access the Jenkins BUILD_NUMBER as system environment variable.
task getBuildNumber << {
ext.env = System.getenv()
ext.buildNumber = env.BUILD_NUMBER?.toInteger()
println "Build Number: $buildNumber"
}
Turns out, the Git plugin DOES export the last build revision as an environment variable. So instead of using the accepted answer:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
you can just use this instead:
GIT_PREVIOUS_COMMIT
One failproof way to see exactly what's available to your build script is to choose Add Build Step > Execute Shell then simply add the following:
export
view your console (for the build) and you should see lots of great environment variables available to you. The git-related variables that were available to me (using the git plugin) were:
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_BRANCH
GIT_COMMIT
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_PREVIOUS_COMMIT
GIT_URL
Lastly, to see a less comprehensive list of available environment variables, you can also just go to this url: http://[your-jenkins-domain-and-port]/env-vars.html
I do not think the git plugin exports the last built revision as an environment variable, but the information is easily available using a simple shell command like:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
BUILD_URL always points to the build's own page and the rest of the information seems to be available using the xml api.
The current build-number is provided as the Jenkins-variable BUILD_NUMBER
In Unix it is set for you as ${BUILD_NUMBER}
In Windows it is available as %BUILD_NUMBER%
The complete list of variables is available on your Jenkins server, at: http://[your-jenkins-server]/env-vars.html
The Git plugin returns information from the checkout() command. In Pipeline script, this can be used to get the previous commit:
def scmVars = checkout scm
scmVars.GIT_PREVIOUS_COMMIT

Resources