Index

Post Commit

post_commit

Summary

Provides an endpoint to your website to use as a webhook for git post commit hooks. When triggered the endpoint will queue a git pull to be executed by cron. This allows you to automate a deployment (plus whatever you want, e.g. Drupal cache clear), whenever you push to your repository. By leveraging cron, you can keep tight permissions and run your git pull as the cron user, not, say, apache. Intended to be used for staging websites, rather than production, where you would want to deploy manually.

Visit https://aklump.github.io/post_commit for full documentation.

Quick Start

  1. Install in your repository root using cloudy pm-install aklump/post_commit
  2. Open bin/config/post_commit.yml and set configuration.
  3. Create the logs directory as configured in the previous step; be sure to ignore this file in SCM.
  4. Open bin/config/post_commit.local.yml and modify as needed; be sure to ignore this file in SCM.
  5. Modify as needed and add bin/auto_deploy.sh to SCM.
  6. Run ./bin/post_commit init to finish installing.
  7. Give write permissions to both owner and group for logs/*, e.g. chmod -R ug+w logs
  8. Set up a cron job to execute runner.php (see below).
  9. Compile the post commit hook url and add it to your github project. Keep the key in the url, do not use the secret textfield. Also you will want to choose the json format. Make sure to use https if you can, a self-signed cert should work fine.

    https://{user}:{password}@{website}/scheduler.php?key={yourprivatekeyhere}
    

GitHub Webhook

Requirements

Installation

The installation script above will generate the following structure where . is your repository root.

.
├── bin
│   ├── post_commit -> ../opt/post_commit/post_commit.sh
│   └── config
│       ├── post_commit.yml
│       └── post_commit.local.yml
├── opt
│   ├── cloudy
│   └── aklump
│       └── post_commit
└── {public web root}

To Update

Configuration Files

Filename Description VCS
post_commit.yml Configuration shared across all server environments: prod, staging, dev yes
post_commit.local.yml Configuration overrides for a single environment; not version controlled. no
scheduler.php This should be pinged by a post commit hook on the origin repo via https and including the secret key. yes

Usage

Contributing

If you find this project useful... please consider making a donation.

Testing

  1. Place the url you've created in a browser and look for output.
  2. If the screen is white, look in your server error logs.

You can test a single repository by appending the following to the url, for the purposes of testing.

&repo=jquery.slim_time

You can trigger a single branch response by appending the following to the url, for the purposes of testing.

&branch=master

runner.php

This should be the target of a frequent cron job being run by the same user that owns the website files and .git folder. This script processes any orders that were scheduled by scheduler.php. You can redirect the output to logs/cron.txt file during setup, and once all is working you should consider doing like the second example which does not capture the output.

* * * * * /usr/bin/php /home/user/mysite/opt/post_commit/runner.php >> /home/user/mysite/opt/post_commit/logs/cron.txt

Example 2, no log of cron jobs, better once all is working correctly. You might want to delete logs/cron.txt at this point.

* * * * * /usr/bin/php /home/user/mysite/opt/post_commit/runner.php > /dev/null

orders.txt

You can truncate all text files without messing up perms using reset.php.

A log of incoming orders post commit hooks.

complete.txt

A log of runner.php.

pending.txt

The commands that have not yet been executed by cron. This file should get emptied at each cron run.

To dump all logs and cancel standing orders

Don't just delete the log files as permissions are bit tricky; instead use the reset script.

php reset.php

Troubleshooting

Jobs get scheduled but not run?

  1. Check to make sure the the logs directory and all log files are owned by the cron user and the group is the php user and that both user and group has rw permissions.

  2. Does the git repo require a passphrase for pulling? You will have to disable that.

  3. Try logging in to the server as the cron user and running the command.

  4. Make sure you can git pull manually using your ssh keys, etc.