在插件的 jenkins 管道内动态创建列表

发布于 2025-01-16 02:42:45 字数 2780 浏览 2 评论 0原文

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?)语法,但在我看来,它是 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}"
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-23 02:42:45

我想在 @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}"
            }
        }
    }
}

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 my 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}"
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文