在插件的jenkins管道中动态创建一个List



Jenkins管道中是否有方法生成/修改一个"列表";给了一个插件?

例如,我正在使用插件nexus工件上传器。有时我想上传n配置文件,其他时间上传n-1文件。取决于用户设置的参数。

steps {
nexusArtifactUploader artifacts: [
[artifactId: "${projectName}", classifier: 'swagger', file: "swagger.json", type: 'json'],
[artifactId: "${projectName}", classifier: 'k8s-dtr-secret', file: "k8s-dtr-secret.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-app-secret', file: "k8s-app-secret.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-deployment', file: "k8s-deployment.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-autoscaler', file: "k8s-autoscaler.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-service', file: "k8s-service.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-ingress', file: "k8s-ingress.yml", type: 'yml']
],
credentialsId: 'my-credentialsId',
groupId: 'my-groupId',
nexusUrl: 'my-nexusUrl',
nexusVersion: 'nexus2',
protocol: 'http',
repository: 'my-repository',
version: "${buildTag}"
}

假设我不想上传k8s-ingress,因为我希望我的部署是本地的,而不是在k8s中的命名空间之外访问。我不熟悉(groovy?(语法,但在我看来,它是maplistlist?是否可以使用for循环动态创建该列表,或者根据其中的值弹出一个项目?

steps {
def my_artifacts = [
[artifactId: "${projectName}", classifier: 'swagger', file: "swagger.json", type: 'json'],
[artifactId: "${projectName}", classifier: 'k8s-dtr-secret', file: "k8s-dtr-secret.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-app-secret', file: "k8s-app-secret.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-deployment', file: "k8s-deployment.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-autoscaler', file: "k8s-autoscaler.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-service', file: "k8s-service.yml", type: 'yml'],
[artifactId: "${projectName}", classifier: 'k8s-ingress', file: "k8s-ingress.yml", type: 'yml']
]
if (params.no_istio == True){
my_artifacts.remove() # Someway to find in my list of list of maps my value for key `file` == "k8s-ingress.yml"
}
nexusArtifactUploader artifacts: my_artifacts,
credentialsId: 'my-credentialsId',
groupId: 'my-groupId',
nexusUrl: 'my-nexusUrl',
nexusVersion: 'nexus2',
protocol: 'http',
repository: 'my-repository',
version: "${buildTag}"
}

我想在@NoamHelmer的评论中添加一些内容

我不能在步骤中声明my_artifacts,我必须在pipeline之外声明和修改它。

def artifactsList = [
[artifactId: "${params.PROJECT_NAME}", classifier: 'swagger', file: "swagger.json", type: 'json'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-dtr-secret', file: "k8s-dtr-secret.yml", type: 'yml'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-app-secret', file: "k8s-app-secret.yml", type: 'yml'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-deployment', file: "k8s-deployment.yml", type: 'yml'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-autoscaler', file: "k8s-autoscaler.yml", type: 'yml'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-service', file: "k8s-service.yml", type: 'yml'],
[artifactId: "${params.PROJECT_NAME}", classifier: 'k8s-ingress', file: "k8s-ingress.yml", type: 'yml']
]
if (!params.EXPOSE_API_OUTSIDE_K8S) {
artifactsList.removeAll { it.classifier in (['k8s-ingress'])}
}
pipeline {
stages {
stage("My step") {
steps {
nexusArtifactUploader artifacts: my_artifacts,
credentialsId: 'my-credentialsId',
groupId: 'my-groupId',
nexusUrl: 'my-nexusUrl',
nexusVersion: 'nexus2',
protocol: 'http',
repository: 'my-repository',
version: "${buildTag}"
}
}
}
}

最新更新