是否可以从管道步骤动态设置 Jenkins 作业参数? [英] Is it possible to set Jenkins job parameters dynamically from a pipeline step?

查看:20
本文介绍了是否可以从管道步骤动态设置 Jenkins 作业参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(简化的)Jenkins 管道代码.

I have the following (simplified) Jenkins pipeline code.

jobParams.groovy

jobParams.groovy

List get(Object paramVars = {}) {

    def params = []


    params += [
        choice(
            choices: ['branch', 'tag'],            
            name: 'RELEASE_TYPE'
        ),
        string(
            defaultValue: '',
            name: 'VERSION'
        ),
    ]

    return params
}

管道.groovy

def call() {

    properties([
        parameters(
            jobParams.get()
        )
    ])

    pipeline {
        agent { label 'deploy-slave' }

        stages {
            stage('Prepare') {
                steps {
                    script {
                        // Do some logic here and set a job parameter?
                    }
                }
            }
        }
    }
}

这很好用.当管道启动时,作业参数被设置并在下次作业运行时可用.

This works fine. When the pipeline starts the job parameters are set and available for the next time the job runs.

但是,是否也可以在流水线步骤中的某些逻辑之后动态设置作业参数?

However, is it also possible to set job parameters dynamically after some logic in a pipeline step?

推荐答案

原来很简单!

我在我的共享管道库中创建了一个 jobProperties.groovy 文件,它组成了参数列表并调用了 properties() 函数.

I created a jobProperties.groovy file in my shared pipeline library, which composes the parameter list and calls the properties() function.

def call() {
    params = [
        string(
            defaultValue: '',
            description: 'Version to deploy',
            name: 'VERSION'
        ),
    ]

    if (env.HANDLER == 'ansible') {
        params += [
            string(
                defaultValue: '',
                description: 'DEPLOY_ARGS | Ad hoc "ansible-playbook" args. Example to limit hosts to' +
                    ' deploy to "-l somehost"',
                name: 'DEPLOY_ARGS'
            ),
        ]
    } else if (env.HANDLER == 'capistrano') {
        params += [
            string(
                defaultValue: '',
                description: 'DEPLOY_ARGS | Ad hoc "cap" args. Example to limit hosts to' +
                    ' deploy to "-z somehost"',
                name: 'DEPLOY_ARGS'
            ),
        ]
    }

    properties([
        parameters(
            params
        )
    ])
}

管道.groovy

def call() {
    pipeline {
        agent { label 'deploy-slave' }

        stages {
            stage('Prepare') {
                steps {
                    script {
                        jobProperties()
                    }
                }
            }
        }
    }
}

我觉得如果没有共享管道库,jobParams.groovy的代码也可以直接放到管道的script {} wrapper中.

I think that if you don't have a shared pipeline library, the code of jobParams.groovy can be also put directly in the script {} wrapper of the pipeline.

这篇关于是否可以从管道步骤动态设置 Jenkins 作业参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆