在插件的 jenkins 管道内动态创建列表
Jenkins 管道中有没有办法生成/修改给插件的“列表”?
例如,我正在使用插件 nexus-artifact-uploader。 有时我想上传 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?)语法,但在我看来,它是 map
的 list
的 list
?例如,是否可以使用 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}"
}
Is there a way in Jenkins pipeline to generate/modify a "list" given to a plugin?
For example, I'm working with the plugin nexus-artifact-uploader.
Sometimes I want to upload n
configuration files, the other time n-1
files. Depending of a parameter set by the user.
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}"
}
Let's say I don't want tu upload the k8s-ingress
, because I want my deployement to be local and not accessed outside my namespace in k8s. I'm not famillar with the (groovy?) syntax, but seems to me that it's a list
of list
of map
? Is it possible to dynamically create that list with a for loop for example, or pop an item depending of a value inside?
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}"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想在 @NoamHelmer 的评论中添加一些内容
,我无法在我的步骤中声明
my_artifacts
,我必须在我的pipeline
之外声明并修改它。I want to add something to @NoamHelmer 's comment
I could not declare
my_artifacts
inside my steps, I had to declare and modify it outside mypipeline
.