Indexing trigger branch on multi-channel pipelines (Jenkins / Git)

I am trying to automatically run Branch Indexing in a Jenkins Multibranch Pipelines project.

Only one method actually works at the moment, which is polling, but I cannot do this, and polling is a bad solution anyway.

The plugin does not support "Trigger assemblies remotely (for example, from scripts)" (the parameters are not saved), so I can not start it using the web hook when clicked, etc.

I tried to build a freestyle “trigger” assembly on the repo, but “Actions after assembly” - “Building other projects” claims that the Multibranch Pipeline project is not a project that can be built.

If polling is the only way I can do this, then I need to disable SCM auto-launch (otherwise, we will get duplicates when re-indexing), because I need to activate the launch of the web hook in the branch projects.

But this does not work, because I configure the web hook on the script pipeline in the branch project, and you need to create it at least once in order to register this property.

I have been going around in a circle for some time, so I hope I just missed something obvious, but any help would be appreciated.

I imagined the opportunity to do one of the following

  • Somehow initiate a project with multiple branches as a top-down project

  • Interrogate a project with multiple channels and only create branch projects that have not yet been built before

Greetings

+5
source share
3 answers

The ComputedFolder.scheduleBuild() method can be called from a groovy script.

I just called branch indexing in one project with a multi-block pipeline from groovy code in another multi-brand pipeline project, which then starts a downward build in that project.

The code looks something like this:

 @NonCPS void scanRepo(String downStreamProjectName) { Jenkins.instance.getItemByFullName(downStreamProjectName).scheduleBuild() } ... String downStreamProject = 'my-folder/my-multibranch-project' String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}" if (Jenkins.instance.getItemByFullName(downStreamJob) == null) { scanRepo(downStreamProject) while (Jenkins.instance.getItemByFullName(downStreamJob) == null) { sleep(1) } } build([job: downStreamJob, wait: false, quietPeriod: 0]) 

Note that Jenkins.instance.getItemByFullName(downStreamProjectName) is a WorkflowMultiBranchProject that is not Serializable , so some caution needs to be done.

+4
source

Based on @jjc's answer , I created a version using the build step also to start a scan:

 String downStreamProject = 'my-folder/my-multibranch-project' String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}" if (Jenkins.instance.getItemByFullName(downStreamJob) == null) { // we would need "wait: true", which is not possible as of now // https://github.com/jenkinsci/pipeline-build-step-plugin/blob/3ff14391fe27c8ee9ccea9ba1977131fe3b26dbe/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepExecution.java#L66 build job: downStreamProject, wait: false // continue only once the expected job appears while (Jenkins.instance.getItemByFullName(downStreamJob) == null) { sleep(1) } } build downStreamJob 

This requires confirmation of the following signatures:

  • method jenkins.model.Jenkins getItemByFullName java.lang.String
  • staticMethod jenkins.model.Jenkins getInstance
0
source

The simplest option that I know of is to remotely tell the Jenkins Git plugin that there is a new commit for a specific repository. However, this will not cause Jenkins to immediately begin work. It happens that the Git plugin starts (re) indexing a specific repository. Then the Jenkins task starts if changes are detected.

From your repository (GitHub, GitLab, etc.) you should run the following URL:

http://my-jenkins-host/git/ notifyCommit?url=git@gitlab.example.com :group/repository.git&delay=0sec

The url value must match the SCM URL configured in the Jenkins job (Git plugin)!

Gotcha: maybe your Jenkins is not deployed in the root context ( / ), in which case the URL would be http://my-jenkins-host/context-path/git/...

0
source

Source: https://habr.com/ru/post/1262610/


All Articles