Skip to content

Commit 49e072e

Browse files
committed
Use AWS tools for deployment.
1 parent 3a67590 commit 49e072e

File tree

3 files changed

+187
-121
lines changed

3 files changed

+187
-121
lines changed

.github/workflows/cicd.yml

Lines changed: 96 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,33 @@
33

44
name: CI/CD
55

6+
permissions:
7+
contents: read
8+
69
on:
710
push:
811
branches: ["master"]
912
pull_request:
1013
branches: ["master"]
1114

1215

16+
defaults:
17+
run:
18+
shell: bash
19+
20+
21+
env:
22+
AWS_DEPLOY_ROLE: "arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/ranger-gha-deploy"
23+
AWS_ECR_IMAGE_PREFIX: "/${{ vars.DEPARTMENT_ID }}/"
24+
AWS_ECR_IMAGE_NAME: "${{ vars.PROJECT_ID }}"
25+
AWS_ECS_CLUSTER: ${{ vars.DEPARTMENT_ID }}
26+
AWS_ECS_CONTAINER_NAME: ${{ vars.PROJECT_ID }}
27+
AWS_ECS_SERVICE_STAGING: ${{ vars.PROJECT_ID }}-staging-fg
28+
AWS_ECS_TASK_DEFINITION_ARN_STAGING: "arn:aws:ecs:us-west-2:${{ secrets.AWS_ACCOUNT_ID }}:task-definition/${{ vars.PROJECT_ID }}-staging-fg"
29+
AWS_ECS_TASK_DEFINITION_FAMILY_STAGING: ${{ vars.PROJECT_ID }}-staging-fg
30+
AWS_REGION: us-west-2
31+
32+
1333
jobs:
1434

1535
check-syntax:
@@ -19,7 +39,7 @@ jobs:
1939
timeout-minutes: 5
2040

2141
steps:
22-
- name: Harden CI
42+
- name: Harden Runner
2343
uses: step-security/[email protected]
2444
with:
2545
egress-policy: audit
@@ -44,7 +64,7 @@ jobs:
4464

4565
steps:
4666

47-
- name: Harden CI
67+
- name: Harden Runner
4868
uses: step-security/[email protected]
4969
with:
5070
egress-policy: audit
@@ -68,7 +88,7 @@ jobs:
6888
run: ./bin/build
6989

7090
- name: Save Docker image
71-
run: docker image save ranger-clubhouse-api:dev | gzip -9 > docker_image.tgz
91+
run: docker image save "${{ vars.PROJECT_ID }}:dev" | gzip -9 > docker_image.tgz
7292

7393
- name: Upload Docker image artifacts
7494
uses: actions/upload-artifact@v5
@@ -86,7 +106,7 @@ jobs:
86106

87107
steps:
88108

89-
- name: Harden CI
109+
- name: Harden Runner
90110
uses: step-security/[email protected]
91111
with:
92112
egress-policy: audit
@@ -115,7 +135,7 @@ jobs:
115135

116136
steps:
117137

118-
- name: Harden CI
138+
- name: Harden Runner
119139
uses: step-security/[email protected]
120140
with:
121141
egress-policy: audit
@@ -136,23 +156,26 @@ jobs:
136156

137157

138158
deploy-staging:
139-
name: Deploy code from master branch to the staging environment
159+
name: Deploy to staging
140160

141161
needs: [check-syntax, unit, docker-build, docker-test]
142162
if: github.ref == 'refs/heads/master'
143163

144164
runs-on: ubuntu-latest
145-
timeout-minutes: 5
165+
timeout-minutes: 30
166+
167+
permissions:
168+
id-token: write
146169

147170
steps:
148171

149-
- name: Harden CI
172+
- name: Harden Runner
150173
uses: step-security/[email protected]
151174
with:
152-
egress-policy: audit
153-
154-
- name: Checkout source code
155-
uses: actions/checkout@v5
175+
egress-policy: block
176+
allowed-endpoints: >
177+
*.amazonaws.com:443
178+
*.docker.io:443
156179
157180
- name: Download Docker image artifacts
158181
uses: actions/download-artifact@v6
@@ -162,34 +185,65 @@ jobs:
162185
- name: Load Docker image
163186
run: gzip --uncompress --stdout docker_image.tgz | docker image load
164187

165-
- name: Install Python
166-
uses: actions/setup-python@v6
188+
- name: Configure AWS credentials
189+
uses: aws-actions/configure-aws-credentials@v5
190+
with:
191+
aws-region: ${{ env.AWS_REGION }}
192+
role-to-assume: ${{ env.AWS_DEPLOY_ROLE }}
193+
role-session-name: ranger-gha-${{ github.repository_id }}-${{ github.job }}-${{ github.run_id }}
194+
195+
- name: Login to AWS ECR
196+
id: aws-login-ecr
197+
uses: aws-actions/amazon-ecr-login@v2
198+
199+
- name: Push docker image to Amazon ECR
200+
run: |
201+
image_repository="${{ steps.aws-login-ecr.outputs.registry }}${{ env.AWS_ECR_IMAGE_NAME }}"
202+
docker login
203+
# Push SHA tag
204+
docker tag "${{ vars.PROJECT_ID }}:${{ github.sha }}" "${image_repository}:${{ github.sha }}"
205+
docker push "${image_repository}:${{ github.sha }}"
206+
# Push staging rollback tag
207+
if docker pull "${image_repository}:staging"; then
208+
echo "Pushing rollback tag for staging"
209+
docker tag "${image_repository}:staging" "${image_repository}:staging_rollback"
210+
docker push "${image_repository}:staging_rollback"
211+
fi
212+
# Push staging tag
213+
echo "Pushing staging tag"
214+
docker tag "${image_repository}:${{ github.sha }}" "${image_repository}:staging"
215+
docker push "${image_repository}:staging"
216+
217+
- name: Get current date
218+
id: date
219+
run: echo "date=$(date "+%Y-%m-%dT%H:%M:%S")" >> "${GITHUB_OUTPUT}"
220+
221+
- name: Fetch and update current task definition
222+
id: new-task-definition
223+
uses: aws-actions/amazon-ecs-render-task-definition@v1
224+
with:
225+
task-definition-arn: "${{ env.AWS_ECS_TASK_DEFINITION_ARN_STAGING }}"
226+
task-definition-family: "${{ env.AWS_ECS_TASK_DEFINITION_FAMILY_STAGING }}"
227+
container-name: "${{ env.AWS_ECS_CONTAINER_NAME }}"
228+
image: "${{ steps.aws-login-ecr.outputs.registry }}${{ env.AWS_ECR_IMAGE_NAME }}:${{ github.sha }}"
229+
environment-variables: |
230+
GHA_ACTOR_ID=${{ github.actor_id }}
231+
GHA_ACTOR=${{ github.actor }}
232+
GHA_EVENT_NAME=${{ github.event_name }}
233+
GHA_JOB=${{ github.job }}
234+
GHA_REF=${{ github.ref }}
235+
GHA_RUN_ATTEMPT=${{ github.run_attempt }}
236+
GHA_RUN_ID=${{ github.run_id }}
237+
GHA_RUN_NUMBER=${{ github.run_number }}
238+
GHA_TRIGGERING_ACTOR=${{ github.triggering_actor }}
239+
GHA_WORKFLOW=${{ github.workflow }}
240+
GIT_SHA=${{ github.sha }}
241+
TASK_UPDATED=${{ steps.date.outputs.date }}
242+
243+
- name: Update task definition and ECS service
244+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
167245
with:
168-
python-version: "3.12"
169-
170-
- name: Deploy to staging
171-
run: ./bin/deploy staging
172-
env:
173-
# https://github.com/burningmantech/ranger-secret-clubhouse/settings/secrets
174-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
175-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
176-
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
177-
AWS_ECR_IMAGE_NAME: ${{ secrets.AWS_ECR_IMAGE_NAME }}
178-
AWS_ECS_CLUSTER_STAGING: rangers
179-
AWS_ECS_SERVICE_STAGING: ${{ secrets.AWS_ECS_SERVICE_STAGING }}
180-
NOTIFY_SMTP_HOST: ${{ secrets.NOTIFY_SMTP_HOST }}
181-
NOTIFY_SMTP_USER: ${{ secrets.NOTIFY_SMTP_USER }}
182-
NOTIFY_SMTP_PASSWORD: ${{ secrets.NOTIFY_SMTP_PASSWORD }}
183-
NOTIFY_EMAIL_RECIPIENT: ${{ secrets.NOTIFY_EMAIL_RECIPIENT }}
184-
NOTIFY_EMAIL_SENDER: ${{ secrets.NOTIFY_EMAIL_SENDER }}
185-
CI: true
186-
PROJECT_NAME: Ranger Secret Clubhouse API Service
187-
REPOSITORY_ID: ${{ github.repository }}
188-
BUILD_NUMBER: 0
189-
BUILD_URL: https://github.com/burningmantech/ranger-secret-clubhouse/commit/${{ github.sha }}/checks
190-
COMMIT_ID: ${{ github.event.head_commit.id }}
191-
COMMIT_URL: ${{ github.event.head_commit.url }}
192-
COMMIT_AUTHOR_USER: ${{ github.event.head_commit.author.username }}
193-
COMMIT_AUTHOR_NAME: ${{ github.event.head_commit.author.name }}
194-
COMMIT_AUTHOR_EMAIL: ${{ github.event.head_commit.author.email }}
195-
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
246+
task-definition: "${{ steps.new-task-definition.outputs.task-definition }}"
247+
service: "${{ env.AWS_ECS_SERVICE_STAGING }}"
248+
cluster: "${{ env.AWS_ECS_CLUSTER }}"
249+
wait-for-service-stability: true

0 commit comments

Comments
 (0)