###Automatically deploy code changes from github repository across all EC2 instances behind AWS autoscaling group in a single region.
- You push to github repository (public/private)
- Github webhook URL will be invoked
- Aws autoscaling code deployer will instantly deploy those changes
- That's it. :P
For now I'm assuming the following parameters, please change those depending upon your requirements
- Web server: Apache2
- Document root:
/var/www
- Apache username:
www-data
- General username:
ubuntu
Important
-
It'll work irrespective of the repository type, and I guess in most cases your github repository will be private although it's entirely upto you.
-
Github webhook URL: Use either one of Public DNS/Elastic IP of anyone of the EC2 instance behind autoscaling group or ELB A record or your domain name. Keep in mind that all of those must point to the location
aws_autoscaling_code_deployer/index.php
Public DNS: http://ec2-xxx-xxx-xxx-xxx.region-name.compute.amazonaws.com/path/to/aws_autoscaling_code_deployer/index.php
Elastic IP: http://xxx-xxx-xxx-xxx/path/to/aws_autoscaling_code_deployer/index.php
ELB A record: http://elb_name-xxxxxxxxx.region-name.elb.amazonaws.com/path/to/aws_autoscaling_code_deployer/index.php
Domain name: http://www.example.com/path/to/aws_autoscaling_code_deployer/index.php
Make sure that your repository contains index.php
and config.php
of this repository inside aws_autoscaling_code_deployer
directory
- Specify your AWS credentials and configs in
config.php
-
Access key ID
Example:
AKIAIOSFODNN7EXAMPLE
-
Secret access key:
Example:
wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
-
Region: AWS region where you've setup your autoscaling group
Must be set to one of the following values:
us-east-1, ap-northeast-1, sa-east-1, ap-southeast-1, ap-southeast-2, us-west-2, us-gov-west-1, us-west-1, cn-north-1, eu-west-1
-
Autoscaling group: Name of your autoscaling group
-
Document root: Your webserver document root
Example:
/var/www
- Setup a service hook such that it points to index.php of this repository by navigating to your github repository -> settings -> hooks -> WebHook URLs
Minimum requirements
- Web server
- PHP 5.3.3+ compiled with the cURL extension
- cURL 7.16.2+ compiled with OpenSSL and zlib
- git
Steps to configure AMI
- Change document root ownership to web-server user
sudo chown -R www-data:www-data /var/www
- Login as web-server user
sudo su www-data
- Change directory to web-server home directory
cd ~
- Generate SSH keys
- Create empty repository
git init
- Add a remote origin
git remote add origin git@github.com:username/repository.git
- Download the latest from remote repository without trying to merge or rebase anything
git fetch --all
- Pull
git pull origin master
- Logout of www-data user
- Configure to run a startup script
sudo nano /etc/rc.local
Add
su - www-data -c /etc/init.d/pull > /home/ubuntu/pull.log
before
exit 0
- Create the startup script
sudo nano /etc/init.d/pull
Add the following lines
#!/bin/sh
php /var/www/aws_autoscaling_code_deployer/index.php update
sudo chmod +x /etc/init.d/pull
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
Joy Prakash Sharma