-
Notifications
You must be signed in to change notification settings - Fork 36
176 lines (149 loc) · 5.28 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
name: Deploy to ecs
on:
push:
branches:
- develop
- master
tags:
- 'v*'
env:
AWS_ROLE_ARN: arn:aws:iam::887442827229:role/GithubActions_decidim-cfj-cdk-deploy
jobs:
create-app-env:
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
actions: write
contents: read
id-token: write
outputs:
image-tag: ${{ steps.output-app-env.outputs.image-tag }}
eb-environment-name: ${{ steps.output-app-env.outputs.eb-environment-name }}
steps:
- uses: actions/checkout@v3
- name: Set env to staging
id: set-env-staging
if: endsWith(github.ref, 'heads/develop')
run: |
echo "IMAGE_TAG_PREFIX=staging" >> $GITHUB_ENV
echo "EB_ENVIRONMENT_NAME=staging" >> $GITHUB_ENV
- name: Set env to production
id: set-env-production
if: endsWith(github.ref, 'heads/master') || contains(github.ref, 'tags/v')
run: |
echo "IMAGE_TAG_PREFIX=prd-v0265" >> $GITHUB_ENV
echo "EB_ENVIRONMENT_NAME=prd-v0265" >> $GITHUB_ENV
- name: Output App Env
id: output-app-env
run: |
echo "image-tag=${IMAGE_TAG_PREFIX}-${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
echo "eb-environment-name=${EB_ENVIRONMENT_NAME}" >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
timeout-minutes: 1200
needs: create-app-env
permissions:
actions: write
contents: read
id-token: write
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
uses: docker/build-push-action@v2
if: endsWith(github.ref, 'heads/master') || endsWith(github.ref, 'heads/develop')
id: build-image
with:
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.AWS_ECR_REPO_NAME }}:${{ needs.create-app-env.outputs.image-tag }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
if: endsWith(github.ref, 'heads/master') || endsWith(github.ref, 'heads/develop')
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy:
runs-on: ubuntu-latest
timeout-minutes: 1800
if: endsWith(github.ref, 'heads/develop') || contains(github.ref, 'tags/v')
permissions:
actions: write
contents: read
id-token: write
needs:
- create-app-env
- build
steps:
- uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Check if ECR Image exists with tag
if: contains(github.ref, 'tags/v')
env:
IMAGE_TAG: ${{ needs.create-app-env.outputs.image-tag }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO_NAME }}
run: |
EXIT_CODE=0
aws ecr describe-images --repository-name=$ECR_REPOSITORY --image-ids=imageTag=$IMAGE_TAG 2> /dev/null || EXIT_CODE=$?
if [[ $EXIT_CODE != 0 ]]; then
echo "${IMAGE_TAG} image tag not found"
exit 1
fi
- name: Checkout decidim-cfj cdk
uses: actions/checkout@v3
with:
repository: codeforjapan/decidim-cfj-cdk
path: decidim-cfj-cdk
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
run: npm install
working-directory: decidim-cfj-cdk
- name: Install dependencies
run: npm install -g aws-cdk
working-directory: decidim-cfj-cdk
- name: cdk deploy
run: cdk -c stage=$EB_ENVIRONMENT_NAME -c tag=$IMAGE_TAG deploy --all --require-approval never
working-directory: decidim-cfj-cdk
env:
AWS_DEFAULT_REGION: 'ap-northeast-1'
EB_ENVIRONMENT_NAME: ${{ needs.create-app-env.outputs.eb-environment-name }}
IMAGE_TAG: ${{ needs.create-app-env.outputs.image-tag }}