Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load another groovy script in the same Jenkins node?

When loading a pipeline script from another pipeline script, the two pipelines don't get executed on the same node : the first one is executed on my master node and the second gets executed on a slave node.

I'm using Jenkins pipelines with Pipeline Script from SCM option for a lot of jobs in this way :

  • Each of my jobs defines their corresponding Git repo URL with Poll SCM option so that the repository gets automatically polled when a change is made to my code (basic job usage).

  • Each of my jobs define a simple Jenkinsfile at the root of their repository, and the pipeline script inside does basically nothing but loading a more generic pipeline.

E.g. :

node {
    // --- Load the generic pipeline ---
    checkout scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://github/owner/pipeline-repo.git']]]
    load 'common-pipeline.groovy'
}()

My common-pipeline.groovy pipeline does the actual stuff such as building, releasing or deploying artifacts, e.g. :

{ ->
    node() {
        def functions = load 'common/functions.groovy'

        functions.build()
        functions.release()
        functions.deploy()
    }
}

Now I don't want to force the node for each job so both pipelines have node("master") or node("remote") because I really don't want to be handling that manually, however I'd like that once the first pipeline runs on a specific node (either master, slave1, slave2, slave3) the second/loaded pipeline gets executed on the same node, because otherwise my actual Git repository code is not available from other node's workspace...

Is there any way I can specify that I want my second pipeline to be executed on the same node as the first, or maybe pass an argument when using the load step ?

like image 845
Pom12 Avatar asked Aug 10 '16 14:08

Pom12


People also ask

How do I load Groovy script in Jenkins pipeline?

In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.

How do I run a Jenkins job on a specific node?

Install Node and Label parameter plugin. In test_job's configuration, select 'This build is parameterized' and add a Node parameter and set the parameter name to node_name. In pipeline add Choice parameter, name it node , put there possible choices, so running pipeline you can choose on which node it should be run.


1 Answers

How about stashing the workspace after the checkout and before the script load?:

e.g.

stash includes: '**', name: "source"

and then unstash it in another node(){} section:

e.g. unstash "source"

That way it will be available in the other node

Don't forget to clean up the workspace though

Or how about creating a common function that contains the logic for the checkout too (maybe passing in branches as a param). Then you can discard the node(){} in the Jenkins file and just use node(){} entries in your shared groovy script?

e.g. Jenkinsfile

load 'common-pipeline.groovy'
createWorlflow("*/master")

common-pipeline.groovy:

def createWorkflow(branches){
node() {
        def functions = load 'common/functions.groovy'
        functions.checkout(branches)
        functions.build()
        functions.release()
        functions.deploy()
    }
}
like image 163
GCDev Avatar answered Nov 11 '22 07:11

GCDev