-
Notifications
You must be signed in to change notification settings - Fork 0
181 lines (156 loc) · 5.38 KB
/
ci-cd.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
177
178
179
180
181
name: CI/CD
on:
push:
branches:
- '**'
tags-ignore:
- '**'
workflow_dispatch:
permissions:
id-token: write
contents: write
packages: write
env:
AWS_REGION: us-east-1
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: '.python-version'
- name: Install dependencies
working-directory: app
shell: bash
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run pytest
working-directory: app
shell: bash
run: pytest test.py
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Build container
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
context: app
push: false
tags: ${{ github.sha }}
semantic-release:
name: 'Semantic Release'
needs: [ tests, build ]
# We run semantic-release only on merges to main branch
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
# To avoid race conditions, this makes sure semantic-release is async
concurrency: 'semantic-release'
outputs:
new_release_published: ${{ steps.semantic.outputs.new_release_published }}
new_release_git_tag: ${{ steps.semantic.outputs.new_release_git_tag }}
steps:
- name: 'Checkout'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
persist-credentials: false
- name: 'Setup Node'
uses: actions/setup-node@v4.1.0
with:
node-version: 18.15.0
- name: Semantic bot token
id: bot-token
uses: getsentry/action-github-app-token@v3.0.0
with:
app_id: ${{ secrets.SEMANTIC_APP_ID }}
private_key: ${{ secrets.SEMANTIC_APP_PRIVATE_KEY }}
- name: Semantic Release
id: semantic
uses: cycjimmy/semantic-release-action@v4.1.0
with:
semantic_version: 19.0.5
extra_plugins: |
conventional-changelog-conventionalcommits@5.0.0
env:
GITHUB_TOKEN: ${{ steps.bot-token.outputs.token }}
publish:
if: needs.semantic-release.outputs.new_release_published == 'true'
needs: semantic-release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.GHA_ASSUME_ROLE }}
- name: AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.ECR_ROLE }}
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ env.AWS_SESSION_TOKEN }}
role-skip-session-tagging: true
role-duration-seconds: 3000
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build container
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
context: app
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ vars.ECR_REPO }}:${{ needs.semantic-release.outputs.new_release_git_tag }}
update-repo:
needs: [publish, semantic-release]
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
persist-credentials: false
- name: Semantic bot token
id: bot-token
uses: getsentry/action-github-app-token@v3.0.0
with:
app_id: ${{ secrets.SEMANTIC_APP_ID }}
private_key: ${{ secrets.SEMANTIC_APP_PRIVATE_KEY }}
- name: Update image tag in terraform.tfvars
working-directory: terraform
run: |
cp terraform.tfvars terraform.tfvars.bak
echo 'latest_image_tag = "${{ needs.semantic-release.outputs.new_release_git_tag }}"' > terraform.tfvars
cat terraform.tfvars.bak | tail -n+2 >> terraform.tfvars
- name: Setup Git User
run: |
# Extract commit information
AUTHOR_NAME=$(git show -s --format='%an' ${{ github.event.inputs.commit }})
AUTHOR_EMAIL=$(git show -s --format='%ae' ${{ github.event.inputs.commit }})
git config --local user.email "${AUTHOR_EMAIL}"
git config --local user.name "${AUTHOR_NAME}"
- name: Commit files
run: |
git pull origin main
git add terraform/terraform.tfvars
git commit -m "chore: update image tag to ${{ needs.semantic-release.outputs.new_release_git_tag }} [skip ci]"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ steps.bot-token.outputs.token }}
branch: main