diff --git a/Jenkinsfile b/Jenkinsfile index ffea505..0149534 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,76 +1,176 @@ pipeline { 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 * * * *') } environment { REGISTRY = 'registry.aetherframe.tech' - IMAGE = 'simulationable/eop-services-api' - APP_PORT = '8080' - HOST_PORT = '5002' + IMAGE = 'simulationable/eop-services-api' // แนะนำพิมพ์เล็กล้วน + APP_PORT = '8080' // Kestrel ใน container + HOST_PORT = '5002' // พอร์ตฝั่ง host (Nginx ยิงเข้า) } stages { - stage('Checkout'){ steps { checkout scm } } - stage('Unit Tests (optional)'){ - when { expression { fileExists('AMREZ.EOP.sln') } } + stage('Checkout') { + steps { checkout scm } + } + + stage('Preflight diagnostics') { steps { sh ''' - 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" + set -euxo pipefail + 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 { - sh 'docker version' 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}" + + env.IMAGE_TAG = tag + env.IMAGE_COMMIT = commit + env.IMAGE_BRANCH = branch + sh """ - docker build -t ${REGISTRY}/${IMAGE}:${tag} . - docker tag ${REGISTRY}/${IMAGE}:${tag} ${REGISTRY}/${IMAGE}:latest + 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 """ - env.IMAGE_TAG = tag } } } - stage('Docker Push'){ + stage('Diagnose registry before push') { steps { - withCredentials([usernamePassword(credentialsId:'registry-basic', usernameVariable:'REG_USER', passwordVariable:'REG_PASS')]){ - sh """ - echo "\$REG_PASS" | docker login ${REGISTRY} -u "\$REG_USER" --password-stdin + sh ''' + set -euxo pipefail + 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}: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 -eux + set -euxo pipefail + echo "Stopping previous container if exists..." docker rm -f eop-services-api || true - docker run -d --name eop-services-api \ - -p 127.0.0.1:${HOST_PORT}:${APP_PORT} \ - -e ASPNETCORE_URLS=http://+:${APP_PORT} \ - --restart=always \ + + 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 """ } } } post { - success { echo "Deployed at http://127.0.0.1:${HOST_PORT} (ผ่าน Nginx/SSL เมื่อเซ็ต)" } - always { cleanWs() } + 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() + } } }