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

Generated Multibranch Pipeline Job does not index branches

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Using a JobDSL script to generate a multibranch Pipeline job does not trigger a branch index to find Jenkinsfile

      Here is a simple dsl job

      multibranchPipelineJob(repo) {
        branchSources {
          github {
            scanCredentialsId(credentials)
            repoOwner(credentials)
            repository(repo)
          }
        }
      }
      

      Using this created the job fine but did not trigger a branch scan until I manually triggered a branch index. It also works if you open the multibranch job configuration and save it with no changes.

      Creating a multibranch job directly from the UI works fine.

      The only way I can trigger a branch index is to add a triggers section to the command to periodically scan every minute. I then had to create 3 build steps:

      1. JobDSL to create mutlibranch Pipeline job with a trigger set to 1 minute
      2. Shell step to Sleep for 60 seconds
      3. JobDSL to modify the multibranch Pipeline job and turn off the trigger.

        Attachments

          Issue Links

            Activity

            Hide
            si John F added a comment -

            I'm having an issue that matches this with Bitbucket multi branch pipeline jobs created from the Job DSL.  The credentials seem to be invalid, even though they are set correctly.  After running the seed job, the indexing will fail with an authorization failure against Bitbucket.  If I simply open the configuration for the job in the web UI, and save it with no changes, it will work from that point forward.  It also shows a red failure icon with "Not found" for the Owner (Project in BB) until I hit save.  Is this the same issue?  It's unclear from the comments/lack of resolution.  Are there possibly other already known issues that match this?

            Show
            si John F added a comment - I'm having an issue that matches this with Bitbucket multi branch pipeline jobs created from the Job DSL.  The credentials seem to be invalid, even though they are set correctly.  After running the seed job, the indexing will fail with an authorization failure against Bitbucket.  If I simply open the configuration for the job in the web UI, and save it with no changes, it will work from that point forward.  It also shows a red failure icon with "Not found" for the Owner (Project in BB) until I hit save.  Is this the same issue?  It's unclear from the comments/lack of resolution.  Are there possibly other already known issues that match this?
            Hide
            gregoirew greg oire added a comment -

            I also faced this issue. I end up adding a build step on my job :

             1st step: Creation of the pipeline if it do not exist:

            hudson.model.Item i=jenkins.model.Jenkins.instance.getItemByFullName("$folder/$name")
            
            if (i!=null){
               throw new javaposse.jobdsl.dsl.DslException("$folder/$name already exist")
            }
            
            multibranchPipelineJob("$folder/$name") {
              description("")
              ...
              branchSources {
                branchSource {
                  source {
                    github {
                      id(UUID.randomUUID().toString())
                      ...
                    }
                  }
                  strategy {
                    ...
                  }
                }
              }
            
              factory {
                workflowBranchProjectFactory {
                  scriptPath("Jenkinsfile")
                }
              }
            }
            

            Then on another step, I execute the post action that are not triggered :

            println("Initializing job")
            
            hudson.model.Item i=jenkins.model.Jenkins.instance.getItemByFullName("$folder/$name")
            
            println("Job found: "+i.getDisplayName())
            
            i.save()
            i.getSCMSources().get(0).afterSave()
            
            queue("$folder/$name")
            
            println("Inializing done")
            

            I save the job (I'm not sure it is needed, but it make the configuration file much like it is if I do it manually) then I trigger the "afterSave()" method on the SCM source (In github use-case, it adds the Webhook on the repo ) and finally I trigger a build of the multibranch so that branch are scanned.

            JobDSL is generic, so I'm not sure one day it will be able to do that, but until then, this will do the trick .... I hope ...

            Show
            gregoirew greg oire added a comment - I also faced this issue. I end up adding a build step on my job :  1st step: Creation of the pipeline if it do not exist: hudson.model.Item i=jenkins.model.Jenkins.instance.getItemByFullName("$folder/$name") if (i!=null){    throw new javaposse.jobdsl.dsl.DslException("$folder/$name already exist") } multibranchPipelineJob("$folder/$name") {   description("") ...   branchSources {   branchSource { source { github { id(UUID.randomUUID().toString()) ... } } strategy { ... } } } factory { workflowBranchProjectFactory { scriptPath("Jenkinsfile") } } } Then on another step, I execute the post action that are not triggered : println("Initializing job") hudson.model.Item i=jenkins.model.Jenkins.instance.getItemByFullName("$folder/$name") println("Job found: "+i.getDisplayName()) i.save() i.getSCMSources().get(0).afterSave() queue("$folder/$name") println("Inializing done") I save the job (I'm not sure it is needed, but it make the configuration file much like it is if I do it manually) then I trigger the "afterSave()" method on the SCM source (In github use-case, it adds the Webhook on the repo ) and finally I trigger a build of the multibranch so that branch are scanned. JobDSL is generic, so I'm not sure one day it will be able to do that, but until then, this will do the trick .... I hope ...
            Hide
            daspilker Daniel Spilker added a comment -

            The problem is IMHO, that the MultiBranchProject will do the necessary updates only after the config page has been saved (Stapler submit):

            https://github.com/jenkinsci/branch-api-plugin/blob/branch-api-2.5.2/src/main/java/jenkins/branch/MultiBranchProject.java#L896-L897

            But it should also implement ItemListener and do the updates in onUpdated, onCreated, etc to allow to create a multi-branch project through API (e.g. ModifiableTopLevelItemGroup.createProjectFromXML or AbstractItem.updateByXml).

            This issue does not only apply to Job DSL, but also to other consumers of that API, maybe CLI and REST API.

            Show
            daspilker Daniel Spilker added a comment - The problem is IMHO, that the MultiBranchProject will do the necessary updates only after the config page has been saved (Stapler submit): https://github.com/jenkinsci/branch-api-plugin/blob/branch-api-2.5.2/src/main/java/jenkins/branch/MultiBranchProject.java#L896-L897 But it should also implement ItemListener and do the updates in onUpdated , onCreated , etc to allow to create a multi-branch project through API (e.g. ModifiableTopLevelItemGroup.createProjectFromXML or AbstractItem.updateByXml). This issue does not only apply to Job DSL, but also to other consumers of that API, maybe CLI and REST API.
            Show
            daspilker Daniel Spilker added a comment - PR: https://github.com/jenkinsci/branch-api-plugin/pull/158  
            Hide
            renescheibe René Scheibe added a comment - - edited

            Daniel Spilker does this pull request also fix the issues reported in JENKINS-43693 and JENKINS-57235?

            At first sight it might do so. This would be really great.

            Show
            renescheibe René Scheibe added a comment - - edited Daniel Spilker does this pull request also fix the issues reported in JENKINS-43693 and JENKINS-57235 ? At first sight it might do so. This would be really great.

              People

              • Assignee:
                stephenconnolly Stephen Connolly
                Reporter:
                hrmpw Patrick Wolf
              • Votes:
                6 Vote for this issue
                Watchers:
                16 Start watching this issue

                Dates

                • Created:
                  Updated: