Add Jenkinsfile

This commit is contained in:
Thanakarn Klangkasame
2025-10-31 16:57:00 +07:00
parent 2c500219b4
commit 0d13390f34
19 changed files with 1294 additions and 363 deletions

109
Jenkinsfile vendored Normal file
View File

@@ -0,0 +1,109 @@
pipeline {
agent any
options {
ansiColor('xterm'); timestamps(); disableConcurrentBuilds()
timeout(time: 25, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10'))
}
environment {
REGISTRY = 'registry.aetherframe.tech'
IMAGE = 'simulationable/amrez-nova-web-services-app' // 👈 ชื่ออิมเมจให้ตรง repo นี้
APP_PORT = '3000' // Next.js in container
HOST_PORT = '5007' // 👈 พอร์ต host (กันชนตัวอื่น)
NODE_ENV = 'production'
NEXT_TELEMETRY_DISABLED = '1'
DOCKER_BUILDKIT = '0'
DOCKER_CLI_HINTS = 'false'
}
stages {
stage('Checkout'){ steps { checkout scm } }
stage('Preflight') {
steps {
sh '''
set -eux
echo "== Sys =="; whoami || true; uname -a || true
echo "== Git =="; git rev-parse --abbrev-ref HEAD; git rev-parse --short=12 HEAD
echo "== Docker =="; docker version; docker info || true
echo "== Registry =="; getent hosts ${REGISTRY} || true; curl -sS -I https://${REGISTRY}/v2/ || true
'''
}
}
stage('Docker Build'){
steps {
script {
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
sh """
set -eux
docker build --pull \
--label org.opencontainers.image.revision="${commit}" \
-t ${REGISTRY}/${IMAGE}:${tag} \
-t ${REGISTRY}/${IMAGE}:latest \
.
docker images ${REGISTRY}/${IMAGE} --digests || true
"""
}
}
}
stage('Registry Check') {
steps {
sh '''
set -eux
curl -sS -I https://${REGISTRY}/v2/ || true
docker images ${REGISTRY}/${IMAGE} --digests || true
'''
}
}
stage('Docker Push'){
steps {
withCredentials([usernamePassword(credentialsId:'reg-creds', usernameVariable:'REG_USER', passwordVariable:'REG_PASS')]){
sh '''
set -eux
docker logout ${REGISTRY} || true
echo "$REG_PASS" | docker login ${REGISTRY} -u "$REG_USER" --password-stdin
docker push ${REGISTRY}/${IMAGE}:${IMAGE_TAG}
docker push ${REGISTRY}/${IMAGE}:latest
curl -sS -u "$REG_USER:$REG_PASS" https://${REGISTRY}/v2/${IMAGE}/tags/list || true
docker logout ${REGISTRY} || true
'''
}
}
}
stage('Deploy (same host)'){
when { branch 'main' }
steps {
sh """
set -eux
docker rm -f amrez-nova-web || true
docker run -d --name amrez-nova-web \\
-p 127.0.0.1:${HOST_PORT}:${APP_PORT} \\
-e NODE_ENV=${NODE_ENV} \\
-e PORT=${APP_PORT} \\
--restart=always \\
${REGISTRY}/${IMAGE}:latest
sleep 2
docker ps --no-trunc | sed -n '1,5p' || true
docker logs --tail=200 amrez-nova-web || true
(curl -fsS http://127.0.0.1:${HOST_PORT}/ || true)
"""
}
}
}
post {
success { echo "✅ Deployed at http://127.0.0.1:${HOST_PORT}" }
failure { echo "❌ Failed — ดูสเตจ Preflight/Push/Deploy" }
always { cleanWs() }
}
}