Multibranch Pipeline / Git question -- Use same information as used to retrieve Jenkinsfile

732 views
Skip to first unread message

Greg Smith

unread,
May 31, 2016, 11:00:50 AM5/31/16
to Jenkins Users

Hello,

I have a question that is probably obvious, but I couldn't seem to find it anywhere.  Apologies for my newbie question:

If I have a multibranch Pipeline job, does my Jenkinsfile need to do the git extract?  Or should that be assumed as part of the retrieval of the Jenkinsfile?

For example, lets say I have project "MyProject", with branch "master", with a Jenkins file like this:

node() {
   stage 'Checkout'
   // git -- Do I need to do the git checkout here??
   git credentialsId: '1ab8712b-28f4-498e-af0f-433b409675ea', url: 'g...@github.com:smithgcovert/MyProject.git'

  // build stuff
}

Now I branch master to "newtestbranch" -- it has the same Jenkinsfile at the top.

1)  Do I even need the git extract step?  It would seem to be implied by the fact that the Jenkins file was found in that branch... but it seems i do from the testing I've done
2)  How can I pass the url and branch into the git step, so that I do not put a path to the repo in the file that is checked in? 

IE, I want the repo URL, and the branch being built, and the credentials used to do the extract to all be defined by the same url, branch, creds that were used to retrieve the Jenkinsfile.  It seems wrong to have any url, branch or credential information in the Jenkinsfile -- is there a way to say "extract the same as used to retrieve the Jenkinsfile"?

Thanks,
Greg

Antonio Muñiz

unread,
May 31, 2016, 11:54:25 AM5/31/16
to jenkins...@googlegroups.com
You can use `checkout scm` which will clone the repository at the same revision than the previously fetched Jenkinsfile.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/549e1e95-d6f2-4c40-83b4-b731fdbf2ba6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Antonio Muñiz
Software Engineer
CloudBees, Inc.

Greg Smith

unread,
May 31, 2016, 12:03:25 PM5/31/16
to Jenkins Users

Thank you -- This is exactly what I was looking for.  Now that I have it, and was able to search the docs for it, I don't know how I missed it....

Thanks,
Greg



On Tuesday, May 31, 2016 at 11:54:25 AM UTC-4, Antonio Muñiz wrote:
You can use `checkout scm` which will clone the repository at the same revision than the previously fetched Jenkinsfile.

KP

unread,
Jul 1, 2016, 11:08:11 AM7/1/16
to Jenkins Users
I am also interested in this, but for a slightly different reason. I want to checkout into a sub-directory using git. I know that I can do that by using syntax like: 

checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'SUBDIR']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'XXXX', url: 'MYURL']]])

but I need to supply the credentials and url (which I don't want to do in a committed Jenkinsfile). Can I use the same credentials/url as would be used in "checkout scm" somehow? Or extend "checkout scm" so that I can use a subdirectory? Or do I need to set the parameters in the job and use those instead?

Thanks.

On Tuesday, May 31, 2016 at 11:54:25 AM UTC-4, Antonio Muñiz wrote:

Philipp Meier

unread,
Jul 1, 2016, 12:43:50 PM7/1/16
to Jenkins Users
You can get the values from the global SCM variable i.e.:

checkout([
       
$class                           : 'GitSCM',
       
branches                         : [[$class: 'BranchSpec', name: git_reference]],
       
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
       
extensions                       : scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: true]],
       
submoduleCfg                     : [],
       
userRemoteConfigs                : scm.userRemoteConfigs
])

Antonio Muñiz

unread,
Jul 1, 2016, 1:16:48 PM7/1/16
to jenkins...@googlegroups.com

On Fri, Jul 1, 2016 at 5:08 PM, KP <kie...@pineapplecat.net> wrote:
Or extend "checkout scm" so that I can use a subdirectory?

Try this:

dir('a_subdirectoty') {
    checkout scm

KP

unread,
Jul 1, 2016, 2:24:45 PM7/1/16
to Jenkins Users
Great. Thank you and Philipp for your fast and helpful replies.
Reply all
Reply to author
Forward
0 new messages