-
Notifications
You must be signed in to change notification settings - Fork 29
100 lines (85 loc) · 4.12 KB
/
deploy-staging.yaml
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
name: Staging review apps
on:
- pull_request
env:
FUNCTION_NAME: "website-staging"
GITHUB_SHA: ${{ github.sha }}
IMAGE: website-staging
REGISTRY: 773858180673.dkr.ecr.ca-central-1.amazonaws.com
ROLE_ARN: arn:aws:iam::773858180673:role/website-staging-execution-role
jobs:
build-and-push-staging-container:
runs-on: ubuntu-latest
steps:
- name: Set envs
run: echo "PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- name: Build container
run: |
docker build -t $REGISTRY/$IMAGE:$PR_NUMBER .
- name: Configure AWS credentials
id: aws-creds
uses: aws-actions/configure-aws-credentials@50ac8dd1e1b10d09dac7b8727528b91bed831ac0 # v3.0.2
with:
aws-access-key-id: ${{ secrets.STAGING_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.STAGING_AWS_SECRET_ACCESS_KEY }}
aws-region: ca-central-1
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@5a88a04c91d5c6f97aae0d9be790e64d9b1d47b7 # v1.7.1
- name: Push containers to ECR
run: |
docker push $REGISTRY/$IMAGE:$PR_NUMBER
- name: Delete old images
run: |
IMAGES_TO_DELETE=$( aws ecr list-images --region ca-central-1 --repository-name $IMAGE --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json )
aws ecr batch-delete-image --region ca-central-1 --repository-name $IMAGE --image-ids "$IMAGES_TO_DELETE" || true
- name: Logout of Amazon ECR
run: docker logout $REGISTRY
deploy-staging-images:
needs: build-and-push-staging-container
strategy:
matrix:
lang: ["fr", "en"]
runs-on: ubuntu-latest
steps:
- name: Set envs
run: echo "PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")" >> $GITHUB_ENV
- name: Configure AWS credentials
id: aws-creds
uses: aws-actions/configure-aws-credentials@50ac8dd1e1b10d09dac7b8727528b91bed831ac0 # v3.0.2
with:
aws-access-key-id: ${{ secrets.STAGING_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.STAGING_AWS_SECRET_ACCESS_KEY }}
aws-region: ca-central-1
- name: Create/Update lambda function
run: |
if aws lambda get-function --function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }} > /dev/null 2>&1; then
aws lambda update-function-code \
--function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }} \
--image-uri $REGISTRY/$IMAGE:$PR_NUMBER > /dev/null 2>&1
else
aws lambda create-function \
--function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }} \
--package-type Image \
--role $ROLE_ARN \
--code ImageUri=$REGISTRY/$IMAGE:$PR_NUMBER \
--environment Variables={CONTENT_DIR=/var/www/html/${{ matrix.lang }}} \
--description "$GITHUB_REPOSITORY/pull/$PR_NUMBER"
aws lambda wait function-active --function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }}
echo URL=$(aws lambda create-function-url-config --function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }} --auth-type NONE | jq .FunctionUrl) >> $GITHUB_ENV
aws lambda add-permission --function-name $FUNCTION_NAME-$PR_NUMBER-${{ matrix.lang }} --statement-id FunctionURLAllowPublicAccess --action lambda:InvokeFunctionUrl --principal "*" --function-url-auth-type NONE > /dev/null 2>&1
fi
- name: Update PR
if: env.URL != ''
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## :test_tube: Review environment (${{ matrix.lang }})\n${process.env.URL.slice(1, -1)}`
})