Frerich Raabe
Frerich Raabe

Reputation: 94279

Does the 'scm' object in Jenkins pipeline scripts specify a single commit to check out, or a branch?

I'm working on a Jenkins pipeline which builds a project in parallel, on multiple nodes. On each node, source code needs to be checked out from a Git repository. When doing this via

checkout scm

as mentioned in the SCM Step documentation, is it safe to assume that every node will check out the exact same commit (i.e. is the commit to check out part of the scm object) or is it possible that - in case a commit is done while the build is running - some nodes will build a different state of the code than others (in case the scm object merely specifies the repository & branch to check out)?

I tried to see what information is available in the scm object, but it appears I cannot use the dump() method on it - the Jenkins pipeline plugin rejects this with the error message

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods dump java.lang.Object

Another attempt using scm.properties.collect{it}.join('\n') failed as well, generating a similar error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getProperties java.lang.Object

I also had a quick look at the GitSCM.java source code and it appears that the plugin supports checking out a specific commit - it's just not clear whether the scm object provides it.

Upvotes: 2

Views: 2908

Answers (1)

Siddhant Mishra
Siddhant Mishra

Reputation: 508

If I am getting this right , what you can do is specify the git commit id for all the checkouts in the branch field like:

checkout([
    $class: 'GitSCM',
    userRemoteConfigs: [[url: 'path.to.your.git']],
    branches: [[name: '5062ac843f2b947733e6a3b105977056821bd352']],
    ...
])

where 5062ac843f2b947733e6a3b105977056821bd352 is the commit id of the commit you want to checkout on all nodes.

If you don't know the commit id. You can first checkout(a shallow checkout --- just the last commit) the master (or any other branch) and get the commit id ,store it in a variable and use it for all nodes.

E.g.

def commitId

.
.
.
get the commit id
.
.
.

node('first')
{
     checkout([$class: 'GitSCM', **branches: [[name: 
     **"${commitId}"**]]**, 
     doGenerateSubmoduleConfigurations: false, extensions: [], 
     gitTool: 'Default', submoduleCfg: [], userRemoteConfigs: [[url: 
     'path.to.your.git']]])
}

Upvotes: 1

Related Questions