6

Multibranch pipeline Jenkins screensnippet

I am setting up a Jenkins multi-branch instance which needs to allocate a resource on branch job creation when a new branch is detected by the https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin plugin (version 2.21), and deallocate it when the branch job is deleted by the Multi-branch pipeline when the branch is not present in Git any more.

(To be precise, we need to ship the built binaries to an OpenShift deployment which needs to be explicitly set up and destroyed for each branch).

I have looked at the documentation, but not yet found the location where this can be configured. To my untrained eye, this would make sense to be a "stage" outside the "stages" node, but it is the same for all builds, so it could also go outside Jenkinsfile.

How can I do this?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • so you want one jenkins job per branch? or one jenkins job to handle all the branches? – Jose Martinez Jul 17 '19 at 14:18
  • I have added a screenshot. The jobs are managed by the multibranch pipeline functionality in Jenkins. – Thorbjørn Ravn Andersen Jul 17 '19 at 14:31
  • Afaik this isn't possible in the current state. The builtin MultibranchPipeline is quite limited. You can however, create your own custom Multibranch-lookalike with the help of a seed job, that triggers regularly and includes all logic on branch discovery, branch deletion discovery, other custom rules, and then does things like creating a job with help of the jobDSL plugin or trigger other actions. – Dominik Gebhart Jul 17 '19 at 15:41
  • @ThorbjørnRavnAndersen, why not have a webhook for branch deletion and creation which then triggers a jenkins job? https://i.stack.imgur.com/NJcHI.png. You can then from that jenkins job trigger other jobs or anything else you want. – Tarun Lalwani Jul 17 '19 at 16:18
  • @TarunLalwani Because that is what is already happening? I'd like to leverage their work if at all possible. – Thorbjørn Ravn Andersen Jul 17 '19 at 18:26
  • What kind of resources are we talking about here? If it makes sense to allocate them in an init stage and release in post actions? – Ram Jul 17 '19 at 18:48
  • I need to invoke a shell script for init and another for release (which in turn allocate and deallocate a resource on our cluster, but for this purpose it is just a shell). It is a oneliner so it may be a single `sh("...")` command. – Thorbjørn Ravn Andersen Jul 17 '19 at 19:21
  • Can you add a link to the Jenkins plugin (by editing the question)? Is it *[Pipeline Multibranch](https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin)* (the name does not quite match)? – Peter Mortensen Jul 20 '19 at 11:14
  • @PeterMortensen Sorry, missed your comment. Updated with a link to the only multi-something plugin listed by Jenkins. – Thorbjørn Ravn Andersen Jul 23 '19 at 10:42

2 Answers2

1

You can use the MultiBranch Action Triggers plugin. With this plugin you run external jobs on branch creation/deletion. You need to create external pipelines which will do things on OpenShift.

Aytunc Beken
  • 111
  • 6
0

First, allocation of your resources could be part of the pipeline stage:

  • Allocating blindly to OpenShift and store the branch/resource id within a file
  • Then check this allocation file in the next build before a new allocation

OR

  • You could first interrogate OpenShift for allocated

Finally, you can build another Jenkins job (scheduled, crontab-like based) for housekeeping.

This one could fetch out the available branch and compare to OpenShift allocated resources (or by an ad hoc file, or by fetching directly from OpenShift) and then call for the cleanup script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Grunnpi
  • 92
  • 6
  • So essentially moving it inside a build step instead (which is what we currently do) or outside the pipeline? So you say what I want to do is not possible? – Thorbjørn Ravn Andersen Jul 22 '19 at 15:48
  • @ThorbjørnRavnAndersen, according to me yes. A first build step to assume/assert resource is available or created. Another scheduled job for cleanup/keep-alive purpose. _(Note that you can build reusable library to embed into your pipeline to avoid duplicated code every where)_ – Grunnpi Jul 23 '19 at 10:24