Jenkins Multi-branch: How do I add functionality to branch creation/deletion?
Asked Answered
P

2

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?

Pippas answered 15/7, 2019 at 12:24 Comment(9)
so you want one jenkins job per branch? or one jenkins job to handle all the branches?Sazerac
I have added a screenshot. The jobs are managed by the multibranch pipeline functionality in Jenkins.Prudential
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.Herminahermine
@ThorbjørnRavnAndersen, why not have a webhook for branch deletion and creation which then triggers a jenkins job? i.sstatic.net/NJcHI.png. You can then from that jenkins job trigger other jobs or anything else you want.Feleciafeledy
@TarunLalwani Because that is what is already happening? I'd like to leverage their work if at all possible.Prudential
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?Reprobative
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.Prudential
Can you add a link to the Jenkins plugin (by editing the question)? Is it Pipeline Multibranch (the name does not quite match)?Arium
@PeterMortensen Sorry, missed your comment. Updated with a link to the only multi-something plugin listed by Jenkins.Prudential
E
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.

Environs answered 15/1, 2020 at 21:49 Comment(0)
H
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.

Humpy answered 19/7, 2019 at 12:51 Comment(2)
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?Prudential
@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)Humpy

© 2022 - 2024 — McMap. All rights reserved.