Fix Jenkinsfile

This commit is contained in:
Thanakarn Klangkasame
2025-09-30 11:13:12 +07:00
parent 92e614674c
commit 30a2cb936e

36
Jenkinsfile vendored
View File

@@ -1,27 +1,19 @@
pipeline { pipeline {
agent any agent any
options { ansiColor('xterm'); timestamps(); disableConcurrentBuilds(); timeout(time:25, unit:'MINUTES') }
options { // ถ้าอยากมี fallback ให้ Jenkinsเช็คทุก 2 นาที ให้เพิ่มบรรทัดด้านล่างแทน giteaPush():
ansiColor('xterm'); timestamps(); disableConcurrentBuilds() // triggers { pollSCM('H/2 * * * *') }
timeout(time: 25, unit: 'MINUTES')
}
/* ให้ Gitea ยิง webhook แล้ว build ได้เลย */
triggers {
giteaPush()
}
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 ฟัง 8080 APP_PORT = '8080'
HOST_PORT = '5002' // พอร์ตฝั่ง host ที่ Nginx จะ proxy เข้า HOST_PORT = '5002'
} }
stages { stages {
stage('Checkout') { stage('Checkout'){ steps { checkout scm } }
steps { checkout scm }
}
stage('Unit Tests (optional)'){ stage('Unit Tests (optional)'){
when { expression { fileExists('AMREZ.EOP.sln') } } when { expression { fileExists('AMREZ.EOP.sln') } }
@@ -37,7 +29,6 @@ pipeline {
steps { steps {
sh 'docker version' sh 'docker version'
script { script {
/* รองรับทั้ง Multibranch (BRANCH_NAME) และ Pipeline เดี่ยว (GIT_BRANCH) */
def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'main').replaceFirst(/^origin\\//,'') def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'main').replaceFirst(/^origin\\//,'')
def tag = "${branch}-${env.BUILD_NUMBER}" def tag = "${branch}-${env.BUILD_NUMBER}"
sh """ sh """
@@ -51,8 +42,7 @@ pipeline {
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 """
echo "\$REG_PASS" | docker login ${REGISTRY} -u "\$REG_USER" --password-stdin echo "\$REG_PASS" | docker login ${REGISTRY} -u "\$REG_USER" --password-stdin
docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG} docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG}
@@ -69,10 +59,10 @@ pipeline {
sh """ sh """
set -eux set -eux
docker rm -f eop-services-api || true docker rm -f eop-services-api || true
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
""" """
} }
@@ -80,7 +70,7 @@ pipeline {
} }
post { post {
success { echo "Deployed at http://127.0.0.1:${HOST_PORT} (จะเข้าผ่าน https หลังตั้ง Nginx)" } success { echo "Deployed at http://127.0.0.1:${HOST_PORT} (ผ่าน Nginx/SSL เมื่อเซ็ต)" }
always { cleanWs() } always { cleanWs() }
} }
} }