Create/destroy staging deploy

Signed-off-by: Jacob Heider <jacob@tea.xyz>
This commit is contained in:
Jacob Heider 2022-05-19 13:34:44 -04:00
parent 8950de1215
commit 7a0de2c56e
Failed to extract signature
9 changed files with 1875 additions and 0 deletions

42
.github/workflows/cleanup.yml vendored Normal file
View file

@ -0,0 +1,42 @@
on:
pull_request:
types:
- closed
jobs:
clean-up:
runs-on: ubuntu-latest
steps:
- name: inject slug/short variables
uses: rlespinasse/github-slug-action@v3.x
- name: set STAGE variable in environment for next steps
run: echo "STAGE=pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}" >> $GITHUB_ENV
- name: checkout the files
uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 14
# there is a bug with the actions/cache used in bahmutov/npm-install@v1 on "closed" event
# more infos here : https://github.com/actions/cache/issues/478
- name: install node dependencies
run: yarn --frozen-lockfile
- name: configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: destroy the stack on AWS
run: yarn destroy
- name: delete the github deployments and the corresponding environment
uses: strumwolf/delete-deployment-environment@v1.1.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
environment: ${{ env.STAGE }}

56
.github/workflows/staging.yml vendored Normal file
View file

@ -0,0 +1,56 @@
on:
pull_request:
concurrency:
group: ${{ github.event.number }}
cancel-in-progress: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: inject slug/short variables
uses: rlespinasse/github-slug-action@v3.x
- name: set STAGE variable in environment for next steps
run: echo "STAGE=pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}" >> $GITHUB_ENV
- name: Create Deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ github.token }}
env: ${{ env.STAGE }}
ref: ${{ github.head_ref }}
no_override: false
transient: true
- uses: actions/checkout@v3
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- uses: actions/setup-node@v3
with:
node-version: 14
- name: install node dependencies
uses: bahmutov/npm-install@v1
- name: deploy the stack on AWS
id: cdk_deploy
run: yarn deploy
- name: Seal Deployment
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ github.token }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env: ${{ env.STAGE }}
env_url: ${{ steps.cdk_deploy.outputs.env_url }}

3
.gitignore vendored
View file

@ -1 +1,4 @@
.DS_Store .DS_Store
node_modules
.envrc
cdk.out

15
bin/app.ts Normal file
View file

@ -0,0 +1,15 @@
import * as cdk from '@aws-cdk/core';
import TeaXYZ from '../lib/tea.xyz';
const app = new cdk.App();
/**
* The name of the stack depends on the STAGE environment variable so we can deploy the infrastructure multiple times in parallel
* @example
* AwesomeStack-pr-1-awesome-branch
* AwesomeStack-production
*/
const stackName = 'TeaXYZ-' + process.env.STAGE;
new TeaXYZ(app, stackName);

6
cdk.json Normal file
View file

@ -0,0 +1,6 @@
{
"app": "yarn ts-node bin/app.ts",
"context": {
"@aws-cdk/core:newStyleStackSynthesis": true
}
}

65
lib/tea.xyz.ts Normal file
View file

@ -0,0 +1,65 @@
import * as cloudfront from "@aws-cdk/aws-cloudfront";
import * as cloudfrontOrigins from "@aws-cdk/aws-cloudfront-origins";
import * as s3 from "@aws-cdk/aws-s3";
import * as lambda from "@aws-cdk/aws-lambda";
import * as s3Deployment from "@aws-cdk/aws-s3-deployment";
import * as cdk from "@aws-cdk/core";
/**
* The CloudFormation stack holding all our resources
*/
export default class TeaXYZ extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
/**
* The S3 Bucket hosting our build
*/
const bucket = new s3.Bucket(this, "Bucket", {
autoDeleteObjects: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const edgeLambda = lambda.Version.fromVersionArn(this, "Lambda", "arn:aws:lambda:us-east-1:640264234305:function:www-redirect:10");
/**
* The CloudFront distribution caching and proxying our requests to our bucket
*/
const distribution = new cloudfront.Distribution(this, "Distribution", {
defaultBehavior: {
origin: new cloudfrontOrigins.S3Origin(bucket),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [
{
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
functionVersion: edgeLambda,
},
],
},
defaultRootObject: "index.html",
errorResponses: [
{
httpStatus: 403,
responsePagePath: "/404.html"
},
],
});
/**
* Output the distribution's url so we can pass it to external systems
*/
new cdk.CfnOutput(this, "DeploymentUrl", {
value: "https://" + distribution.distributionDomainName
});
/**
* Upload our build to the bucket and invalidate the distribution's cache
*/
new s3Deployment.BucketDeployment(this, "BucketDeployment", {
destinationBucket: bucket,
distribution,
distributionPaths: ["/", "/index.html"],
sources: [s3Deployment.Source.asset('./public')],
});
}
}

29
package.json Normal file
View file

@ -0,0 +1,29 @@
{
"name": "deploy-your-pull-requests-to-aws-using-github-actions-and-aws-cdk",
"version": "1.0.0",
"main": "index.js",
"author": "Julien Goux",
"license": "MIT",
"private": true,
"engines": {
"node": ">=14 <15"
},
"scripts": {
"bootstrap": "CDK_NEW_BOOTSTRAP=1 cdk bootstrap --cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess",
"deploy": "cdk deploy \"TeaXYZ-${STAGE}\" --require-approval never --outputs-file cdk.out.json",
"postdeploy": "node --eval \"console.log('::set-output name=env_url::' + require('./cdk.out.json')['TeaXYZ-${STAGE}'].DeploymentUrl)\"",
"destroy": "cdk destroy \"TeaXYZ-${STAGE}\" --force"
},
"devDependencies": {
"@aws-cdk/aws-cloudfront": "1.93.0",
"@aws-cdk/aws-cloudfront-origins": "1.93.0",
"@aws-cdk/aws-s3": "1.93.0",
"@aws-cdk/aws-s3-deployment": "1.93.0",
"@aws-cdk/core": "1.93.0",
"@tsconfig/node14": "1.0.0",
"@types/node": "14.14.34",
"aws-cdk": "1.93.0",
"ts-node": "9.1.1",
"typescript": "4.2.3"
}
}

3
tsconfig.json Normal file
View file

@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node14/tsconfig.json"
}

1656
yarn.lock Normal file

File diff suppressed because it is too large Load diff