AWS DevOps Blog

Automatically Deploy from GitHub Using AWS CodeDeploy

(This post has been updated on October 1, 2018 to reflect the deprecation of GitHub services. You can learn more about this deprecation here. We now recommend setting up automatic deployments from GitHub using AWS CodePipeline and AWS CodeDeploy.)

AWS CodeDeploy is a service that makes it easy to deploy application updates to Amazon EC2 instances. CodeDeploy is targeted at customers who manage their EC2 instances directly, instead of those who use an application management service like AWS Elastic Beanstalk or AWS OpsWorks that have their own built-in deployment features. CodeDeploy allows developers and administrators to centrally control and track their application deployments across their different development, testing, and production environments.

GitHub is a popular code management and developer collaboration tool. By connecting GitHub to CodeDeploy, you can set up an end-to-end pipeline to move your code changes from source control to your testing or production environments. The remainder of this post walks through the steps required to set up automatic deployments from GitHub to CodeDeploy.

Setting Up the Prerequisites

To start with, we’ll assume that you already have an application set up in CodeDeploy that’s successfully deploying to a set of EC2 instances. You can learn about the steps required to do this in our User Guide. To get started quickly, you can also create a sample deployment to a set of test instances through our Getting Started Wizard in the console. The steps below will use this getting started sample application, but you can also translate these actions to your own application.

Moving Your Application Into GitHub

If the application files that you want to deploy are not already in a GitHub repository, you’ll need to set that up. Here’s how you can do it with the getting started sample application. First, download the application files. These examples use Linux / Unix commands.

mkdir codedeploy-sample
cd codedeploy-sample
curl -O http://s3.amazonaws.com/aws-codedeploy-us-east-1/samples/latest/SampleApp_Linux.zip
unzip SampleApp_Linux.zip
rm SampleApp_Linux.zip

Next, you need to create a repository on GitHub to store these application files. If you need help, you can read the GitHub documentation. Your repository can be public or private. After the GitHub repository is created, you’ll push your local application files to it.

git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:YOUR_USERNAME/YOUR_REPOSITORY.git
git push -u origin master

Manually Deploying Application Files from GitHub

Once your application files are in GitHub, you can configure CodeDeploy to pull the application bundle directly from the GitHub repository, rather than from Amazon S3. Let’s trigger a deployment from your GitHub repository using the AWS Management Console. From the Deployments page, click Create New Deployment. Select the name of your application, the target deployment group, and GitHub for the revision type. You should then see a Connect to GitHub section.

Click Connect With GitHub, and then step through the OAuth process. A few different things might happen next. First, if you are not logged into GitHub in your browser, you will be asked to log in. Next, if you haven’t already granted AWS CodeDeploy access to your GitHub repositories, you will be asked to authorize that now. Once this is done, you’ll return to the AWS Management Console and CodeDeploy will have the permissions required to access your repository. All that’s left is to fill in the Repository Name and Commit ID. The repository name will be in the format “GITHUB_USERNAME/REPOSITORY_NAME”. The commit ID will be the full SHA (a 40-digit hex number) that can be copied through the GitHub UI. You can find this information from the commit history page of your repository.

Click Deploy Now, and then monitor the deployment in the console to ensure that it succeeds. After you’re sure this is set up correctly, you can proceed with configuring the automatic deployment from GitHub.

Creating a Continuous Delivery Pipeline using GitHub, AWS CodePipeline, and AWS CodeDeploy

A good solution for deploying from GitHub is by creating a continuous delivery pipeline with AWS CodePipeline and then deploying with AWS CodeDeploy. CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. You can automate your release workflow by creating a new pipeline in CodePipeline that connects to your GitHub repository via a webhook. You can configure a webhook from GitHub via the AWS CodePipeline console, AWS Command Line Interface, or CodePipeline API.

To get started, create a CodePipeline pipeline starting from step 2 of this tutorial. Be sure to replace the source action steps related to Amazon S3 with GitHub source action.

Note: If you previously used GitHub Services to deploy to CodeDeploy, you can configure your CodeDeploy action in CodePipeline using the existing CodeDeploy Application and Deployment Group you used with GitHub Services. You should not need to re-configure your current CodeDeploy setup

CodePipeline allows you to scale your deployment process to include build steps that compile and package your application, test steps to validate that your code doesn’t break existing features, deployment stages to deploy and test your code in pre-production environments, and manual approvals to require human sign-off before a change moves into production. You can learn more about CodePipeline features and pricing by visiting the homepage.

Going Further

To dive deeper into the AWS CodeDeploy service, you can find links to documentation, tutorials, and samples on our Developer Resources page. We’d love to hear your feedback and suggestions for the service, so please reach out to the product team through our forum.