Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-41929

Offer "Build with Parameters" on first build when declarative Jenkinsfile found

      By default a branch project will automatically run the first build, with no parameters, so params will just pick up any default values. You have the option to suppress the automatic first build, but this does not give you any way to enter parameters for it (at least in the UI; perhaps possible via CLI/REST), since Jenkins does not know what the parameters are going to be until it starts running. But in the case of Declarative we could in principle inspect the Jenkinsfile when the branch project is created (via SCMFileSystem) and determine the parameter definitions by static parsing without actually running.

      More generally, if Declarative is in use and there are properties, we could set all the project properties when the branch project is created, even if the first build is run automatically. (Though I would suggest that the automatic first build should be automatically suppressed if there is a ParametersDefinitionProperty.)

          [JENKINS-41929] Offer "Build with Parameters" on first build when declarative Jenkinsfile found

          Miguel Costa added a comment -

          I guess there is no real chances this gets implemented since it's been more than 3 years since it's open?
          Is there any good way around this without failing the first build but also having to run it manually?

          Miguel Costa added a comment - I guess there is no real chances this gets implemented since it's been more than 3 years since it's open? Is there any good way around this without failing the first build but also having to run it manually?

          mcosta : From felipecassiors's comment above (slightly simplified), we add this to pipelines:

           

          stage('prepare') {
            steps {
              // Initialize params as envvars, workaround for bug https://issues.jenkins-ci.org/browse/JENKINS-41929
              script { params.each { k, v -> env[k] = v } }
            }
          }

           

          Quentin Nerden added a comment - mcosta : From felipecassiors 's comment above (slightly simplified), we add this to pipelines:   stage( 'prepare' ) { steps { // Initialize params as envvars, workaround for bug https://issues.jenkins-ci.org/browse/JENKINS-41929 script { params.each { k, v -> env[k] = v } } } }  

          Felipe Santos added a comment -

          qnerden slightly? This is much cleaner and better! Thanks a lot for sharing.

          One important thing to note is that none of the parameters should be included in `environment` section of the pipeline, otherwise changes made during a stage (like this 'prepare' one) won't be retained for all stages.

          Felipe Santos added a comment - qnerden  slightly? This is much cleaner and better! Thanks a lot for sharing. One important thing to note is that none of the parameters should be included in `environment` section of the pipeline, otherwise changes made during a stage (like this 'prepare' one) won't be retained for all stages.

          orasre added a comment - - edited

          Hi felipecassiors,

          I am having the same issue but using the above workaround didn't work. I have two parameters one of them is a choice that is populated by a list variable. The other one is an empty text field to be populated later. None of them can have a default value.

           

          Thanks

          orasre added a comment - - edited Hi felipecassiors , I am having the same issue but using the above workaround didn't work. I have two parameters one of them is a choice that is populated by a list variable. The other one is an empty text field to be populated later. None of them can have a default value.   Thanks

          Felipe Santos added a comment -

          Try with this:

          stage('prepare') {
            steps {
              // Initialize params as envvars, workaround for bug https://issues.jenkins-ci.org/browse/JENKINS-41929
              script { params.each { k, v -> env[k] = "${v}" } }
            }
          }
          

          Felipe Santos added a comment - Try with this: stage( 'prepare' ) { steps { // Initialize params as envvars, workaround for bug https://issues.jenkins-ci.org/browse/JENKINS-41929 script { params.each { k, v -> env[k] = "${v}" } } } }

          orasre added a comment -

          Thanks felipecassiors  for your quick reply. It didn't work too. In my case, I am using the seed job to update the Jenkins pipeline jobs. Once, I run the seed job, the "Build with Parameters" options disappears from the updated pipeline and I then am left with just "build"

           

          It is OK for most of my jobs to just start and stop a job manually to bring the "Build with Parameters" back but the problem is with the scheduled ones. They won't start unless I manually start/stop them first.

          orasre added a comment - Thanks felipecassiors   for your quick reply. It didn't work too. In my case, I am using the seed job to update the Jenkins pipeline jobs. Once, I run the seed job, the "Build with Parameters" options disappears from the updated pipeline and I then am left with just "build"   It is OK for most of my jobs to just start and stop a job manually to bring the "Build with Parameters" back but the problem is with the scheduled ones. They won't start unless I manually start/stop them first.

          menna khaled added a comment -

          hello jensre, what worked for me is: I get the job via API, I get the 'nextBuildNumber' field, then when I upload the jenkinsfile of a job I add to the jenkinsfile a check if the build number is equal to <nextBuildNumber> , then abort the build, this way the button is 'build with parameters' for all the upcoming builds.

          menna khaled added a comment - hello jensre , what worked for me is: I get the job via API, I get the 'nextBuildNumber' field, then when I upload the jenkinsfile of a job I add to the jenkinsfile a check if the build number is equal to <nextBuildNumber> , then abort the build, this way the button is 'build with parameters' for all the upcoming builds.

          Antoine added a comment -

          Hi jensre , in your case this is easier.

          In your seed job, include in your pipeline job definition the parameters:

          pipelineJob('YourJob')

          {     parameters \{stringParam('First param')}

              parameters {stringParam('Second param')}

          Then the "Build with Parameters" will be available after the seed job creates/updates your pipeline jobs.

          Antoine added a comment - Hi jensre , in your case this is easier. In your seed job, include in your pipeline job definition the parameters: pipelineJob('YourJob') {     parameters \{stringParam('First param')}     parameters {stringParam('Second param')} Then the "Build with Parameters" will be available after the seed job creates/updates your pipeline jobs.

          Diptiman added a comment - - edited

          Hi,

          I am using one work around to get rid of this issue. I thought of sharing this as it might help some folks who is dealing with this issue. Let's say I need to pass param1, param2 to the declarative pipeline what is used to generate the actual Jenkins pipeline. We can use temporary vars to store the actual value of parameters we want to pass for the pipelines to run and override the pipelines params just before the pipeline starts. 

          Below snippet is reference for storing the pipeline parameters into temp vars.

           

          public interface Params {
            String param1 = 'value1'
            String param2 = 'value2'
          } 
          
          pipeline {
          .
          .
          .
            parameters {
              string (
                      description: 'param1',
                      name: 'PARAM1',
                      trim: true,
                      defaultValue: Params.param1
               )
              string (
                      description: 'param2',
                      name: 'PARAM2',
                      trim: true,
                      defaultValue: Params.param2
               )

           

          Then we can override the params before the actual build starts to override the param value: 

           

          stage('Override Build params from SCM config') {
            steps {
              echo 'Initialize parameters as environment variables due to https://issues.jenkins-ci.org/browse/JENKINS-41929'
              echo "Debug: The value of param1 before ${params.param1}"
              script {
                env.param1 = Params.param1
                env.param2 = Params.param2
                echo "Debug: The value of param1 after ${param1} or ${params.param1}"
              }
            }
          } 

          There are couple of drawbacks for this solution:

          • We need to modify the param in two places in groovy script
          • For triggered pipelines it will still show the old param value from the previous config in the build param page

          Thanks

          Diptiman Adak

           

          Diptiman added a comment - - edited Hi, I am using one work around to get rid of this issue. I thought of sharing this as it might help some folks who is dealing with this issue. Let's say I need to pass param1, param2 to the declarative pipeline what is used to generate the actual Jenkins pipeline. We can use temporary vars to store the actual value of parameters we want to pass for the pipelines to run and override the pipelines params just before the pipeline starts.  Below snippet is reference for storing the pipeline parameters into temp vars.   public interface Params { String param1 = 'value1' String param2 = 'value2' } pipeline { . . . parameters { string ( description: 'param1' , name: 'PARAM1' , trim: true , defaultValue: Params.param1     ) string ( description: 'param2' , name: 'PARAM2' , trim: true , defaultValue: Params.param2     )   Then we can override the params before the actual build starts to override the param value:    stage( 'Override Build params from SCM config' ) { steps { echo 'Initialize parameters as environment variables due to https: //issues.jenkins-ci.org/browse/JENKINS-41929' echo "Debug: The value of param1 before ${params.param1}" script { env.param1 = Params.param1 env.param2 = Params.param2 echo "Debug: The value of param1 after ${param1} or ${params.param1}" } } } There are couple of drawbacks for this solution: We need to modify the param in two places in groovy script For triggered pipelines it will still show the old param value from the previous config in the build param page Thanks Diptiman Adak  

          Jesse Glick added a comment -

          Jesse Glick added a comment - Discussed in https://github.com/jenkins-infra/repository-permissions-updater/issues/3551#issuecomment-1750578563 .

            Unassigned Unassigned
            jglick Jesse Glick
            Votes:
            168 Vote for this issue
            Watchers:
            180 Start watching this issue

              Created:
              Updated: