-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (135 loc) · 4.93 KB
/
deploy.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
156
name: Deploy Application
on:
push:
branches: [main]
paths:
- '.github/workflows/deploy.yml'
- '.github/workflows/push-container.yml'
- 'src/**'
- 'container/**'
- 'template.yml'
- 'package.json'
workflow_dispatch:
env:
AWS_REGISTRY_URI: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.us-east-1.amazonaws.com
AWS_REGION: us-east-1
AWS_ROLE_TO_ASSUME: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/coderunner-pipeline-role
REPOSITORY_NAME: coderunner
CLOUDFORMATION_ROLE_ARN: arn:aws:iam::254837279431:role/coderunner-cloudformation-role
CODERUNNER_BUCKET: coderunner-${{ secrets.AWS_ACCOUNT_ID }}
jobs:
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
- uses: actions/checkout@v4
- name: Run tests
run: |
npm ci
npm run test
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
directory: ./tests/coverage/
token: ${{ secrets.CODECOV_TOKEN }}
push-container:
needs: [unit-test]
uses: ./.github/workflows/push-container.yml
secrets:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
deploy:
runs-on: ubuntu-latest
needs: [push-container]
steps:
- uses: actions/checkout@v4
- uses: aws-actions/setup-sam@v2
with:
use-installer: true
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Set REPOSITORY_URI
run: |
REPOSITORY_URI=$AWS_REGISTRY_URI/$REPOSITORY_NAME
echo "REPOSITORY_URI=$REPOSITORY_URI" >> "$GITHUB_ENV"
- name: Remove files and folders
run: |
rm -rf container scripts tests
rm -rf -v $(find . -maxdepth 1 -type f ! -name 'package.json' ! -name 'package-lock.json' ! -name 'template.yml')
- name: Install dependencies
run: |
rm package-lock.json
docker run \
-v $(pwd):/var/task \
-w /var/task \
--entrypoint /bin/bash \
amazon/aws-lambda-nodejs:20 \
-c "npm i --omit=dev"
- name: Build and Upload Package
run: |
zip --symlinks -r package.zip ./*
aws s3 cp package.zip s3://${{ env.CODERUNNER_BUCKET }}/package/${{ github.run_id }}.zip
- name: Deploy template.yml
run: |
sam deploy --stack-name coderunner-dev \
--no-fail-on-empty-changeset \
--s3-bucket ${{ env.CODERUNNER_BUCKET }} \
--image-repositories CodeRunnerFunction=$REPOSITORY_URI \
--capabilities CAPABILITY_IAM \
--role-arn ${CLOUDFORMATION_ROLE_ARN} \
--parameter-overrides \
ImageUri=${{ env.REPOSITORY_URI }}:${{ github.run_id }} \
CodeUri=package/${{ github.run_id }}.zip
migrations:
runs-on: ubuntu-latest
needs: [deploy]
steps:
- uses: aws-actions/setup-sam@v2
with:
use-installer: true
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Run migrations
run: |
aws lambda invoke \
--function-name "MigrationsFunction" \
--cli-binary-format raw-in-base64-out \
-
delete-outdated-image:
runs-on: ubuntu-latest
needs: [deploy]
steps:
- uses: aws-actions/setup-sam@v2
with:
use-installer: true
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Delete images from ECR Repository
run: |
IMAGE_ID=$(aws ecr describe-images \
--repository-name $REPOSITORY_NAME \
--query "sort_by(imageDetails, &imagePushedAt) | reverse(@) | [1:].{imageId:imageTags[0]}")
for row in $(echo "${IMAGE_ID}" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
aws ecr batch-delete-image \
--repository-name $REPOSITORY_NAME \
--image-ids imageTag=$(_jq '.imageId')
done