我有一个带有声明管道的jenkinsfile。我试图从config.yml中检索ECR图像名,将它们放在地图中,并在Jenkins中用循环打印它们。我到目前为止所做的事情如下:
config.yml:
images:
- "123.dkr.ecr.eu-west-1.amazonaws.com/image-1:latest"
- "123.dkr.ecr.eu-west-1.amazonaws.com/image-2:latest"
- "456.dkr.ecr.eu-west-1.amazonaws.com/image-9:latest"jenkinsfile:
def account_info = readYaml file: "config.yml"
def image_info = "${account_info.images}"
def map = [
("image") : "${image_info}"
]
pipeline {
stages {
stage('tests') {
steps {
loopImages(map)
}
}
}
}
def loopImages(map){
map.each { entry ->
stage('test-' + entry.key) {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
script {
sh """
echo "${image_info}"
"""
}
}
}
}
}想知道你是否建议我用不同的方式来做这件事?我对Jenkins并不熟悉,也不知道如何从yaml中获取变量并将它们放到地图中。
这样做会给我带来以下错误:
Required context class hudson.FilePath is missing我的目标是有一张这样的地图:
def map = [
("image") : "123.dkr.ecr.eu-west-1.amazonaws.com/image-1:latest"
("image") : "123.dkr.ecr.eu-west-1.amazonaws.com/image-2:latest"
("image") : "456.dkr.ecr.eu-west-1.amazonaws.com/image-9:latest"
]发布于 2020-04-13 16:34:43
这里的错误是您试图在readYaml块之外使用一个步骤( pipeline )。这不起作用,因为步骤需要来自管道的上下文,例如当前节点。
实际上,您应该将pipeline视为您的主要函数,除了简单的变量初始化之外,不要在它之外执行太多操作。
对于我必须通过步骤初始化的事情,我通常创建一个“初始化”阶段。这将保持事物的分离,并遵循单一层次的抽象原则。
def account_info = null
def image_info = null
def map = null
pipeline {
stages {
stage('initialize') {
steps {
initialize()
}
}
stage('tests') {
steps {
loopImages(map)
}
}
}
}
void initialize() {
account_info = readYaml file: "config.yml"
//... and so on
}https://stackoverflow.com/questions/61190072
复制相似问题