pipeline { agent any options { ansiColor('xterm'); timestamps(); disableConcurrentBuilds(); timeout(time:25, unit:'MINUTES') } // ถ้าอยากมี fallback ให้ Jenkinsเช็คทุก 2 นาที ให้เพิ่มบรรทัดด้านล่างแทน giteaPush(): // triggers { pollSCM('H/2 * * * *') } environment { REGISTRY = 'registry.aetherframe.tech' IMAGE = 'simulationable/eop-services-api' APP_PORT = '8080' HOST_PORT = '5002' } stages { stage('Checkout'){ steps { checkout scm } } stage('Unit Tests (optional)'){ when { expression { fileExists('AMREZ.EOP.sln') } } 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" ''' } } stage('Docker Build'){ steps { sh 'docker version' script { def branch = (env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'main').replaceFirst(/^origin\\//,'') 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 } } } stage('Docker 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 docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG} docker push ${REGISTRY}/${IMAGE}:latest docker logout ${REGISTRY} || true """ } } } stage('Deploy (same host)'){ when { branch 'main' } steps { sh """ set -eux 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 \ ${REGISTRY}/${IMAGE}:latest """ } } } post { success { echo "Deployed at http://127.0.0.1:${HOST_PORT} (ผ่าน Nginx/SSL เมื่อเซ็ต)" } always { cleanWs() } } }