cd ~
git clone git://github.com/Lullabot/jenkins_github_drupal.git
sudo mv jenkins_github_drupal /usr/local/share
sudo ln -s /usr/local/share/jenkins_github_drupal/cleanup.sh \
/usr/local/bin/jgd-cleanup
sudo ln -s /usr/local/share/jenkins_github_drupal/clone_site.sh \
/usr/local/bin/jgd-clone-site
sudo ln -s /usr/local/share/jenkins_github_drupal/github_comment.sh \
/usr/local/bin/jgd-github-comment
sudo ln -s /usr/local/share/jenkins_github_drupal/prepare_dir.sh \
/usr/local/bin/jgd-prepare-dir
jgd-cleanup -h
jgd-clone-site -h
jgd-github-comment -h
jgd-prepare-dir -h
- Log into Jenkins
- Go to Manage Jenkins
- Click on Manage Plugins
- Click on Available Plugins
- Install these plugins
- EnvInject Plugin
- Github pull requests builder
Note: You must restart Jenkins after installing these plugins, otherwise expect errors.
Some nice to have Plugins:
- AnsiColor plugin, to show Drush colored output for errors and warnings.
- Log Parser plugin, with Drush rules to log errors and warnings in Jenkins based on the same from drush command output.
After the Log Parser plugin is installed, you'll need to add Drush rules. See https://gist.github.com/4236526 for instructions.
- Go back to 'Manage Jenkins' and click on 'Configure System'
- Down at the bottom under Github pull requests builder, enter the credentials for Github "bot" user, for instance,
serverbots
. - Enter the admin list. 'Admins' are users that can kick off builds of the jobs by typing comments on the Pull Request. Separate each user with spaces.
- Click Advanced in that same area if you'd like to change the regex expression to look for to kick off jobs
You need to have a webroot that Jenkins can write to for this job. This requires an Apache Vhost or Nginx server spec that allows subdirectories, and following symlinks. For example, say you have a staging site at http://stage.example.com. You might want to create another site at http://pull-request.example.com, or http://pr.stage.example.com, etc. You then need a directory that is writable by the Jenkins user that this vhost points to. Note that if you are executing this job on a Jenkins node, the user the command is being executed as may not be jenkins. Adjust the following accordingly. You will also want to make sure that the Jenkins user is in the web group, such as www-data, or apache, or whatever group your web server runs as.
- Add the jenkins user to the www-data group.
sudo usermod -a -G jenkins www-data
- Create pull-requests/example.com
sudo mkdir -p /var/www/pull-requests/example.com
- Make the Jenkins user the owner of this directory.
sudo chown -R jenkins:www-data /var/www/pull-requests
- Optionally, preserve the www-data group on this directory.
sudo chmod 2775 /var/www/pull-requests
As always, name the job with underscores, not spaces. For instance: Stage_Example_Pull_Request_Builder
. I like to prefix all job names consistently, based on Environment and Project, so you can set up groups that match on these names in Jenkins easily.
Note: While you are creating and editing the job, it is best to save often, as some of these plugins seem a bit flaky.
- Choose Build a free-style software project, unless you already have a Pull Request job you can clone.
- Under Source Code Management, choose 'Git'
- Enter your github repository URL, the URL you use to clone the repo.
- Click Advanced underneath this field
- Under Refspec, enter
+refs/pull/*:refs/remotes/origin/pr/*
- Branch Specifier should be
${sha1}
- Click Advanced under the branch
- Set Local subdirectory for repo (optional) to
new_pull_request
- Scroll down to Build Triggers
- Click Github pull requests builder
- Under Build Environment, click Inject passwords to the build as environment variables
- Click Add
- Type
GITHUB_TOKEN
under name - Enter your bot user's github token in password. If you need to create one, do the following, replacing
[github-bot-username]
with your github bot username:
curl -u [github-bot-username] https://api.github.com/authorizations -d '{"scopes":["repo"]}'
- Under Build Steps, choose
Execute Shell
- See https://gist.github.com/4236407 for an example script to call.
- If you have the Log Parser plugin, under Post-Build-Actions choose
Console output (build log) parsing
- Choose
Drush
, and mark failed on error, unstable on warning.
Often you'd like to be able to tear down these environments after they are tested and merged. You can create a Jenkins job to handle this for you with minimal effort.
- Create a new job, choosing, Build a free-style software project again.
- Name the job example_com_pull_request_tear_down or something like that. Just try to keep it consistent, mkay? ;)
- Check the This build is parameterized checkbox.
- Click Add Parameter and choose String Parameter.
- Under name specify
GHPRID
- Under Description specify The github pull request ID.
- Click Add build step under Build.
- Choose Execute shell.
- In the shell, do something like the following:
jgd-cleanup -i "$GHPRID" -l "/srv/www/pull-requests/example.com"
- If you have the Log Parser plugin, under Post-Build-Actions choose
Console output (build log) parsing
- Choose
Drush
, and mark failed on error, unstable on warning.