How can I reference the Jenkinsfile directory, with Pipeline?

前端 未结 6 1180
你的背包
你的背包 2021-02-03 18:50

I have a groovy file, I want to run from the Jenkinsfile.

ie. load script.groovy

However, I am not sure how I can reference this file if it is store

相关标签:
6条回答
  • 2021-02-03 19:04

    if this script.groovy file is in the root of your project, like the Jenkinsfile, it will be fetched from git into the same folder as your Jenkinsfile. So the command you are using should work OK.

    Are you getting some error? Please provide more details if so.

    EDIT: now I can see what's in your Jenkinsfile, I can see you are checking out a git project called integration_bus which is where the groovy script resides. You can specify the location where that is checked out like this:

    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'esb_deploy']], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://git@giturl/integration_bus.git']]])
    

    as opposed to what you have

    git branch: branch, url: 'ssh://git@giturl/integration_bus.git'
    

    Then you should be able to reference the groovy script in the esb_deploy folder like this

    load 'esb_deploy/esb_deploybar_pipeline/deploy_esb.groovy'
    
    0 讨论(0)
  • 2021-02-03 19:12

    There's one scenario that I haven't seen anyone mention. Which is how to load the Groovy scripts when the job is supposed to run on a Jenkins agent/slave, rather than on the master.

    Since the master is the one that checks out the Jenkins pipeline project from SCM, the Groovy scripts are only found in the master's file system. So while this will work:

    node {       
        def workspace = pwd() 
        def Bar = load "${workspace}@script/Bar.groovy"
        Bar.doSomething()
    }
    

    It's only a happy coincidence because the node that clones the pipeline from SCM is the same one that attempts to load the groovy scripts in it. However, just adding the name of a different agent to execute on:

    node("agent1"){
        def workspace = pwd() 
        def Bar = load "${workspace}@script/Bar.groovy"
        Bar.doSomething()
    }
    

    Will fail, resulting in:

    java.io.IOException: java.io.FileNotFoundException: /Jenkins/workspace/Foo_Job@script/Bar.groovy (No such file or directory)
    

    This is because this path:

    /Jenkins/workspace/Foo_Job@script/
    

    Only exists on the master Jenkins box. Not in the box running agent1.

    So if you're facing this issue, make sure to load the groovy scripts from the master into globally declared variables, so the agent can then use them:

    def Bar
    node {       
        def workspace = pwd() 
        if(isUnix()){
            Bar = load "${workspace}@script/Bar.groovy"
        }
        else{
            Bar = load("..\\workspace@script\\Bar.groovy")
        }
    }
    node("agent1"){
        Bar.doSomething()
    }
    

    Note: The variable used to pass the module between nodes must be declared outside the node blocks.

    0 讨论(0)
  • 2021-02-03 19:13

    This should work

    load "${WORKSPACE}/../${JOB_NAME}@script/esb_deploy/esb_deploybar_pipeline/deploy_esb.groovy"
    
    0 讨论(0)
  • 2021-02-03 19:22

    You can just assume that all file operations in the Jenkinsfile are relative to the current workspace (which is the default workspace when using load inside a node).

    So if the target file (say deploy_esb.groovy) is inside the folder foo in your SCM, this should work without additional configuration:

    git branch: branch, url: 'ssh://git@giturl/integration_bus.git'
    load 'foo/deploy_esb.groovy'
    

    Or this if the file to load is in the same repository than the Jenkinsfile:

    checkout scm
    load 'foo/deploy_esb.groovy'
    
    0 讨论(0)
  • 2021-02-03 19:25

    If deploy_esb.groovy file is stored in the same SCM as the Jenkinsfile you can do:

    node {       
       def workspace = pwd() 
       load "${workspace}@script/esb_deploybar_pipeline/deploy_esb.groovy"
    }
    
    0 讨论(0)
  • 2021-02-03 19:31

    Had the same problem. Ended up with extra clone operation to get copy of script repo under workspace dir so I can reliably access groovy files in it:

    dir ('SCRIPTS_DIR') {
        checkout scm
        commonScripts = load 'subdir/Common.groovy' // no def, so script is global
    }
    
    0 讨论(0)
提交回复
热议问题