Jenkins "Build Per Branch"

Auto-create Jenkins build jobs for new feature branches

View the Project on GitHub entagen/jenkins-build-per-branch

Jenkins Build Per Branch

The code in this repo lets you automatically generate Jenkins jobs for new branches in a specified repository, using templates to create the jobs.

Using it is as easy as:

This job will be in charge of both creating new jobs for new feature branches as well as cleaning up old feature branch jobs that have been fully merged into another branch

Installation

It has the following plugin requirements in Jenkins:

There are a few ways to use jenkins-build-per-branch but the easiest is probably to make a new Jenkins job that runs on a cron timer (every 5 minutes or so).

You'll want to already have your template jobs running and passing (likely one or more jobs on the master branch of your repo).

Then, create a new Jenkins job called SyncYOURPROJECTGitBranchesWithJenkins as a "free-style software project".

Set the job to clone the jenkins-build-per-branch repo from github with the repository url git@github.com:entagen/jenkins-build-per-branch.git.

The branch to build should be set to origin/master.

Check the box to have it "build periodically" and set the cron expression to */5 * * * * to get it building every 5 minutes.

Click on the "Add build step" dropdown and pick "Invoke Gradle script" from the dropdown.

In the "Switches" box, enter the system parameters unique to your jenkins setup and git install, here's an example that you can use to tweak:

-DjenkinsUrl=http://localhost:8080/jenkins -DgitUrl=git@github.com:mygithandle/myproject.git -DtemplateJobPrefix=MyProject -DtemplateBranchName=master -DnestedView=MyProject-branches -DdryRun=true
            

Those switches include a -DdryRun=true system parameter so that the initial run does not change anything, it only logs out what it would do.

In the "Tasks" field, enter syncWithRepo.

Save the job and then "Build Now" to see if you've got things configured correctly. Look at the output log to see what's happening. If everything runs without exceptions, and it looks like it's creating the jobs and views you want, remove the -DdryRun=true flag and let it run for real.

This job is potentially destructive as it will delete old feature branch jobs for feature branches that no longer exist. It's strongly recommended that you back up your jenkins jobs directory before running, just in case. Another good alternative would be to put your jobs directory under git version control. Ignore workspace and builds directories and just about everything can be added. Commit periodocally and if something bad happens, revert back to the last known good version.

Script System Parameter Details

The following options are available in the script:

Conventions

It is expected that there will be 1 or more "template" jobs that will be replicated for each new branch that is created. In most workflows, this will likely be the jobs for the master branch.

The job names are expected to be of the format:

<templateJobPrefix>-<jobName>-<templateBranchName>

Where:

So you could have 3 jobs that are dependent on each other to run, ex:

If you created a new feature branch (ex: newfeature) and pushed it to the main git repo, it would create these jobs in Jenkins:

It will also create a new view for the branch to contain all 3 of those jobs called "MyProject-newfeature". If you haven't used the nestedView parameter, it will be a new tab at the top of the main screen, otherwise it will be contained within that view.

Once newfeature was tested, accepted, and merged into master, the sync job would then delete those jobs and it's view on the next run.

What Kinds of Worflows Need a Build Per Branch?

This can be very useful if your team is using a workflow like Github Flow.

In this workflow:

The advantages to this model are:

One disadvantage is that with multiple, short-lived branches, manually creating CI to confirm that all tests continue to pass becomes far too much overhead. It must be automated for this model to work and be trusted. "Jenkins Build Per Branch" is the answer to that problem.

Potential Future Enhancements