-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
156 lines (141 loc) · 4.02 KB
/
Jenkinsfile
File metadata and controls
156 lines (141 loc) · 4.02 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
pipeline {
agent {
kubernetes {
label 'docker'
defaultContainer 'docker'
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: docker
image: docker:27-cli
command: ["cat"]
tty: true
env:
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: DOCKER_TLS_CERTDIR
value: ""
- name: dind
image: docker:27-dind
securityContext:
privileged: true
env:
- name: DOCKER_TLS_CERTDIR
value: ""
args:
- --host=tcp://0.0.0.0:2375
- --host=unix:///var/run/docker.sock
"""
}
}
environment {
DISCORD_WEBHOOK = credentials('discord-webhook')
ORG = "checkmateit"
REGISTRY = "ghcr.io/${ORG}"
IMAGE_TAG = "${env.BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Docker test') {
steps {
sh '''
echo "Waiting for dind..."
for i in $(seq 1 60); do
if docker info >/dev/null 2>&1; then
echo "Docker daemon is ready"
break
fi
sleep 2
done
docker version
docker info
'''
}
}
stage('Build container net check') {
steps {
sh '''
set +e
docker run --rm --network host curlimages/curl:8.5.0 -I https://repo.maven.apache.org/maven2/ -m 10
echo "curl exit code=$?"
exit 0
'''
}
}
stage('Detect changed services') {
steps {
script {
def allServices = ["gateway-service","user-service","community-service","store-service","study-service","eureka-service"]
def hasPrevCommit = (sh(script: 'git rev-parse --verify HEAD~1 >/dev/null 2>&1', returnStatus: true) == 0)
if (!hasPrevCommit) {
echo "No previous commit detected (first build). Building ALL services."
env.CHANGED_SERVICES = allServices.join(" ")
} else {
def changedFiles = sh(script: "git diff --name-only HEAD~1 HEAD", returnStdout: true).trim()
echo "Changed files:\n${changedFiles}"
def lines = changedFiles ? changedFiles.readLines() : []
def changed = []
for (svc in allServices) {
if (lines.any { it.startsWith("${svc}/") }) changed << svc
}
env.CHANGED_SERVICES = (changed.isEmpty() ? allServices : changed).join(" ")
}
}
}
}
stage('Login to GHCR') {
steps {
withCredentials([usernamePassword(credentialsId: 'github-credentials', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN')]) {
sh 'echo $GITHUB_TOKEN | docker login ghcr.io -u $GITHUB_USER --password-stdin'
}
}
}
stage('Build & Push Images') {
steps {
script {
def services = env.CHANGED_SERVICES.split("\\s+")
for (svc in services) {
def imageName = "${REGISTRY}/checkmate-${svc.replace('-service','')}:${IMAGE_TAG}"
sh """
echo "=== Building ${svc} -> ${imageName} ==="
docker build --network=host --build-arg SERVICE=${svc} -t ${imageName} .
docker push ${imageName}
"""
}
}
}
}
}
post {
success {
script {
try {
discordSend(
title: "SUCCESS: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
description: "OK\n${env.BUILD_URL}",
webhookURL: env.DISCORD_WEBHOOK
)
} catch (e) { echo "discordSend failed: ${e}" }
}
}
failure {
sh 'echo "keeping pod for debug"; sleep 600'
script {
try {
discordSend(
title: "FAIL: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
description: "FAIL\n${env.BUILD_URL}",
webhookURL: env.DISCORD_WEBHOOK
)
} catch (e) { echo "discordSend failed: ${e}" }
}
}
always {
sh 'docker image prune -f || true'
}
}
}