Fix Jenkinsfile

This commit is contained in:
Thanakarn Klangkasame
2025-09-30 13:19:59 +07:00
parent 174b810119
commit 1e89fac663

85
Jenkinsfile vendored
View File

@@ -5,27 +5,22 @@ pipeline {
timeout(time: 25, unit: 'MINUTES') timeout(time: 25, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10')) buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10'))
} }
// ถ้าอยากมี fallback polling ทุก 2 นาที แทนที่จะพึ่งแต่ webhook:
// triggers { pollSCM('H/2 * * * *') } // triggers { pollSCM('H/2 * * * *') }
environment { environment {
REGISTRY = 'registry.aetherframe.tech' REGISTRY = 'registry.aetherframe.tech'
IMAGE = 'simulationable/eop-services-api' // แนะนำพิมพ์เล็กล้วน IMAGE = 'simulationable/eop-services-api'
APP_PORT = '8080' // Kestrel ใน container APP_PORT = '8080'
HOST_PORT = '5002' // พอร์ตฝั่ง host (Nginx ยิงเข้า) HOST_PORT = '5002'
} }
stages { stages {
stage('Checkout'){ steps { checkout scm } }
stage('Checkout') {
steps { checkout scm }
}
stage('Preflight diagnostics') { stage('Preflight diagnostics') {
steps { steps {
sh ''' sh '''
set -euxo pipefail set -eux
echo "== System ==" echo "== System =="
whoami || true whoami || true
id || true id || true
@@ -50,43 +45,35 @@ pipeline {
} }
} }
stage('Unit Tests (optional)') { stage('Unit Tests (optional)'){
when { expression { fileExists('AMREZ.EOP.sln') } } when { expression { fileExists('AMREZ.EOP.sln') } }
steps { steps {
sh ''' sh '''
set -euxo pipefail set -eux
docker run --rm -v "$PWD":/src -w /src mcr.microsoft.com/dotnet/sdk:9.0 \ docker run --rm -v "$PWD":/src -w /src mcr.microsoft.com/dotnet/sdk:9.0 \
bash -lc "dotnet test --configuration Release --nologo --logger trx || true" bash -lc "set -euo pipefail; dotnet test --configuration Release --nologo --logger trx || true"
''' '''
} }
} }
stage('Docker Build') { stage('Docker Build'){
steps { steps {
script { script {
// รองรับทั้ง Multibranch (BRANCH_NAME) และ Pipeline เดี่ยว (GIT_BRANCH)
def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()) def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim())
.replaceFirst(/^origin\\//,'') .replaceFirst(/^origin\\//,'').toLowerCase()
.toLowerCase()
def commit = sh(script: 'git rev-parse --short=12 HEAD', returnStdout: true).trim() def commit = sh(script: 'git rev-parse --short=12 HEAD', returnStdout: true).trim()
def tag = "${branch}-${env.BUILD_NUMBER}" def tag = "${branch}-${env.BUILD_NUMBER}"
env.IMAGE_TAG = tag
env.IMAGE_TAG = tag
env.IMAGE_COMMIT = commit
env.IMAGE_BRANCH = branch
sh """ sh """
set -euxo pipefail set -eux
echo "Building image: ${REGISTRY}/${IMAGE}:${tag}" echo "Building image: ${REGISTRY}/${IMAGE}:${tag}"
docker build --pull --progress=plain \\ docker build --pull --progress=plain \\
--label org.opencontainers.image.source="${env.GIT_URL ?: 'gitea'}" \\
--label org.opencontainers.image.revision="${commit}" \\ --label org.opencontainers.image.revision="${commit}" \\
-t ${REGISTRY}/${IMAGE}:${tag} \\ -t ${REGISTRY}/${IMAGE}:${tag} \\
-t ${REGISTRY}/${IMAGE}:latest \\ -t ${REGISTRY}/${IMAGE}:latest \\
. .
echo "== Built images =="
docker images ${REGISTRY}/${IMAGE} --digests || true docker images ${REGISTRY}/${IMAGE} --digests || true
echo "== Inspect built image =="
docker inspect ${REGISTRY}/${IMAGE}:${tag} --format='ID={{.Id}} Size={{.Size}}' || true docker inspect ${REGISTRY}/${IMAGE}:${tag} --format='ID={{.Id}} Size={{.Size}}' || true
""" """
} }
@@ -96,81 +83,53 @@ pipeline {
stage('Diagnose registry before push') { stage('Diagnose registry before push') {
steps { steps {
sh ''' sh '''
set -euxo pipefail set -eux
getent hosts ${REGISTRY} || true getent hosts ${REGISTRY} || true
curl -sS -I https://${REGISTRY}/v2/ || true curl -sS -I https://${REGISTRY}/v2/ || true
echo "Local images matching ${REGISTRY}/${IMAGE}:"
docker images ${REGISTRY}/${IMAGE} --digests || true docker images ${REGISTRY}/${IMAGE} --digests || true
"""
''' '''
} }
} }
stage('Docker Push') { stage('Docker Push'){
steps { steps {
withCredentials([usernamePassword(credentialsId: 'registry-basic', withCredentials([usernamePassword(credentialsId:'registry-basic', usernameVariable:'REG_USER', passwordVariable:'REG_PASS')]){
usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) {
sh ''' sh '''
set -euxo pipefail set -eux
echo "Login registry ${REGISTRY} as $REG_USER"
docker logout ${REGISTRY} || true docker logout ${REGISTRY} || true
echo "$REG_PASS" | docker login ${REGISTRY} -u "$REG_USER" --password-stdin echo "$REG_PASS" | docker login ${REGISTRY} -u "$REG_USER" --password-stdin
echo "Pushing tags: ${REGISTRY}/${IMAGE}:${IMAGE_TAG} and :latest"
docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG} docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG}
docker push ${REGISTRY}/${IMAGE}:latest docker push ${REGISTRY}/${IMAGE}:latest
echo "== Verify tags on registry =="
# ไม่พึ่ง jq เพื่อกัน dependency — แสดง JSON ดิบๆพอ
curl -sS -u "$REG_USER:$REG_PASS" https://${REGISTRY}/v2/${IMAGE}/tags/list || true curl -sS -u "$REG_USER:$REG_PASS" https://${REGISTRY}/v2/${IMAGE}/tags/list || true
docker logout ${REGISTRY} || true docker logout ${REGISTRY} || true
''' '''
} }
} }
} }
stage('Deploy (same host)') { stage('Deploy (same host)'){
when { branch 'main' } when { branch 'main' }
steps { steps {
sh """ sh """
set -euxo pipefail set -eux
echo "Stopping previous container if exists..."
docker rm -f eop-services-api || true docker rm -f eop-services-api || true
echo "Starting container from ${REGISTRY}/${IMAGE}:latest"
docker run -d --name eop-services-api \\ docker run -d --name eop-services-api \\
-p 127.0.0.1:${HOST_PORT}:${APP_PORT} \\ -p 127.0.0.1:${HOST_PORT}:${APP_PORT} \\
-e ASPNETCORE_URLS=http://+:${APP_PORT} \\ -e ASPNETCORE_URLS=http://+:${APP_PORT} \\
--restart=always \\ --restart=always \\
${REGISTRY}/${IMAGE}:latest ${REGISTRY}/${IMAGE}:latest
echo "== ps/logs =="
sleep 2 sleep 2
docker ps --no-trunc | sed -n '1,5p' || true docker ps --no-trunc | sed -n '1,5p' || true
docker logs --tail=200 eop-services-api || true docker logs --tail=200 eop-services-api || true
(curl -fsS http://127.0.0.1:${HOST_PORT}/health || curl -fsS http://127.0.0.1:${HOST_PORT}/) || true
echo "== App health (best effort) =="
for i in \$(seq 1 30); do
(curl -fsS http://127.0.0.1:${HOST_PORT}/health || \\
curl -fsS http://127.0.0.1:${HOST_PORT}/) && break || true
sleep 1
done || true
""" """
} }
} }
} }
post { post {
success { success { echo "✅ Deployed at http://127.0.0.1:${HOST_PORT}" }
echo "✅ Deployed at http://127.0.0.1:${HOST_PORT} (จะเข้า https ผ่าน Nginx หลังตั้งค่า vhost)" failure { echo "❌ Failed — ดูสเตจ Preflight/Push เพื่อตามต่อ" }
} always { echo "Cleaning workspace…"; cleanWs() }
failure {
echo "❌ Failed — ดู log ของ stages: Preflight, Diagnose registry, Docker Push เพื่อหาสาเหตุ"
}
always {
echo "Cleaning workspace…"
cleanWs()
}
} }
} }