-
Notifications
You must be signed in to change notification settings - Fork 12
183 lines (159 loc) · 7.15 KB
/
test-admin-deploy.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: Deploy test admin environment
on:
pull_request:
branches:
- main
types:
- labeled
- opened
- reopened
- synchronize
env:
AWS_DEFAULT_REGION: ca-central-1
FUNCTION_NAME: "notify-admin-pr"
IMAGE: notify/admin
REGISTRY: 239043911459.dkr.ecr.ca-central-1.amazonaws.com
ROLE_ARN: arn:aws:iam::239043911459:role/notify-admin-pr
jobs:
build-and-push-container:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'Renovate') }}
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@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Configure AWS credentials
id: aws-creds
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
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: Move dockerignore
run: |
mv ci/Dockerfile.lambda.dockerignore .
- name: Build Docker image
run: |
docker build \
--build-arg GIT_SHA=${{ github.sha }} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ env.PR_NUMBER }} \
-f ci/Dockerfile.lambda .
- name: Push Docker image to ECR
run: |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ env.PR_NUMBER }}
- name: Delete old images
run: |
IMAGES_TO_DELETE="$(aws ecr list-images --repository-name $IMAGE --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)"
aws ecr batch-delete-image \
--repository-name $IMAGE \
--image-ids "$IMAGES_TO_DELETE" || true
- name: Logout of Amazon ECR
run: docker logout $REGISTRY
deploy-test-admin:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'Renovate') }}
runs-on: ubuntu-latest
outputs:
LAMBDA_URL: ${{ steps.create-update-lambda.outputs.pr_url }}
needs: build-and-push-container
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@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
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
id: create-update-lambda
run: |
if aws lambda get-function --function-name $FUNCTION_NAME-$PR_NUMBER > /dev/null 2>&1; then
aws lambda update-function-code \
--function-name $FUNCTION_NAME-$PR_NUMBER \
--image-uri $REGISTRY/$IMAGE:$PR_NUMBER > /dev/null 2>&1
else
aws lambda create-function \
--function-name $FUNCTION_NAME-$PR_NUMBER \
--package-type Image \
--role $ROLE_ARN \
--timeout 15 \
--memory-size 1024 \
--code ImageUri=$REGISTRY/$IMAGE:$PR_NUMBER \
--description "$GITHUB_REPOSITORY/pull/$PR_NUMBER" \
--vpc-config SubnetIds=${{ secrets.PR_REVIEW_ENV_SUBNET_IDS }},SecurityGroupIds=${{ secrets.PR_REVIEW_ENV_SECURITY_GROUP_IDS }} > /dev/null 2>&1
aws lambda wait function-active --function-name $FUNCTION_NAME-$PR_NUMBER
aws lambda add-permission \
--function-name $FUNCTION_NAME-$PR_NUMBER \
--statement-id FunctionURLAllowPublicAccess \
--action lambda:InvokeFunctionUrl \
--principal "*" \
--function-url-auth-type NONE > /dev/null 2>&1
URL="$(aws lambda create-function-url-config --function-name $FUNCTION_NAME-$PR_NUMBER --auth-type NONE | jq .FunctionUrl)"
echo "URL=$URL" >> $GITHUB_ENV
aws lambda update-function-configuration \
--function-name $FUNCTION_NAME-$PR_NUMBER \
--environment "Variables={\
NOTIFY_ENVIRONMENT=staging,\
FLASK_APP=application.py,\
IP_GEOLOCATE_SERVICE=False,\
REDIS_ENABLED=True,\
SENDING_DOMAIN=staging.notification.cdssandbox.xyz,\
API_HOST_NAME=https://api.staging.notification.cdssandbox.xyz,\
ADMIN_BASE_URL=$URL,\
AWS_XRAY_SDK_ENABLED=False
}" > /dev/null 2>&1
aws logs create-log-group --log-group-name /aws/lambda/$FUNCTION_NAME-$PR_NUMBER > /dev/null 2>&1
aws logs put-retention-policy --log-group-name /aws/lambda/$FUNCTION_NAME-$PR_NUMBER --retention-in-days 14 > /dev/null 2>&1
fi
aws lambda wait function-updated --function-name $FUNCTION_NAME-$PR_NUMBER
aws lambda put-function-concurrency \
--function-name $FUNCTION_NAME-$PR_NUMBER \
--reserved-concurrent-executions 5
PR_URL=$(aws lambda get-function-url-config --function-name $FUNCTION_NAME-$PR_NUMBER --query 'FunctionUrl' --output text)
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
- 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\n${process.env.URL.slice(1, -1)}`
})
cypress-tests:
runs-on: ubuntu-latest
needs: deploy-test-admin
continue-on-error: true
steps:
- env:
CYPRESS_BASE_URL: ${{needs.deploy-test-admin.outputs.LAMBDA_URL}}
run: echo HEY "$CYPRESS_BASE_URL"
- name: checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3.8.2
with:
node-version: '16.x'
- name: Write the cypress.env.json file
# use quotes around the secret, as its value
# is simply inserted as a string into the command
run: |
echo '${{ secrets.CYPRESS_ENV_JSON }}' > tests_cypress/cypress.env.json
- name: Run the cypress tests
uses: cypress-io/github-action@d79d2d530a66e641eb4a5f227e13bc985c60b964 # v4.2.2
env:
CYPRESS_BASE_URL: ${{needs.deploy-test-admin.outputs.LAMBDA_URL}}
with:
record: false
config: "video=false,screenshotOnRunFailure=false"
build: npx cypress info
working-directory: tests_cypress
spec: |
cypress/e2e/admin/ci.cy.js