Fix Jenkinsfile
This commit is contained in:
160
Jenkinsfile
vendored
160
Jenkinsfile
vendored
@@ -1,76 +1,176 @@
|
|||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
options { ansiColor('xterm'); timestamps(); disableConcurrentBuilds(); timeout(time:25, unit:'MINUTES') }
|
options {
|
||||||
|
ansiColor('xterm'); timestamps(); disableConcurrentBuilds()
|
||||||
|
timeout(time: 25, unit: 'MINUTES')
|
||||||
|
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10'))
|
||||||
|
}
|
||||||
|
|
||||||
// ถ้าอยากมี fallback ให้ Jenkinsเช็คทุก 2 นาที ให้เพิ่มบรรทัดด้านล่างแทน giteaPush():
|
// ถ้าอยากมี 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'
|
APP_PORT = '8080' // Kestrel ใน container
|
||||||
HOST_PORT = '5002'
|
HOST_PORT = '5002' // พอร์ตฝั่ง host (Nginx ยิงเข้า)
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Checkout'){ steps { checkout scm } }
|
|
||||||
|
|
||||||
stage('Unit Tests (optional)'){
|
stage('Checkout') {
|
||||||
when { expression { fileExists('AMREZ.EOP.sln') } }
|
steps { checkout scm }
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Preflight diagnostics') {
|
||||||
steps {
|
steps {
|
||||||
sh '''
|
sh '''
|
||||||
docker run --rm -v "$PWD":/src -w /src mcr.microsoft.com/dotnet/sdk:9.0 \
|
set -euxo pipefail
|
||||||
bash -lc "dotnet test --configuration Release --nologo --logger trx; true"
|
echo "== System =="
|
||||||
|
whoami || true
|
||||||
|
id || true
|
||||||
|
uname -a || true
|
||||||
|
echo "Workspace: $PWD"
|
||||||
|
df -h || true
|
||||||
|
free -h || true
|
||||||
|
|
||||||
|
echo "== Git =="
|
||||||
|
git rev-parse --is-inside-work-tree || true
|
||||||
|
git rev-parse --abbrev-ref HEAD || true
|
||||||
|
git rev-parse --short=12 HEAD || true
|
||||||
|
|
||||||
|
echo "== Docker =="
|
||||||
|
docker version
|
||||||
|
docker info || true
|
||||||
|
|
||||||
|
echo "== DNS/HTTPS to Registry =="
|
||||||
|
getent hosts ${REGISTRY} || true
|
||||||
|
curl -sS -I https://${REGISTRY}/v2/ || true
|
||||||
'''
|
'''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Docker Build'){
|
stage('Unit Tests (optional)') {
|
||||||
|
when { expression { fileExists('AMREZ.EOP.sln') } }
|
||||||
|
steps {
|
||||||
|
sh '''
|
||||||
|
set -euxo pipefail
|
||||||
|
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"
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Docker Build') {
|
||||||
steps {
|
steps {
|
||||||
sh 'docker version'
|
|
||||||
script {
|
script {
|
||||||
def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'main').replaceFirst(/^origin\\//,'')
|
// รองรับทั้ง 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()
|
||||||
|
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}"
|
||||||
sh """
|
|
||||||
docker build -t ${REGISTRY}/${IMAGE}:${tag} .
|
|
||||||
docker tag ${REGISTRY}/${IMAGE}:${tag} ${REGISTRY}/${IMAGE}:latest
|
|
||||||
"""
|
|
||||||
env.IMAGE_TAG = tag
|
env.IMAGE_TAG = tag
|
||||||
|
env.IMAGE_COMMIT = commit
|
||||||
|
env.IMAGE_BRANCH = branch
|
||||||
|
|
||||||
|
sh """
|
||||||
|
set -euxo pipefail
|
||||||
|
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
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Docker Push'){
|
stage('Diagnose registry before push') {
|
||||||
steps {
|
steps {
|
||||||
withCredentials([usernamePassword(credentialsId:'registry-basic', usernameVariable:'REG_USER', passwordVariable:'REG_PASS')]){
|
sh '''
|
||||||
sh """
|
set -euxo pipefail
|
||||||
echo "\$REG_PASS" | docker login ${REGISTRY} -u "\$REG_USER" --password-stdin
|
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') {
|
||||||
|
steps {
|
||||||
|
withCredentials([usernamePassword(credentialsId: 'registry-basic',
|
||||||
|
usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) {
|
||||||
|
sh '''
|
||||||
|
set -euxo pipefail
|
||||||
|
echo "Login registry ${REGISTRY} as $REG_USER"
|
||||||
|
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}:${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
|
||||||
|
|
||||||
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 -eux
|
set -euxo pipefail
|
||||||
|
echo "Stopping previous container if exists..."
|
||||||
docker rm -f eop-services-api || true
|
docker rm -f eop-services-api || true
|
||||||
docker run -d --name eop-services-api \
|
|
||||||
-p 127.0.0.1:${HOST_PORT}:${APP_PORT} \
|
echo "Starting container from ${REGISTRY}/${IMAGE}:latest"
|
||||||
-e ASPNETCORE_URLS=http://+:${APP_PORT} \
|
docker run -d --name eop-services-api \\
|
||||||
--restart=always \
|
-p 127.0.0.1:${HOST_PORT}:${APP_PORT} \\
|
||||||
|
-e ASPNETCORE_URLS=http://+:${APP_PORT} \\
|
||||||
|
--restart=always \\
|
||||||
${REGISTRY}/${IMAGE}:latest
|
${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
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
success { echo "Deployed at http://127.0.0.1:${HOST_PORT} (ผ่าน Nginx/SSL เมื่อเซ็ต)" }
|
success {
|
||||||
always { cleanWs() }
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user