-
Notifications
You must be signed in to change notification settings - Fork 1
160 lines (145 loc) · 5.66 KB
/
aws_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
157
158
159
160
name: AWS Deploy
on:
workflow_call:
inputs:
aws-region:
description: "AWS region to use"
required: true
type: string
cicd-role:
description: "AWS IAM role to assume by GitHub action runner"
required: true
type: string
ecr-repository:
description: "ECR repository to push image to"
required: true
type: string
ecs-cluster-name:
description: "ECS cluster to deploy to"
required: true
type: string
ecs-web-service-name:
description: "ECS service for web"
required: true
type: string
ecs-container-name:
description: "Name of container in ECS task definition"
required: true
type: string
ecs-container-port:
description: "Container port number in ECS task definition"
required: true
type: number
environment:
description: "Environment to deploy to"
required: true
type: string
shortEnv:
description: "Shortened version of the environment"
required: true
type: string
task-definition-path:
description: "Path to task definition file"
required: true
type: string
codedeploy-application:
description: 'CodeDeploy application to use'
required: true
type: string
codedeploy-deployment-group:
description: 'CodeDeploy deployment group to use'
required: true
type: string
path-to-dockerfile:
description: 'path to the dockerfile to use'
required: true
type: string
secrets:
AWS_ACCOUNT_ID:
description: "AWS account ID to deploy to"
required: true
EFS_FILE_SYSTEM_ID:
description: "EFS file system ID to mount"
required: true
DD_API_KEY:
description: "Datadog API key for sending logs"
required: true
RDS_READER_ENDPOINT:
description: "RDS reader endpoint for database connection"
required: false
RDS_DATADOG_PASSWORD:
description: "Password for datadog user inside RDS"
required: false
permissions:
id-token: write
contents: read
jobs:
build:
name: Build and push image to ECR
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ inputs.cicd-role }}
role-session-name: github-action-application-deploy
aws-region: ${{ inputs.aws-region }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and push image to ECR
uses: docker/build-push-action@v4
with:
file: ${{inputs.path-to-dockerfile}}
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr-repository }}:${{inputs.ecs-web-service-name}}-${{ github.sha }}
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ inputs.cicd-role }}
role-session-name: github-action-application-deploy
aws-region: ${{ inputs.aws-region }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Replace variables in task definition file
run: |
sed -i 's/<AWS_ACCOUNT_ID>/${{ secrets.AWS_ACCOUNT_ID }}/g' ${{ inputs.task-definition-path }}
sed -i 's/<EFS_FILE_SYSTEM_ID>/${{ secrets.EFS_FILE_SYSTEM_ID }}/g' ${{ inputs.task-definition-path }}
sed -i 's/<DD_API_KEY>/${{ secrets.DD_API_KEY }}/g' ${{ inputs.task-definition-path }}
sed -i 's/<RDS_READER_ENDPOINT>/${{ secrets.RDS_READER_ENDPOINT }}/g' ${{ inputs.task-definition-path }}
sed -i 's/<RDS_DATADOG_PASSWORD>/${{ secrets.RDS_DATADOG_PASSWORD }}/g' ${{ inputs.task-definition-path }}
sed -i 's/<DD_COMMIT_SHA>/${{ github.sha }}/g' ${{ inputs.task-definition-path }}
- name: Replace variables in appspec
run: |
sed -i 's/<AWS_ACCOUNT_ID>/${{ secrets.AWS_ACCOUNT_ID }}/g' .aws/deploy/appspec.json
sed -i 's/<SHORTENV>/${{ inputs.shortEnv }}/g' .aws/deploy/appspec.json
sed -i 's/<CONTAINER_NAME>/${{ inputs.ecs-container-name }}/g' .aws/deploy/appspec.json
sed -i 's/<CONTAINER_PORT>/${{ inputs.ecs-container-port }}/g' .aws/deploy/appspec.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ inputs.task-definition-path }}
container-name: ${{ inputs.ecs-container-name }}
image: ${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr-repository }}:${{inputs.ecs-web-service-name}}-${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
cluster: ${{ inputs.ecs-cluster-name }}
service: ${{ inputs.ecs-web-service-name }}
wait-for-service-stability: true
codedeploy-appspec: .aws/deploy/appspec.json
codedeploy-application: ${{ inputs.codedeploy-application }}
codedeploy-deployment-group: ${{ inputs.codedeploy-deployment-group }}