Deployer
Deployer is an automated script built for handling BitBucket's POST
hook deployment process., (*1)
When you put this script to a location accessible to BitBucket, you can set up
a configuration file which matches your environment, then you set up a POST hook
in your repository and everytime you push data in there, the POST hook calls
your instance of Deployer., (*2)
It downloads head revision in a branch you want to observe, then flushes cache
in a specified directory and memcached servers. Deployment report is then sent
via SMTP server to specified inboxes., (*3)
Requirements
- PHP + Apache server
- Read access to your Git repository using pubkey authorization
- Git repository already initialized (= when you enter the repo root and type
"git pull", your repo gets updated)
- You need to have a functional connection between your server and BitBucket
using custom Deployment keys
- Deployer does not use tags for indicating "this commit should go
to the server" - it just pulls the head of branch defined in
DeployConfig::$branch. You have to maintain a branch in which you merge the
revision you want to push to your server. Deployer then checks out
that branch and pulls whatever newest there is.
Security
There is no authentication for POST hooks, anybody can trigger your deployment
script if you don't set up your environment correctly. It won't probably damage
your server (assuming only trusted people have write access to your repository),
but at least it would cause malicious cache flushing and emails delivery, which
is at least annoying, if not harmful to the server speed., (*4)
You should:
- Make sure all .htaccess rules are working (nobody can access subdirectories
within your Deployer directory)
- Extend your .htaccess to allow access only from certain IP addresses
- Make sure the URL which you call is at least /^[a-z0-9]{64}$/i
- Make sure you never insert your production config files to your source
control software
Installation and usage
Following steps assume you are using a domain name of http://example.com and all
above requirements are met., (*5)
Installing via composer
Just add ''"deployer": "dev-master",'' to your composer.json, (*6)
In this case every configuration file and log file is stored inside directory
created by composer. Make sure ''tmp/'' directory is writable by both
webserver user and deployment user., (*7)
Installing manually
Copy all files from the ''src/'' folder somewhere, where your webserver can
serve them via browser., (*8)
In this case every configuration file and log file is stored inside directory
where you placed everything. Make sure ''tmp/'' directory is writable by both
webserver user and deployment user., (*9)
Setup with with webserver user having RW access
You can apply this use case / instructions if you can issue ''git pull'' under
the user which runs your web server (usualy www-data or nobody). That is highly
unusual for a production environment and if you are running such server, keep in
mind that if any attacket gains any significant level of access to your web
server, he could alter files you have there and cause you trouble., (*10)
But - whatever works. If that's your case, go on. If not, skip to the next point., (*11)
Here you go:, (*12)
- Make sure your files are accessible through your browser
(say http://example.com/deployer)
- Copy the "config/config.sample.php" to a configuration file named
''config/config.SOMESECRETSTRING.php'' with the "SOMESECRETSTRING" being
any word of alphanumeric characters between 16 and 128 characters long
(say "config/config.xiRrubWvkXGz8uNi.php").
- Make sure you set your configuration file according to your needs (see the
sample file for more documentation)
- Open http://example.com/deployer/xiRrubWvkXGz8uNi.php and you should see
a deployment report - ending with "Deyploment finished.". You should also
receive an email report at the address you set to your config file.
- Go to https://bitbucket.org/YOURREPOSITORY/admin/hooks and set add
your POST hook URL to http://example.com/deployer/xiRrubWvkXGz8uNi.php
- Make sure you push some data to a branch which you set into
DeployConfig::$gitBranch
- Push data to your repository.
- Sit back, relax and enjoy waiting for the deployment report.
Setup with with webserver user having Read only access
This use case applies to all situations where the webserver user has only read
access to the directory structure of your website., (*13)
That means you either can't issue any git binary commands or even if you did it,
you'd have no chance to write the updated files., (*14)
In this case the Deployer works in two steps:, (*15)
- The POST request to deploy.php registers the POST request into a file in
''tmp/pending/*.json'' directory
- Any CLI call of the deploy.php checks for all the files in
''tmp/pending/*.json'' and executes them.
The key point is that you can run the CLI call via a different user than the
webserver user. You just need to maintain an extra cronjob for that., (*16)
Here you go:, (*17)
- Proceed with steps 1 through 5 in the RW use case (above)
- Make sure to set your DeployConfig::$requireCliExecution to true
- Make sure to set your DeployConfig::$requireUserForDeploy to the user which
needs to be used for successful ''git pull'' execution
- Create a cron job which calls the deploy.php script as often as you wish
- Push data to your repository.
- Sit back, relax and enjoy waiting for the deployment report.
Troubleshooting
- Most errors get logged into ''tmp/log/*.log'' files.
- Errors occurring before the configuration file is identified get logged into
''tmp/log/error.log'' and if there is something major, it gets logged into
your standard error log
- Progress of any deployment with correct deployment job gets logged into a
log file name matching the deploy job.
- If your deployment report didn't arrive, check error logs and try to call
the deployment URL set in your POST hook manually. You will see some output.
- If you don't see any output, check your server's error log and check if you
have error logging enabled.
Potential plans for the future
- Add execution of unit tests, cancel deployment if any test fails
- Add calling of ''composer update'' at the end of deployment cycle
Contribution
Any contribution is welcome as well as issue reporting in this BitBucket
repository. If you find this tool handy, you can buy me a beer when you're in
Prague (Czech Republic)., (*18)
Cheers!, (*19)