3

I'm newbie in Jenkins. I've created pipeine with parameters in Jenkinsfile in git. It works fine.

Problem is: when I push new parameters into Jenkinsfile, then press button "Build with Parameters", there are no new parameters. I will see them only on next build, after step "Declarative: Checkout SCM" will be done.

I know workaround: add parameter to skip all steps. Then I can run build and skip all steps (except "Declarative: Checkout SCM" of course) and afterwards run build with new parameters.

Question: is there are some magic URL or other possibility to update parameters from git before build?

2

3 Answers 3

2

As implied by the links in comments, for better or for worse, this behavior is an unavoidable consequence of how Pipeline is designed. Pipeline configuration changes in your Jenkinsfile cannot be picked up by Jenkins without executing your Pipeline. This includes things such as adding parameters.

My personal advice is to ensure that your Pipeline jobs are non-destructive, or in other words, to ensure that the worst thing that could possibly happen by running a job is nothing. This allows you to pick up configuration changes by just running your job after committing the changes to your Jenkinsfile, and then you can simply abort the job (or let it run to completion if it's a short job) with the confidence that you won't break anything. Then subsequent runs will have your configuration changes available.

0

A few years ago I used a similar approach:

 parameters {
        booleanParam(
                name: 'ABORT_JOB', defaultValue: false,
                description:    'Set this to true if you need to "dry-run" this job without doing any real work.\n' +
                        'The Jenkinsfile will be read and new parameters added. It will exit in the first stage.')

And in the first stage that runs:

stage('Initialise') {
            steps {
                script {
                    if (params.ABORT_JOB) {
                        currentBuild.result = 'ABORTED'
                        error('Stopping early...')
                    }

I've since started using JobDSL to generate my jobs. This simplifies the pipeline code and removes the need to code as above.

0

My workaround looks like this:

pipeline {
    parameters {
        booleanParam(name: 'skip_all_steps', defaultValue: false, description: 'When changed parameters in pipeline, just update code from git')
    }
    stages {
        stage('Stage1'){
            when { not { expression { return params.skip_all_steps } } }
            steps {
                …
            }
        }
        stage('Install requirements') {
            when {
                allOf {
                    expression { return params.install_django_requirements }
                    not { expression { return params.skip_all_steps } }
                }
            }
            steps {
                sh 'pip3 install --upgrade --user -r src-django/requirements.txt'
                }
        }
        …
    }
    post {
        success {
            script {
                if (!params.skip_all_steps.toBoolean() && params.workspace_cleanup.toBoolean()) {
                    cleanWs()
                }
            }

        }
    }
}

Be careful:

  • parameters in Jenkins UI will be updated only after successful build,
  • maybe it is better idea to use defaultValue: true for skip_all_steps in your particular case (for example, to avoid running any step on first build)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.