1
votes

I have multi branch pipeline with parameterized build setup in the jenkinsfile. Is there any option available in Jenkins to skip Build with parameters step, i.e.The user can directly run the job.

In other words the user can build the job with default parameter value, they don't no need to visit the parameter view. The admin can change the parameter by using Jenkins remote API trigger.

4
This should be possible with both the browser UI, webhooks, and the REST API. Are those not working for you? - Matt Schuchard
@MattSchuchard: Can you please elaborate your suggestion? I can run REST API and handle the webhook. I don't know much about the browser UI you told. - Yahwe Raj

4 Answers

3
votes

There's another solution for that which doesn't require installing an additional plugin.

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
    }
}

You can define a timeout for the supply parameters section and if the timeout expired an no entries have been inserted the "catch" section will set default parameters and the build will start without any user intervention.

2
votes

you should first create an choice parameter and then use it to select and step.

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 !" } } } }

1
votes

I finally found this plugin to resolve my problem.

First of all the Jenkins file have to configure this hidden parameter plugin. This will hides the parameters which are not needed to change from user end.

jenkinsfile:

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

Second the git repository must have configured the webhook to trigger the Jenkins build using REST API.

0
votes

Another option is to use the groovy elvis operator to check if the parameter ENV variables are set. If the parameters are empty/unset, then a default value can be used.

This requires no additional plugins, and resolves the issue of not having the parameters displayed on the first build of a new branch.

It also does not introduce a pause/delay waiting for userinput (as is the case with one of the other suggested answers).

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"
                }
            }
        }
    }
}