Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

Jenkins Declarative Pipeline Include File

I am trying to a separate file holding variable for a Jenkins pipeline, this is because it will be used by multiple pipelines. But I can't seem to find the proper way to include it? Or if there's any way to include it?

MapA:

def MapA = [
    ItemA: [
        Environment: 'envA',
        Name: 'ItemA',
        Version: '1.0.0.2',
    ],
    ItemB: [
        Environment: 'envB',
        Name: 'ItemB',
        Version: '2.0.0.1',
    ]
]

return this;

MainScript:

def NodeLabel = 'windows'
def CustomWorkSpace = "C:/Workspace"

// Tried loading it here (Location 1)
load 'MapA'

pipeline {
    agent {
        node {
            // Restrict Project Execution
            label NodeLabel
            // Use Custom Workspace
            customWorkspace CustomWorkSpace

            // Tried loading it here (Location 2)
            load 'MapA'
        }
    }

    stages {
        // Solution
        stage('Solution') {
            steps {
                script {
                    // Using it here
                    MapA.each { Solution ->
                        stage("Stage A") {
                            ...
                        }

                        stage("Stage B") {
                            ...
                        }

                        // Extract Commit Solution
                        stage("Stage C") {
                            ...
                            echo "${Solution.value.Environment}"
                            echo "${Solution.value.Name}"
                            echo "${Solution.value.Version}"
                        }
                    }
                }
            }
        }
    }
}
  • On Location 1 outside the pipeline and node section: it gave the below error
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
  • On Location 2 inside the node section: it gave the below error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 7: Expected to find ‘someKey "someValue"’ @ line 7, column 14.
    load 'MapA'
    node {
         ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...