Jenkins 声明式管道包含文件

时间:2021-01-19 21:51:42

标签: jenkins jenkins-pipeline

我正在尝试为 Jenkins 管道创建一个单独的文件保存变量,这是因为它将被多个管道使用。但我似乎无法找到包含它的正确方法?或者是否有任何方法可以包含它?

地图A:

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

return this;

主脚本:

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}"
                        }
                    }
                }
            }
        }
    }
}
  • 在管道和节点部分外的位置 1:它给出了以下错误
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
  • 在节点部分内的位置 2:它给出了以下错误
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 7: Expected to find ‘someKey "someValue"’ @ line 7, column 14.
    load 'MapA'
    node {
         ^

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式实现您的场景:

#1

如果你愿意,你可以在同一个 Jenkins 文件中对变量进行硬编码,并在你的管道中使用它,如下例:

Jenkinsfile 内容

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

pipeline {
    agent any;
    stages {
        stage('debug') {
            steps {
                script {
                    MapA.each { k, v ->
                        stage(k) {
                            v.each { k1,v1 ->
                                // do your actual task by accessing the map value like below 
                                echo "${k} , ${k1} value is : ${v1}"
                            }
                        }
                        
                    }
                }
            }
        }
    }
}

#2

如果您想将变量保存在 gitrepo 中的单独 groovy 文件中,则如下所示

Git Repo 文件和文件夹结构

.
├── Jenkinsfile
├── README.md
└── var.groovy

var.groovy

def mapA() {
    return [
        ItemA: [
            Environment: 'envA',
            Name: 'ItemA',
            Version: '1.0.0.2',
        ],
        ItemB: [
            Environment: 'envB',
            Name: 'ItemB',
            Version: '2.0.0.1',
        ]
    ]
} 

def helloWorld(){
    println "Hello World!"
}
return this;

詹金斯档案

pipeline {
  agent any 
  stages {
     stage("iterate") {
         steps {
           sh """
            ls -al
           """
           script {
             def x = load "${env.WORKSPACE}/var.groovy"
             x.helloWorld()
             
             x.mapA().each { k, v ->
                stage(k) {
                  v.each { k1,v1 ->
                    echo "for ${k} value of ${k1} is ${v1}"
                  }
                } //stage
             } //each
           } //script
         } //steps
     } // stage 
  }
}