-
Notifications
You must be signed in to change notification settings - Fork 0
192 lines (172 loc) · 7.04 KB
/
release-lambda.yaml
File metadata and controls
192 lines (172 loc) · 7.04 KB
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
184
185
186
187
188
189
190
191
192
name: Release-AWS Lambda
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true
on:
workflow_dispatch:
inputs:
WORKFLOW_PHASE:
description: "zappa env setup"
required: true
default: dev
type: choice
options:
- dev
- prod
push:
branches:
- "main"
jobs:
BuildAndDeploy:
runs-on: ubuntu-latest
env:
API_STAGE: ${{ github.event_name == 'workflow_dispatch' && inputs.WORKFLOW_PHASE || 'dev' }}
BUMP_RULE: ${{ (github.event_name == 'workflow_dispatch' && inputs.WORKFLOW_PHASE || 'dev') == 'dev' && '--stage' || '' }}
AWS_ECR_REGISTRY: ${{ github.event_name == 'workflow_dispatch' && secrets.AWS_ECR_PROD_URL || secrets.AWS_ECR_DEV_URL }}
steps:
# Checkout source codes
- name: Checkout source codes
uses: actions/checkout@v4
with:
fetch-depth: 0
# Setup AWS Credentials, Python, uv, docker buildx, and login to ECR.
- name: Setup AWS Credentials
uses: aws-actions/configure-aws-credentials@master
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
ignore-nothing-to-cache: true
- name: Install dependencies
run: uv sync --only-group=deployment
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to ECR
uses: docker/login-action@v3
with:
registry: ${{ env.AWS_ECR_REGISTRY }}
- name: Get current date, repo name and release tag version
id: info
run: |
LATEST_TAG=$(git tag -l --sort=-creatordate | head -n 1)
echo "::set-output name=TAG::$(python ./.github/scripts/get_new_version.py --current=$LATEST_TAG ${{ env.BUMP_RULE }})"
echo "::set-output name=date::$(date +'%Y-%m-%d_%H:%M:%S')"
echo "::set-output name=repository_name::$(echo ${{ github.repository }} | sed -e 's/${{ github.repository_owner }}\///')"
# Build and Push Docker image to ECR
- name: Build and Push Docker image to ECR
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ env.AWS_ECR_REGISTRY }}:${{ steps.info.outputs.TAG }},${{ env.AWS_ECR_REGISTRY }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
file: ./infra/lambda.Dockerfile
platforms: linux/amd64
provenance: false
build-args: |
RELEASE_VERSION=${{ steps.info.outputs.TAG }}
GIT_HASH=${{ github.sha }}
IMAGE_BUILD_DATETIME=${{ steps.info.outputs.date }}
# Create git tag
- name: Create and push git tag
run: |
git tag ${{ steps.info.outputs.TAG }}
git push origin ${{ steps.info.outputs.TAG }}
# Checkout and import zappa config & environment variables from pyconkr-secrets repo
- name: Checkout secrets repo
uses: actions/checkout@v4
with:
repository: ${{ secrets.PYCONKR_SECRET_REPOSITORY }}
ssh-key: ${{ secrets.PYCONKR_SECRET_REPOSITORY_DEPLOY_KEY }}
path: secret_envs
clean: false
sparse-checkout-cone-mode: false
sparse-checkout: |
${{ steps.info.outputs.repository_name }}/zappa_settings.json
${{ steps.info.outputs.repository_name }}/environment_variables.json
- run: mv secret_envs/${{ steps.info.outputs.repository_name }}/*.json ./ && rm -rf secret_envs
# Apply environment variables in environment_variables.json to AWS SSM Parameter Store.
- run: |
python .github/scripts/update_ssm_parameter_store.py \
--project_name ${{ steps.info.outputs.repository_name }} \
--stage ${{ env.API_STAGE }} \
--json_file environment_variables.json
# Zappa update
- name: Zappa Update
run: uv run zappa update ${{ env.API_STAGE }} --docker-image-uri ${{ env.AWS_ECR_REGISTRY }}:${{ steps.info.outputs.TAG }}
- name: Django collect staticfiles
run: uv run zappa manage ${{ env.API_STAGE }} "collectstatic --no-input"
# Django migrate
- name: Django migrate
run: uv run zappa manage ${{ env.API_STAGE }} "migrate --noinput"
# Zappa certify
- name: Zappa Certify
run: uv run zappa certify ${{ env.API_STAGE }} --yes
# Notify to Slack (Success)
- name: Notify deployment to Slack
if: failure() || cancelled()
uses: slackapi/slack-github-action@v1.26.0
with:
channel-id: ${{ vars.SLACK_DEPLOYMENT_ALERT_CHANNEL }}
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.info.outputs.repository_name }} ${{ steps.info.outputs.TAG }} 버전 배포 실패 :rotating_light: (${{ job.status }})",
"emoji": true
}
},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "GitHub Action 바로가기"},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": "${{ github.run_id }}"},
"value": "github_action",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
# Notify to Slack (Failure)
- name: Notify deployment to Slack
uses: slackapi/slack-github-action@v1.26.0
with:
channel-id: ${{ vars.SLACK_DEPLOYMENT_ALERT_CHANNEL }}
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.info.outputs.repository_name }} ${{ steps.info.outputs.TAG }} 버전 배포 성공 :tada:",
"emoji": true
}
},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "GitHub Action 바로가기"},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": "${{ github.run_id }}"},
"value": "github_action",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}