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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
pipeline {
agent none
stages {
stage('Compile Tailwind CSS') {
agent {
docker {
image 'node:26.3.1-alpine3.24'
args '-v /tmp:/tmp -v /.npm:/.npm'
}
}
steps {
checkout scm
sh '''
npm install tailwindcss @tailwindcss/cli @tailwindcss/forms
npx tailwindcss -i static/input.css -o static/output.css --minify
'''
stash name: 'tailwindcss-output', includes: 'static/output.css'
}
}
stage('Matrix Build') {
matrix {
axes {
axis {
name 'GOOS'
values 'linux'
}
axis {
name 'GOARCH'
values 'arm64', 'amd64'
}
}
stages {
stage('Build') {
agent {
kubernetes {
defaultContainer 'kaniko'
yaml """
apiVersion: v1
kind: Pod
metadata:
namespace: jenkins
spec:
nodeSelector:
kubernetes.io/arch: ${GOARCH}
containers:
- name: kaniko
image: gcr.io/kaniko-project/executor:v1.24.0-debug
imagePullPolicy: Always
command: [ /busybox/cat ]
tty: true
resources:
requests:
cpu: "1"
memory: 2Gi
ephemeral-storage: 4Gi
limits:
memory: 4Gi
ephemeral-storage: 8Gi
"""
}
}
environment {
IMAGE_PUSH_DESTINATION="registry.sayag.it/sayauz/web"
}
steps {
checkout scm
unstash 'tailwindcss-output'
container(name: 'kaniko', shell: '/busybox/sh') {
withCredentials([string(credentialsId: 'registry-sayagit-jenkins-password', variable: 'REGISTRY_SAYAGIT_JENKINS_PASSWORD')]) {
withEnv(['PATH+EXTRA=/busybox']) {
script {
env.GIT_COMMIT_SHORT = env.GIT_COMMIT ? env.GIT_COMMIT.take(7) : "unknown"
try {
sh '''#!/busybox/sh
mkdir -p /kaniko/.docker
printf '{"auths":{"registry.sayag.it":{"username":"jenkins","password":"%s"}}}' "$REGISTRY_SAYAGIT_JENKINS_PASSWORD" > /kaniko/.docker/config.json
/kaniko/executor --context `pwd` \
--custom-platform=linux/${GOARCH} \
--build-arg TARGETOS=linux \
--build-arg TARGETARCH=${GOARCH} \
--destination $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT-$GOARCH
'''
} catch (Exception e) {
echo "Caught exception: ${e.getMessage()}"
currentBuild.result = 'FAILURE'
throw e
}
}
}
}
}
}
}
}
}
}
stage('Stitch Manifest') {
agent {
kubernetes {
defaultContainer 'manifest-tool'
yaml """
apiVersion: v1
kind: Pod
metadata:
namespace: jenkins
spec:
containers:
- name: manifest-tool
image: mplatform/manifest-tool:alpine-v2.2.2
imagePullPolicy: Always
command: [ 'cat' ]
tty: true
resources:
requests:
cpu: "1"
memory: 2Gi
ephemeral-storage: 4Gi
"""
}
}
environment {
IMAGE_PUSH_DESTINATION="registry.sayag.it/sayauz/web"
}
steps {
script {
env.GIT_COMMIT_SHORT = env.GIT_COMMIT ? env.GIT_COMMIT.take(7) : "unknown"
}
withCredentials([string(credentialsId: 'registry-sayagit-jenkins-password', variable: 'REGISTRY_SAYAGIT_JENKINS_PASSWORD')]) {
sh '''mkdir -p /root/.docker
printf '{"auths":{"registry.sayag.it":{"username":"jenkins","password":"%s"}}}' "$REGISTRY_SAYAGIT_JENKINS_PASSWORD" > /root/.docker/config.json
manifest-tool push from-args \
--platforms linux/amd64,linux/arm64 \
--template $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT-ARCH \
--target $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT
manifest-tool push from-args \
--platforms linux/amd64,linux/arm64 \
--template $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT-ARCH \
--target $IMAGE_PUSH_DESTINATION:latest
if [ "$TAG_NAME" != "" ]; then
manifest-tool push from-args \
--platforms linux/amd64,linux/arm64 \
--template $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT-ARCH \
--target $IMAGE_PUSH_DESTINATION:stable
manifest-tool push from-args \
--platforms linux/amd64,linux/arm64 \
--template $IMAGE_PUSH_DESTINATION:commit-$GIT_COMMIT_SHORT-ARCH \
--target $IMAGE_PUSH_DESTINATION:$TAG_NAME
fi
'''
}
}
}
stage('Deploy (Stage)') {
when { branch 'stage' }
agent {
kubernetes {
defaultContainer 'ansible'
yaml """
apiVersion: v1
kind: Pod
metadata:
namespace: jenkins
spec:
containers:
- name: ansible
image: ghcr.io/ansible/community-ansible-dev-tools:v26.7.2
command: [ 'cat' ]
tty: true
resources:
requests:
cpu: "500m"
memory: 1Gi
"""
}
}
steps {
checkout scm
container('ansible') {
script {
env.GIT_COMMIT_SHORT = env.GIT_COMMIT ? env.GIT_COMMIT.take(7) : "unknown"
def secretVars = [
'sayauz-web-stage-auth-salt' : 'AUTH_SALT',
'sayauz-web-stage-s3-access-key-id' : 'S3_ACCESS_KEY_ID',
'sayauz-web-stage-s3-secret-access-key' : 'S3_SECRET_ACCESS_KEY',
'sayauz-web-stage-mail-salt' : 'MAIL_SALT',
'sayauz-web-stage-mail-host' : 'MAIL_HOST',
'sayauz-web-stage-mail-address' : 'MAIL_ADDRESS',
'sayauz-web-stage-mail-username' : 'MAIL_USERNAME',
'sayauz-web-stage-mail-password' : 'MAIL_PASSWORD'
]
def bindings = secretVars.collect { id, varName -> string(credentialsId: id, variable: varName) }
bindings << sshUserPrivateKey(credentialsId: 'sayauz-svc-jenkins', keyFileVariable: 'SSH_KEY_FILE', usernameVariable: 'SSH_USER')
withCredentials(bindings) {
sh '''
ansible-galaxy collection install community.docker
mkdir -p ~/.ssh
cp "$SSH_KEY_FILE" ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H uz.saya.casa >> ~/.ssh/known_hosts
ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 "$SSH_USER@uz.saya.casa" "echo 'SSH connection successful'"
cd deploy
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
playbook.yml \
-i inventory.yml \
-l stage \
--private-key ~/.ssh/id_ed25519 \
-u "$SSH_USER" \
-e saya_today_web_auth_salt="$AUTH_SALT" \
-e saya_today_web_s3_access_key_id="$S3_ACCESS_KEY_ID" \
-e saya_today_web_s3_secret_access_key="$S3_SECRET_ACCESS_KEY" \
-e saya_today_web_environment=stage \
-e saya_today_web_tag=commit-$GIT_COMMIT_SHORT \
-e saya_today_web_mail_salt="$MAIL_SALT" \
-e saya_today_web_mail_host="$MAIL_HOST" \
-e saya_today_web_mail_address="$MAIL_ADDRESS" \
-e saya_today_web_mail_username="$MAIL_USERNAME" \
-e saya_today_web_mail_password="$MAIL_PASSWORD"
'''
}
}
}
}
}
stage('Deploy (Prod)') {
when { buildingTag() }
agent {
kubernetes {
defaultContainer 'ansible'
yaml """
apiVersion: v1
kind: Pod
metadata:
namespace: jenkins
spec:
containers:
- name: ansible
image: ghcr.io/ansible/community-ansible-dev-tools:v26.7.2
command: [ 'cat' ]
tty: true
resources:
requests:
cpu: "500m"
memory: 1Gi
"""
}
}
steps {
checkout scm
container('ansible') {
script {
def secretVars = [
'sayauz-web-prod-auth-salt' : 'AUTH_SALT',
'sayauz-web-prod-s3-access-key-id' : 'S3_ACCESS_KEY_ID',
'sayauz-web-prod-s3-secret-access-key' : 'S3_SECRET_ACCESS_KEY',
'sayauz-web-prod-mail-salt' : 'MAIL_SALT',
'sayauz-web-prod-mail-host' : 'MAIL_HOST',
'sayauz-web-prod-mail-address' : 'MAIL_ADDRESS',
'sayauz-web-prod-mail-username' : 'MAIL_USERNAME',
'sayauz-web-prod-mail-password' : 'MAIL_PASSWORD',
'sayauz-web-prod-verification-google' : 'GOOGLE_VERIFICATION',
'sayauz-web-prod-verification-yandex' : 'YANDEX_VERIFICATION',
'sayauz-web-prod-verification-bing' : 'BING_VERIFICATION'
]
def bindings = secretVars.collect { id, varName -> string(credentialsId: id, variable: varName) }
bindings << sshUserPrivateKey(credentialsId: 'sayauz-svc-jenkins', keyFileVariable: 'SSH_KEY_FILE', usernameVariable: 'SSH_USER')
withCredentials(bindings) {
sh '''
ansible-galaxy collection install community.docker
mkdir -p ~/.ssh
cp "$SSH_KEY_FILE" ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H uz.saya.casa >> ~/.ssh/known_hosts
ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 "$SSH_USER@uz.saya.casa" "echo 'SSH connection successful'"
cd deploy
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
playbook.yml \
-i inventory.yml \
-l prod \
--private-key ~/.ssh/id_ed25519 \
-u "$SSH_USER" \
-e saya_today_web_auth_salt="$AUTH_SALT" \
-e saya_today_web_s3_access_key_id="$S3_ACCESS_KEY_ID" \
-e saya_today_web_s3_secret_access_key="$S3_SECRET_ACCESS_KEY" \
-e saya_today_web_environment=prod \
-e saya_today_web_tag=$TAG_NAME \
-e saya_today_web_mail_salt="$MAIL_SALT" \
-e saya_today_web_mail_host="$MAIL_HOST" \
-e saya_today_web_mail_address="$MAIL_ADDRESS" \
-e saya_today_web_mail_username="$MAIL_USERNAME" \
-e saya_today_web_mail_password="$MAIL_PASSWORD" \
-e saya_today_google_verification="$GOOGLE_VERIFICATION" \
-e saya_today_yandex_verification="$YANDEX_VERIFICATION" \
-e saya_today_bing_verification="$BING_VERIFICATION"
'''
}
}
}
}
}
}
}
|