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