Skip to content

Commit

Permalink
Initial extraction from encrypted.dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dvassallo committed Jun 17, 2019
0 parents commit 622ea48
Show file tree
Hide file tree
Showing 27 changed files with 12,811 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .babelrc
@@ -0,0 +1,11 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/transform-runtime"
]
]
}
24 changes: 24 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,24 @@
module.exports = {
"env": {
"browser": true,
"amd": true,
"node": true,
"jest": true,
"es6": true
},
"plugins": ["react"],
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"rules": {
"no-console": "off",
"semi": [2, "never"]
},
"parser": "babel-eslint",
"settings": {
"react": {
"version": "detect",
}
}
}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
dist
build
coverage
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Daniel Vassallo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
98 changes: 98 additions & 0 deletions README.md
@@ -0,0 +1,98 @@
# A GitHub-to-EC2 CI/CD Pipeline in 7 Clicks
This is a simple template for creating a CI/CD pipeline that automatically picks up new commits from GitHub, builds them with AWS CodeBuild, and deploys them to EC2 using AWS CodeDeploy. I've extracted this from [a project I'm working on](https://twitter.com/dvassallo/status/1126925437790081024).

You can try this in your AWS account in about 7 clicks. The final result will be a fully working Node.js demo app that gets built and deployed to an EC2 instance in your account, accessible via HTTPS using a [Let's Encrypt](https://letsencrypt.org) certificate.

### Setup From the Console

1. Fork this repo into your GitHub account, or create a new repo using this one [as a template](https://help.github.com/en/articles/creating-a-repository-from-a-template).

2. Create a new [GitHub Personal Access Token](https://github.com/settings/tokens) to let AWS read your GitHub repos and set up a webhook. Give it full "repo" and "admin:repo_hook" permissions.

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/github-permissions.png)

3. Click the button below to start setting up all the AWS resources in your account.

[![Launch Stack](https://cdn.rawgit.com/buildkite/cloudformation-launch-stack-button-svg/master/launch-stack.svg)](https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=MyApp&templateURL=https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/infra.yml)

At the end of this setup you will end up with an EC2 instance, an S3 bucket, and a CodePipeline using CodeBuild and CodeDeploy. You will be able to easily tear down all resources by deleting the CloudFormation stack. Only the S3 bucket needs to be deleted manually. You can find the full template here: [deploy/infra.yml](deploy/infra.yml).

4. Fill in all the required parameters.

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/cfn-create-stack.png)

4. Wait for the CloudFormation stack to finish and find the output URL.

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/stack-output.png)

5. Click on the output URL to see the demo web app.

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/app.png)

6. You can now find your new CI/CD pipeline in the [CodePipeline console](https://console.aws.amazon.com/codesuite/codepipeline/pipelines).

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/pipeline.png)

7. Now when you push a new change to GitHub, the pipeline will pick it up, build it, and deploy it. The demo app takes about 3 minutes to go through the pipeline.

![](https://dvassallo.s3-us-west-2.amazonaws.com/github-to-ec2-pipeline/pipeline-history.png)

### Setup From the AWS CLI

1. Go through steps 1 and 2 from [the console instructions](#setup-from-the-console).

2. Put your GitHub Personal Access Token in `~/.github/access-token`:

```
mkdir -p ~/.github/access-token
ehco "<YOUR TOKEN" > ~/.github/access-token
```

3. Checkout your repo:

```
git clone https://github.com/<YOUR GIHUB USERNAME>/github-to-ec2-pipeline.git
```

4. Run the CloudFormation script using the [AWS CLI](https://aws.amazon.com/cli/), and then follow steps 4 to 7 from [the console instructions](#setup-from-the-console):

```
aws cloudformation deploy \
--region us-west-2 \
--stack-name MyApp \
--template-file ./deploy/infra.yml \
--no-fail-on-empty-changeset \
--parameter-overrides \
GitHubOwner=dvassallo \
GitHubRepo=github-to-ec2-pipeline \
GitHubBranch=master \
GitHubPersonalAccessToken=$(cat ~/.github/access-token) \
EC2InstanceType=t3.micro \
EC2KeyPair=dvassallo \
EC2VPC=vpc-279a8c42 \
EC2Subnet=subnet-533dd837 \
Domain=vassallo.io \
--capabilities CAPABILITY_NAMED_IAM
```

These are all the available options for `parameter-overrides`:

#### GitHub Configuration
* `GitHubOwner`: The username of the source GitHub repo.
* `GitHubRepo`: The source GitHub repo name (without the username).
* `GitHubBranch`: The source GitHub branch. Default: master.
* `GitHubPersonalAccessToken`: Your GitHub personal access token to let AWS read your repos and set up a webhook.

#### EC2 Configuration
* `EC2InstanceType`: The staging host EC2 instance type. Only tested on x86_64. Default: t3.medium.
* `EC2AMI`: The EC2 AMI. Only tested on Amazon Linux 2. Default: The latest Amazon Linux 2 AMI in the region.
* `EC2KeyPair`: [Optional] An existing EC2 keypair to be able to ssh to your staging host.
* `EC2VPC`: The VPC where to run the staging host.
* `EC2Subnet`: The subnet for the staging host.

#### HTTPS Configuration
* `Domain`: [Optional] Your root domain name (Example: example.com). HTTPS will only be enabled if a domain is specified. Only provide this if your DNS is managed by Route 53.

## License

This project is released under the [MIT License](LICENSE).
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
@@ -0,0 +1 @@
module.exports = 'test-file-stub';
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
@@ -0,0 +1 @@
module.exports = {};
28 changes: 28 additions & 0 deletions appspec.yml
@@ -0,0 +1,28 @@
version: 0.0
os: linux
files:
# unzip the build artifact in ~/app
- source: /
destination: /home/ec2-user/app
permissions:
# change permissions from root to ec2-user
- object: /home/ec2-user/app
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
AfterInstall:
# copy the let's encrypt certs from their default dir to the app dir
- location: deploy/install-cert.sh
timeout: 60
runas: root
ApplicationStart:
# start the application
- location: deploy/start-service.sh
timeout: 300
runas: ec2-user
ApplicationStop:
# stop the application
- location: deploy/stop-service.sh
timeout: 300
runas: ec2-user
28 changes: 28 additions & 0 deletions buildspec.yml
@@ -0,0 +1,28 @@
version: 0.2

phases:
install:
runtime-versions:
nodejs: 10
commands:
# uninstall Node.js v10 from CodeBuild's image
- yes | n uninstall

# install Node.js v12
- curl -sL https://deb.nodesource.com/setup_12.x | bash -
- apt-get install -y nodejs
pre_build:
commands:
# run npm install using versions in package-lock.json
- npm ci
build:
commands:
# run the test suite
- npm test

# run the build
- npm run build
artifacts:
files:
# bundle everything as a build artifact, except hidden files
- '**/*'

0 comments on commit 622ea48

Please sign in to comment.