-
Notifications
You must be signed in to change notification settings - Fork 0
240 lines (212 loc) · 7.49 KB
/
k3s-prod.yaml
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
name: Spring Boot Multi-module K3s Deployment
on:
push:
branches:
- develop
env:
REGISTRY: "ghcr.io"
NAMESPACE: "depromeet"
IMAGE_NAME: "kasukabe-server"
jobs:
setup:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
deploy_target: ${{ steps.set-env.outputs.DEPLOY_TARGET }}
sha_short: ${{ steps.sha_short.outputs.sha_short }}
changed_modules: ${{ steps.detect-changes.outputs.changed_modules }}
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Setup Env
id: set-env
run: |
if [[ "${GITHUB_REF}" == "refs/heads/develop" ]]; then
echo "DEPLOY_TARGET=production" >> $GITHUB_OUTPUT
else
echo "DEPLOY_TARGET=development" >> $GITHUB_OUTPUT
fi
- name: Get short SHA
id: sha_short
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Detect Changed Modules
id: detect-changes
run: |
CHANGED_MODULES=""
if [[ $(git diff --name-only HEAD^ HEAD) =~ ^layer-api/ ]]; then
if [ -z "$CHANGED_MODULES" ]; then
CHANGED_MODULES="layer-api"
else
CHANGED_MODULES="$CHANGED_MODULES,layer-api"
fi
fi
if [[ $(git diff --name-only HEAD^ HEAD) =~ ^layer-batch/ ]]; then
if [ -z "$CHANGED_MODULES" ]; then
CHANGED_MODULES="layer-batch"
else
CHANGED_MODULES="$CHANGED_MODULES,layer-batch"
fi
fi
echo "changed_modules=$CHANGED_MODULES"
echo "changed_modules=$CHANGED_MODULES" >> $GITHUB_OUTPUT
build:
name: build
needs: [ setup ]
runs-on: ubuntu-latest
# if: ${{ needs.setup.outputs.changed_modules != '' }}
permissions:
contents: read
packages: write
env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
APPLICATION_SECRET_PROPERTIES: ${{ secrets.APPLICATION_SECRET_PROPERTIES }}
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
SHA_SHORT: ${{ needs.setup.outputs.sha_short }}
CHANGED_MODULES: ${{ needs.setup.outputs.changed_modules }}
steps:
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Checkout sources
uses: actions/checkout@v4
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Setup Gradle
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
# - name: Create application-secret.properties
# run: |
# for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
# echo "${APPLICATION_SECRET_PROPERTIES}" > ./$MODULE/src/main/resources/application-secret.properties
# done
- name: Build Changed Modules
run: |
for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
./gradlew :$MODULE:build
done
- name: Run Tests for Changed Modules
run: |
for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
./gradlew :$MODULE:test
done
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}/$MODULE
- name: Push Docker Images for Changed Modules
run: |
for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
IMAGE_TAG="${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}/$MODULE:${{ env.SHA_SHORT }}"
docker build ./$MODULE -t $IMAGE_TAG
docker push $IMAGE_TAG
done
deploy:
name: Deploy to K3s
needs: [ build, setup ]
runs-on: ubuntu-latest
# if: ${{ needs.setup.outputs.changed_modules != '' }}
env:
DEPLOY_TARGET: ${{ needs.setup.outputs.deploy_target }}
SHA_SHORT: ${{ needs.setup.outputs.sha_short }}
CHANGED_MODULES: ${{ needs.setup.outputs.changed_modules }}
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install kubectl
uses: azure/setup-kubectl@v1
with:
version: 'v1.21.0'
- name: Set up kubeconfig
run: |
mkdir -p $HOME/.kube
echo "${{ secrets.KUBE_CONFIG }}" > $HOME/.kube/config
chmod 600 $HOME/.kube/config
- name: Create K3s deployment files
run: |
for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
cat <<EOF > deployment-$MODULE.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: $MODULE-production
namespace: default
labels:
app: $MODULE-production
spec:
replicas: 1
selector:
matchLabels:
app: $MODULE-production
template:
metadata:
labels:
app: $MODULE-production
spec:
containers:
- name: $MODULE-production
image: ${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}/$MODULE:${{ env.SHA_SHORT }}
ports:
- containerPort: $PORT
env:
- name: TZ
value: "Asia/Seoul"
- name: SPRING_PROFILES_ACTIVE
value: "${{ env.DEPLOY_TARGET }}"
volumeMounts:
- name: secret-volume
mountPath: /config/application-secret.properties
subPath: application-secret.properties
- name: log-volume
mountPath: /log
- name: tokens-volume
mountPath: /config/tokens
volumes:
- name: secret-volume
secret:
secretName: app-secret-dev
- name: log-volume
hostPath:
path: /${{ env.DEPLOY_TARGET }}/log/$MODULE
type: DirectoryOrCreate
- name: tokens-volume
emptyDir: { }
---
apiVersion: v1
kind: Service
metadata:
name: $MODULE-service-production
namespace: default
labels:
app: $MODULE-production
spec:
selector:
app: $MODULE-production
ports:
- protocol: TCP
port: 80
targetPort: $PORT
EOF
fi
done
- name: Deploy to K3s
run: |
for MODULE in $(echo $CHANGED_MODULES | tr "," "\n"); do
kubectl apply -f deployment-$MODULE.yaml
done