在 Jenkins 中跳过使用参数构建的选项

Skip the option build with parameters in Jenkins

我在 jenkinsfile 中有带参数化构建设置的多分支管道。 Jenkins 中是否有任何选项可以跳过带参数的构建步骤,i.e.The 用户可以直接 运行 作业。

换句话说,用户可以使用默认参数值构建作业,他们不需要访问参数视图。管理员可以使用 Jenkins 远程 API 触发器更改参数。

我终于找到 this 插件来解决我的问题。

首先Jenkins文件必须配置这个隐藏参数插件。这将隐藏不需要从用户端更改的参数。

詹金斯文件:

properties([[$class: 'ParametersDefinitionProperty', 
        parameterDefinitions: [
            [$class: 'WHideParameterDefinition', name: 'isValid', defaultValue: 'false']
        ]
    ]])

其次,git 存储库必须配置 webhook 以使用 REST API.

触发 Jenkins 构建

还有另一种不需要安装额外插件的解决方案。

stage ('Setup') {
    try {
        timeout(time: 1, unit: 'MINUTES') {
            userInput = input message: 'Configure build parameters:', ok: '', parameters: [
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'staging\nproduction\nfree', description: 'Choose build flavor', name: 'BUILD_FLAVOR'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'Debug\nRelease', description: 'Choose build type', name: 'BUILD_TYPE'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: '4.1.12\n4.1.11\n4.1.10\n4.1.9\n4.1.8\n4.1.4\n3.5.5\n3.1.8\ncore\nOldVersion', description: 'Version Name', name: 'VERSION_NAME'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'origin/develop\norigin/hotfix/4.1.11\norigin/release/4.1.8\norigin/hotfix/4.1.7\norigin/hotfix/4.1.9\norigin/hotfix/4.1.10\norigin/release/4.1.6\norigin/release/4.1.5\norigin/hotfix/3.5.5', description: 'Git branch', name: 'GIT_BRANCH'],
                [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Enable Gradle debug?', name: 'DEBUG']
        ] // According to Jenkins Bug: https://issues.jenkins-ci.org/browse/JENKINS-26143
      }
    } catch (err) {
        userInput = [BUILD_FLAVOR: 'staging', BUILD_TYPE: 'Debug',  VERSION_NAME: '4.1.12', GIT_BRANCH: 'origin/develop'] // if an error is caught set these values
    }
}

您可以为供应参数部分定义超时,如果超时到期且未插入任何条目,"catch" 部分将设置默认参数并且构建将在没有任何用户干预的情况下开始。

您应该首先创建一个选择参数,然后将其用于 select 和步骤。

pipeline { agent any parameters { choice( choices: 'true\nfalse', description: 'should my action run ? ', name: 'ACTION') } stages { stage ('stage_name') { when { expression { params.ACTION == 'true' } } steps { echo "My action run !" } } } }

另一种选择是使用 groovy elvis 运算符来检查是否设置了参数 ENV 变量。如果参数是empty/unset,那么可以使用默认值。

这不需要额外的插件,并解决了在新分支的第一次构建时不显示参数的问题。

它也没有引入 pause/delay 等待用户输入(其他建议答案之一就是这种情况)。

pipeline {

    parameters {
        booleanParam(name: 'RUN_TESTS',
                     defaultValue: true)
        string(name: 'DEPLOY_ENVIRONMENT',
               defaultValue: "dev")
    }

    stages {
        stage('build') {
            steps {
                script {
                    runTests          = env.RUN_TESTS           ?: true
                    deployEnvironment = env.DEPLOY_ENVIRONMENT  ?: "dev"
                }
            }
        }
    }
}