Scripted Git Branch Deployment with Jenkins

When working with git, normally work is done on a branch then merged onto master. However, it is often desired or required that someone else take a look at it before putting it on master. The process of deploying your branch to a test environment is oftentimes manual / time consuming. It does not have to be that way. It can be as simple as ‘git push; ./deploy.sh;’ using remotely triggered parameterized Jenkins builds. Despite the overly verbose description, the setup is actually not time consuming. In the following section, I am making the assumption that you already have Jenkins build that deploys master to the test environment.

Configuration

  1. Firstly, create a new Jenkins build cloned from your normal deploy job for master.
  2. Mark it as parameterized.Check 'This project is parameterized'. Select 'Branch' as the Parameter Type and give it a logical name.
  3. Mark it as remotely triggered.Check 'Trigger builds remotely (e.g., from scripts)'. Enter an authentication token.
  4. Save the configuration.
  5. Finally, write the deploy script. Something like this:
#!/bin/bash
branchName=$(git symbolic-ref --short HEAD)
echo "Deploying $branchName to test environment"
curl "JENKINS_URL/job/deploy-branch/build?token=26a75277-384c-4a56-9848-3c10df6d4697&BRANCH_NAME=$branchName"