I cannot seem to extract $GIT_COMMIT and $BRANCH_NAME from a Jenkins Workflow Checkout step.
I would like to be able to send this information through to my Gradle scripts in order to pass it onto external sources such as Static analysis etc.
Currently I try to run this:
checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[credentialsId: '2b74a351-67d5-4d00-abd3-49842a984201', url: 'ssh://git#corporate.com:repo.git']]])
And I would like to achieve the following or something similar:
// Specified variables that can be reused
def branch = ${BRANCH_NAME}
def commit = ${GIT_COMMIT}
Or maybe this would work too:
print "BRANCH: ${BRANCH_NAME}, COMMIT: ${GIT_COMMIT}"
// or the following
print "BRANCH: ${env.BRANCH_NAME}, COMMIT: ${env.GIT_COMMIT}"
I did find the following issue which seems to be resolved but it doesn't work in version 1.15:
https://issues.jenkins-ci.org/browse/JENKINS-30252
Anyone have any ideas how to workaround this or if there's a variable I cannot find?
First of all,
def branch = ${BRANCH_NAME}
is not valid Groovy, or at least not doing what you think. Perhaps you meant
def branch = "${BRANCH_NAME}"
which would just be a silly way of writing
def branch = BRANCH_NAME
Anyway environment variables are not currently accessible directly as Groovy variables in Pipeline (there is a proposal to allow it); you need to use the env global variable:
def branch = env.BRANCH_NAME
From within an external process, such as a sh step, it is an actual environment variable, so
sh 'echo $BRANCH_NAME'
works (note that ' means Groovy is not interpolating the variable).
Now, JENKINS-30252 was referring to multibranch projects. If you created a standalone Pipeline job, this variable will not be set.
Anyway in your case your checkout step is always checking out the master branch. If you actually have a multibranch project, then your Jenkinsfile should be using
checkout scm
which will check out a commit on the correct branch (always matching the revision of Jenkinsfile itself).
As to the commit hash, pending JENKINS-26100 this is not available automatically, but you can use something like
sh 'git rev-parse HEAD > commit'
def commit = readFile('commit').trim()
to access it.
I have two Jenkins instances.
In both instances, GIT_COMMIT and BRANCH_NAME environment variables are not defined.
When I try to get them from the return value of checkout() call, each instance behaves differently.
Jenkins Instance 1
Jenkins version: 2.46.1
"Pipeline: SCM Step" plugin version: 2.5
Trying to access the environment variable as explained in the checkout documentation fails.
def scmVars = checkout([$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[credentialsId: '2b74a351-67d5-4d00-abd3-
49842a984201', url: 'ssh://git#corporate.com:repo.git']]])
def commitHash = scmVars.GIT_COMMIT
scmVars returns NULL, and accessing scmVars.GIT_BRANCH fails with exception java.lang.NullPointerException: Cannot get property 'GIT_BRANCH' on null object.
So I had to do the following in order to get the branch:
sh 'git name-rev --name-only HEAD > GIT_BRANCH'
sh 'cat GIT_BRANCH'
git_branch = readFile('GIT_BRANCH').trim()
env.GIT_BRANCH = git_branch
Jenkins Instance 2
Jenkins version: 2.60.2
"Pipeline: SCM Step" plugin version: 2.6
In this instance, I could do the following with success:
def scmVars = checkout([$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[credentialsId: '2b74a351-67d5-4d00-abd3-
49842a984201', url: 'ssh://git#corporate.com:repo.git']]])
env.GIT_COMMIT = scmVars.GIT_COMMIT
env.GIT_BRANCH = scmVars.GIT_BRANCH
So please check which approach works for your Jenkins instance and use it.
If you want to access the BRANCH_NAME from Jenkins environment variable as a shell script, use the below snippet.
sh 'echo Branch Name: $BRANCH_NAME'
The response should be as below:
Branch Name: the_checkedout_branch
Related
I'm pretty new to jenkins and groovy and I'm trying to do a sparse checkout in my jenkins file. Currently I simply do this:
stage('Check out branch from Gitlab'){
echo 'Pulling...' + env.BRANCH_NAME
checkout scm
}
I wish to execute a sparse checkout from a Jenkins Groovy script and I'm struggling to find a good way of doing this. Is there a way of using the "checkout" command to do this?
You should configure a set of parameters for the GitSCM more info here
A basic configuration is presented as an example below:
pipeline {
agent any
stages {
stage ("Git Checkout"){
steps {
script {
checkout([
$class: 'GitSCM',
branches: [[name: "devel"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: "/tmp/jenkins/devel"
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'jenkinsCredentialsId',
url: 'https://git.example.com/git/example'
]]
])
}
}
}
}
}
I attached a fully working Jenkins pipeline of one stage. It checks out the branch devel of the repository https://git.example.com/git/example on directory /tmp/jenkins/devel. Also please note that you should add (if not already done) the credentials of the repository in Jenkins Credentials (/jenkins/credentials/), in the above example is under id jenkinsCredentialsId
You can read the link for GitSCM to find out more details and attributes that you can configure.
I have defined a simple Jenkins pipeline using the Groovy DSL. My intention is that it will simply checkout my git repository. However I am getting an error.
Given my groovy DSL defintion:
stage 'build'
node
{
git branch: '*/mybranch', credentialsId: 'my credentials', url: 'git#git.servername:pathto/myrepo.git'
}
I'm expecting the Jenkins pipeline to simply checkout my git repository.
However I'm getting the following error:
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE
Can anyone help me in resolving this issue please?
The more verbose version works:
checkout([$class: 'GitSCM', branches: [[name: '*/mybranch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'myCredentials', url: 'git#git.servername:pathto/myrepo.git']]])
However it would be nice to use the short-hand version.
Without the */ on the branch specifier works for me.
Try this:
git branch: 'mybranch', credentialsId: 'my credentials', url: 'git#git.servername:pathto/myrepo.git'
I created a pipeline job and would like to get the svn version number to enable further downstream processing in a call to a shell script. I am using a pipeline script similar to the following:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a SVM repository
checkout(
[
$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [
[
...
]
],
workspaceUpdater: [$class: 'UpdateUpdater']
]
)
def svnversionnumber=${SVN_VERSION}
sh "/.../someshellscript ${svnversionnumber};"
}
Is there documentation on the checkout function available? Is it possible to get hold of the svn revision number? I can see that the revision is output to the log.
I had the same issue, but you can solve it by using the map that is returned from calling SCM checkout. It contains a value for SVN_REVISION.
// Get some code from a SVM repository
def scmVars = checkout(
...
)
def svnversionnumber = scmVars.SVN_REVISION
In Groovy pipeline script it's possible to get results of checkout scm command into TreeMap variable and then get what you need:
def checkoutResults = checkout([
poll: false,
scm: [
$class: 'SubversionSCM',
...
]
])
echo 'checkout results' + checkoutResults.toString()
echo 'checkout revision' + checkoutResults['SVN_REVISION']
echo 'checkout revision' + checkoutResults['SVN_REVISION_1']
echo 'checkout revision' + checkoutResults['SVN_REVISION_2']
I ended up invoking a shell to get the svn revision number as follows
def svnVersionNumber = sh(
script: "svn info --show-item last-changed-revision $url",
returnStdout: true
)
This was the only way I could get it to work correctly.
this code work for me in jenkins pipeline:
String url = 'svn+ssh:...'
SVN_REVISION_IN = sh returnStdout: true, script: 'svn info --show-item last-changed-revision ' + url
currentBuild.displayName = "Rev: ${SVN_REVISION_IN}"
There is a file called revision.txt in the build dir. The SubversionSCM provides methods to read this file.
//Here remote returns url#revision but the revision part is across the entire repo
//We will use the url part to get the revision for our branch
def remote = scm.locations.first().remote
def url = remote.split('#').first()
//The revision file has the revision for our branch. Parse returns a map.
def revmap = scm.parseRevisionFile(currentBuild.rawBuild)
revmap[url]
The scm variable is available on Jenkinsfiles. If you are not using a Jenkinsfile you should be able to create the scm object and pass it into the checkout method.
I think one of the best choice can be use a simple little "groovy console script" to get the revision number then put into a Jenkins variable..
Something like this to give you an idea:
Link
Take also a look at this question: Link
I'm exploring Jenkins 2.0 pipelines. So far my file is pretty simple.
node {
stage "checkout"
git([url:"https://github.com/luxengine/math.git"])
stage "build"
echo "Building from pipeline"
}
I can't seem to find any way to set the directory that git will checkout to. I also can't find any kind of documentation related to that. I found https://jenkinsci.github.io/job-dsl-plugin/ but it doesn't seem to match what I see on other tutorials.
Clarification
Looks like you are trying to configure Pipeline job (formerly known as Workflow). This type of job is very distinct from Job DSL.
The purpose of Pipeline job is to:
Orchestrates long-running activities that can span multiple build slaves. Suitable for building pipelines (formerly known as workflows) and/or organizing complex activities that do not easily fit in free-style job type.
Where as Job DSL:
...allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.
Solution
If you want to checkout your code to specific directory then replace git step with more general SCM checkout step.
Final Pipeline configuration should look like that:
node {
stage "checkout"
//git([url:"https://github.com/luxengine/math.git"])
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'checkout-directory']],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://github.com/luxengine/math.git']]])
stage "build"
echo "Building from pipeline"
}
As a future reference for Jenkins 2.0 and Pipeline DSL please use built-in Snippet Generator or documentation.
This can be done by using the directive of dir:
def exists = fileExists '<your target dir>'
if (!exists){
new File('<your target dir>').mkdir()
}
dir ('<your target dir>') {
git url: '<your git repo address>'
}
First make clear that you are using Jenkins Job DSL.
You can do this like this:
scm {
git {
wipeOutWorkspace(true)
shallowClone(true);
remote {
url("xxxx....")
relativeTargetDir('checkout-folder')
}
}
}
https://jenkinsci.github.io/job-dsl-plugin/
This above address gives you the chance simply to type in upper left aread for example 'scm' and than it will show in which relationships 'scm' can be used. Than you can select 'scm-freestylejob' and afterwards click on the '***' than you can see the details.
The general start point for Jenkins Job DSL is here:
https://github.com/jenkinsci/job-dsl-plugin/wiki
You can of course ask here on SO or on Google Forum:
https://groups.google.com/forum/#!forum/job-dsl-plugin
pipeline {
agent any
stages{
stage("Checkout") {
steps {
dir('def exists = fileNotExists \'git\'') {
bat label: '', script: 'sh "mkdir.sh'
}
dir ('cm') {
git branch: 'dev',
credentialsId: '<your credential id>',
url: '<yours git url>'
}
}
} //End of Checkout stage
stage("TestShellScript") {
steps {
bat label: '', script: 'sh "PrintNumber.sh"'
}
}
}//End of stages
} // End of pipeline
Note: cat mkdir.sh
#!/bin/bash
#Create a directory
mkdir git
You are using the Pipeline Plugin, not the Job DSL Plugin. In the Pipeline Plugin, if you want to define something, where there is not yet a function available in the Pipeline syntax, you can define it yourself.
How can I update a jira issue from within a Jenkinsfile (jenkins-worflow/pipeline)?
Is there a way I could use the Jira Issue Updater plugin as a step in the Jenkinsfile?
I know I could use the Jira RestAPI, but I'm trying to figure out if I can re-use the functionality provided by the jira-updater-issue.
What I'm looking for is a something similar to the example below calling Junit archiver, and atifact archiver, but calling jira updater.
node {
git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify"
step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
The Jira Plugin is compatible with Pipeline.
This should work:
step([$class: 'hudson.plugins.jira.JiraIssueUpdater',
issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'],
scm: [$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://github.com/jglick/simple-maven-project-with-tests.git']]]])
You can get a full reference in the built-in Pipeline Snippet Generator.
The JIRA Steps Plugin provides a more declarative way to update an existing Jira Ticket:
node {
stage('JIRA') {
# Look at IssueInput class for more information.
def testIssue = [fields: [ // id or key must present for project.
project: [id: '10000'],
summary: 'New JIRA Created from Jenkins.',
description: 'New JIRA Created from Jenkins.',
customfield_1000: 'customValue',
// id or name must present for issuetype.
issuetype: [id: '3']]]
response = jiraEditIssue idOrKey: 'TEST-01', issue: testIssue
echo response.successful.toString()
echo response.data.toString()
}
}
Since you would like to use the Jenkinsfile to define your pipeline, that should be the preferred way for you to go...
As this was way harder for me than it should be, here is a working example. This will update a custom field of a ticket with a specific value:
step([$class: 'IssueFieldUpdateStep',
issueSelector: [$class: 'hudson.plugins.jira.selector.ExplicitIssueSelector', issueKeys: ticket],
fieldId: field,
fieldValue: value
])
The snippet generator did not work for me. The variables ticket, field and value are all strings. Starting from this you can look for options here: https://www.jenkins.io/doc/pipeline/steps/jira/
Yes, seems like this page answers your question:
https://wiki.jenkins-ci.org/display/JENKINS/Jira+Issue+Updater+Plugin
After you install the plugin, add a build step, or pre/post build step to call this plugin
There you can give it the REST URL to your Jira server, the creds and the JQL to find the issues