Skip to content

Instantly share code, notes, and snippets.

@q0rban
Created December 7, 2012 20:53
Show Gist options
  • Save q0rban/4236407 to your computer and use it in GitHub Desktop.
Save q0rban/4236407 to your computer and use it in GitHub Desktop.
Example Job for Jenkins Github Drupal Pull Request Builder
#!/usr/bin/env bash
set -e
# Jenkins job script for use with the Jenkins Github Drupal, which can be found
# at https://github.com/Lullabot/jenkins_github_drupal. NOTE, all shell scripts
# have been symlinked to /usr/local/bin to make them easier to call here. For
# example:
# ln -s /usr/local/share/jenkins_github_drupal/prepare_dir.sh \
# /usr/local/bin/jgd-prepare-dir
# Since the pull request builder doesn't give us the pull request ID as an
# environment variable, we need $sha1 to be of the format origin/pr/[0-9]/merge,
# so that we can grep out the Pull Request ID.
if [[ $sha1 == origin/pr* ]]; then
ISSUE_NUMBER=${sha1//[^0-9]/}
else
echo "sha1 must be in the format origin/pr/[0-9]/merge." 1>&2
exit 1;
fi
# If you're using something like the Description Setter plugin, you can use this
# line to set the build description. Just set your regex to \[BUILD\] (.*)
echo "[BUILD] Pull Request #$ISSUE_NUMBER"
# Location of the parent directory of the web root this site will be hosted at.
# Defaults to the job workspace. Note, the Jenkins user must have write
# permissions to this directory.
WEBROOT=/var/www/pull-requests/example.com
# Test whether this pull request already exists.
EXISTING=false
if [[ -L $WEBROOT/$ISSUE_NUMBER ]]; then
EXISTING=true
fi
# This does all the work of merging to master, and symlinking the directory to
# the webroot specified above.
jgd-prepare-dir -i $ISSUE_NUMBER $WEBROOT
# The apache or web group on the server, used to chgrp the files directory
# appropriately after rsyncing.
WEBGROUP="www-data"
# The drush alias of the site you are cloning. Typically, this is your staging
# site. Do NOT use a production site here.
ALIAS="@example-com-stage"
# The parent URL that the destination site will be visible at. Defaults to
# 'http://default'. The domain name the site will be set up at. Note, the site
# will be in a subdirectory of this domain using the Pull Request ID, so if the
# Pull Request ID is 234, and you pass https://www.example.com/foo, the site
# will be at https://www.example.com/foo/234. You can get around that with URL
# rewriting in your webserver such that pr.234.example.com points to your site.
URL=https://stage.example.com
# Execute the actual command to clone the site.
jgd-clone-site -i $ISSUE_NUMBER -l $WEBROOT -g $WEBGROUP $ALIAS $URL
# Comment on github with a URL to the new environment below:
BODY="This pull request's testing environment is ready at $URL/$ISSUE_NUMBER"
# If the environment already existed, just comment that it has been updated.
if $EXISTING; then
BODY="The testing environment has been updated with the latest code."
fi
# The Github Organization or User and Repo name.
GITHUB_ACCOUNT_REPO="Lullabot/example.com"
# Github token for the bot user. It is recommended to use the Jenkins EnvInject
# Plugin, and use the Inject Passwords option in your job, rather than
# specificying this here.
#GITHUB_TOKEN
jgd-github-comment -a $GITHUB_ACCOUNT_REPO -i $ISSUE_NUMBER -b "$BODY" <<< $GITHUB_TOKEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment