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

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

    XMLWordPrintable

Details

    Description

      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.)

      Attachments

        Issue Links

          Activity

            felipecassiors 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.

            felipecassiors 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.
            jensre 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

            jensre 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
            felipecassiors 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}" } }
              }
            }
            
            felipecassiors 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}" } } } }
            jensre 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.

            jensre 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 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 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.

            People

              Unassigned Unassigned
              jglick Jesse Glick
              Votes:
              159 Vote for this issue
              Watchers:
              170 Start watching this issue

              Dates

                Created:
                Updated: