This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
155 lines (128 loc) · 5.4 KB
/
ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: github-actions-deploy
on:
push:
branches:
- "master"
pull_request:
types: [ opened, synchronize, reopened, closed ]
env:
PRODUCTION_BRANCH: "master"
REPO_NAME: ${{ github.event.repository.name }}
PULL_REQUEST_NUMBER: ${{ github.event.number }}
GITHUB_ACTION: ${{ github.event.action }}
AWS_REGION: "us-east-1"
SERVICE_NAME: "badges"
REPO_OWNER: "FormidableLabs"
GITHUB_HASH: ${{ github.event.after }}
jobs:
destroy:
if: (github.event_name == 'pull_request') && (github.event.action == 'closed')
runs-on: ubuntu-latest
steps:
- name: Checkout the Repository
uses: actions/checkout@v2
- 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: Set environment variables
run: |
echo "TIER=nonprod" >> $GITHUB_ENV
echo "STAGE=pr$PULL_REQUEST_NUMBER" >> $GITHUB_ENV
- name: Run the install script
run: bash scripts/install.sh
- name: Destroy Terraform
run: terragrunt destroy --auto-approve --terragrunt-non-interactive --terragrunt-working-dir terraform/app
- name: Yarn Serverless Remove
run: yarn sls remove
build:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout the Repository
uses: actions/checkout@v2
- 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: Set environment variables
# https://stackoverflow.com/questions/59882715/use-environment-variable-in-github-action-if
# https://www.techiediaries.com/get-current-branch-name-github-actions-github_ref-bash/
run: |
if [[ $GITHUB_EVENT_NAME != 'pull_request' ]]; then
BRANCH_NAME=$(echo "${GITHUB_REF#refs/heads/}" | sed 's/\//-/')
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
if [[ "${GITHUB_REF#refs/heads/}" == $PRODUCTION_BRANCH ]]; then
TIER="prod"
STAGE="production"
else
TIER="nonprod"
STAGE="pr$PULL_REQUEST_NUMBER"
fi
else
TIER="nonprod"
STAGE="pr$PULL_REQUEST_NUMBER"
fi
echo "TIER=$TIER" >> $GITHUB_ENV
echo "STAGE=$STAGE" >> $GITHUB_ENV
echo "ZIP_FILE=artifact-$REPO_NAME-$TIER-$STAGE.zip" >> $GITHUB_ENV
AWS_ACCOUNT=$(aws sts get-caller-identity | jq -r .Account)
echo "BUCKET_NAME=tf-$REPO_NAME-$TIER-artifacts-$AWS_ACCOUNT" >> $GITHUB_ENV
- name: Run the install script
run: bash scripts/install.sh
- name: Yarn test
run: yarn test
- name: Yarn package
run: yarn sls package
- name: Copy the serverless artifact file
run: cp .serverless/sls-*.zip serverless-artifact.zip
# TODO
# Previously made sure that the CloudFormation stack was in a "ready" state before proceeding
# Made sure that the stack wasn't in a failed state
# Also used to make sure that the current commit was the most recent
# Both of these do not seem very helpful with the move to GitHub Actions
- name: Deploy serverless
run: yarn sls deploy --package .serverless
- name: Apply Terraform
run: |
terragrunt apply --auto-approve --terragrunt-non-interactive --terragrunt-working-dir terraform/app
- name: Create a zip file of artifacts
run: zip $ZIP_FILE scripts terraform/**/*.{tf,hcl} .terraform-version serverless-artifact.zip serverless.yml package.json yarn.lock
- name: Upload Artifact Zip File to S3
run: aws s3 cp $ZIP_FILE s3://$BUCKET_NAME/$ZIP_FILE
- name: Post comment on pull request
uses: actions/github-script@0.9.0
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const endpoint=`https://${process.env.REPO_NAME}-${process.env.TIER}-pr${process.env.PULL_REQUEST_NUMBER}.freetls.fastly.net`
examples = [
'size/github/FormidableLabs/react-fast-compare/master/index.js',
'size/npm/victory/dist/victory.min.js?gzip=true',
'browsers?firefox=20,26&iexplore=!8,-9,10',
'travis/infernojs/inferno/sauce/Havunen?name=InfernoJS',
'sauce/Havunen?labels=none'
]
.map(example => ({ example, url: `${endpoint}/${example}` }))
.map(
({ example, url }) =>
`- [\`${example}\`](${url}) [![example](${url})](${url})`
)
.join('\n');
const output = `## PR Deployment
Deployed PR environment to \`${endpoint}\`
### Examples
${examples}
### Deployment
If it's ready to be deployed to production, merge to the master branch`
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})