Files
eapim-admin/Jenkinsfile
T
rinjae e8069ef371
eapim-admin CI / build (push) Has been cancelled
Jenkinsfile 업데이트
2026-06-18 12:10:31 +09:00

258 lines
7.9 KiB
Groovy

pipeline {
agent { label 'djb-vm' }
triggers {
cron('H/10 * * * *')
}
options {
timestamps()
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20'))
}
environment {
JAVA_HOME = '/apps/opts/jdk8'
JAVA_HOME_TOMCAT = '/apps/opts/jdk17'
GRADLE_HOME = '/apps/opts/gradle-8.7'
GRADLE_USER_HOME = '/apps/opts/gradle-home'
NODE_HOME = '/apps/opts/node-v24'
PATH = "/apps/opts/jdk8/bin:/apps/opts/gradle-8.7/bin:/apps/opts/node-v24/bin:/apps/opts/bin:${env.PATH}"
GIT_SSH_COMMAND = 'ssh -o StrictHostKeyChecking=accept-new'
CATALINA_BASE = '/prod/eapim/adminportal'
CATALINA_HOME = '/prod/eapim/apache-tomcat-9.0.116'
CATALINA_PID = '/prod/eapim/adminportal/temp/catalina.pid'
DEPLOY_HTTP_PORT = '39120'
REVISION_STATE_FILE = '.jenkins-last-deploy-revisions'
SHOULD_DEPLOY = 'true'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Checkout dependencies') {
steps {
sh '''
set -eu
cd "$WORKSPACE/.."
mkdir -p eapim-online
for REPO in elink-online-core elink-online-core-jpa elink-online-transformer elink-online-common elink-online-emsclient; do
TARGET="eapim-online/$REPO"
if [ ! -d "$TARGET/.git" ]; then
rm -rf "$TARGET"
git clone --depth=1 --branch master \
"ssh://git@172.30.1.50:2222/djb-eapim/$REPO.git" \
"$TARGET"
else
git -C "$TARGET" fetch --depth=1 origin master
git -C "$TARGET" reset --hard origin/master
git -C "$TARGET" clean -fdx
fi
done
for REPO in elink-portal-common eapim-admin-djb; do
if [ ! -d "$REPO/.git" ]; then
rm -rf "$REPO"
git clone --depth=1 --branch master \
"ssh://git@172.30.1.50:2222/djb-eapim/$REPO.git" \
"$REPO"
else
git -C "$REPO" fetch --depth=1 origin master
git -C "$REPO" reset --hard origin/master
git -C "$REPO" clean -fdx
fi
done
'''
}
}
stage('Detect changes') {
steps {
script {
int status = sh(
returnStatus: true,
script: '''
set -eu
current_file="$(mktemp)"
{
printf 'eapim-admin %s\\n' "$(git rev-parse HEAD)"
for REPO in eapim-online/elink-online-core eapim-online/elink-online-core-jpa eapim-online/elink-online-transformer eapim-online/elink-online-common eapim-online/elink-online-emsclient elink-portal-common eapim-admin-djb; do
printf '%s %s\\n' "$REPO" "$(git -C "$WORKSPACE/../$REPO" rev-parse HEAD)"
done
} | sort > "$current_file"
if [ -f "$REVISION_STATE_FILE" ] && cmp -s "$current_file" "$REVISION_STATE_FILE"; then
echo "No repository changes detected. Skipping build and deploy."
echo "--- current revisions ---"
cat "$current_file"
rm -f "$current_file"
exit 10
fi
echo "Repository changes detected. Build and deploy will run."
echo "--- previous revisions ---"
[ -f "$REVISION_STATE_FILE" ] && cat "$REVISION_STATE_FILE" || echo "(none)"
echo "--- current revisions ---"
cat "$current_file"
mv "$current_file" .jenkins-current-deploy-revisions
'''
)
if (status == 10) {
env.SHOULD_DEPLOY = 'false'
currentBuild.description = 'No repo changes'
} else if (status != 0) {
error "Revision change detection failed with status ${status}"
} else {
env.SHOULD_DEPLOY = 'true'
}
}
}
}
stage('Verify toolchain') {
when {
expression { env.SHOULD_DEPLOY == 'true' }
}
steps {
sh '''
set -eu
java -version
gradle --version
'''
}
}
stage('Build WAR') {
when {
expression { env.SHOULD_DEPLOY == 'true' }
}
steps {
sh 'gradle clean build -x test --no-daemon'
}
post {
success {
sh '''
set -eu
cd build/libs
sha1sum eapim-admin.war > SHA1SUMS
sha256sum eapim-admin.war > SHA256SUMS
'''
archiveArtifacts artifacts: 'build/libs/eapim-admin.war,build/libs/SHA1SUMS,build/libs/SHA256SUMS', fingerprint: true
}
}
}
stage('Stop Tomcat') {
when {
expression { env.SHOULD_DEPLOY == 'true' }
}
steps {
sh '''
set +e
systemctl --user stop eapim-admin 2>/dev/null
if [ -f "$CATALINA_PID" ] && kill -0 "$(cat "$CATALINA_PID")" 2>/dev/null; then
JAVA_HOME="$JAVA_HOME_TOMCAT" "$CATALINA_HOME/bin/shutdown.sh" 30 -force || true
for i in $(seq 1 30); do
[ -f "$CATALINA_PID" ] && kill -0 "$(cat "$CATALINA_PID")" 2>/dev/null || break
sleep 1
done
fi
rm -f "$CATALINA_PID"
'''
}
}
stage('Deploy monitoring.war') {
when {
expression { env.SHOULD_DEPLOY == 'true' }
}
steps {
sh '''
set -eu
DEPLOY_DIR="$CATALINA_BASE/webapps"
rm -rf "$DEPLOY_DIR/monitoring" "$DEPLOY_DIR/monitoring.war"
cp build/libs/eapim-admin.war "$DEPLOY_DIR/monitoring.war"
ls -la "$DEPLOY_DIR"
'''
}
}
stage('Start Tomcat and readiness') {
when {
expression { env.SHOULD_DEPLOY == 'true' }
}
steps {
sh '''
set -eu
LOG="$CATALINA_BASE/logs/catalina.$(date +%Y-%m-%d).log"
BEFORE=0
[ -f "$LOG" ] && BEFORE=$(stat -c %s "$LOG")
systemctl --user start eapim-admin
PROBE_URL="http://localhost:$DEPLOY_HTTP_PORT/monitoring/loginForm.do"
DEADLINE=$(($(date +%s) + 300))
STATUS=000
while [ "$(date +%s)" -lt "$DEADLINE" ]; do
STATUS=$(curl -sS -o /dev/null -w '%{http_code}' --max-time 3 \
"$PROBE_URL" 2>/dev/null || echo "000")
case "$STATUS" in
200|302|401)
echo "Readiness OK ($PROBE_URL HTTP $STATUS)"
break
;;
esac
if [ -f "$LOG" ] && tail -c +$((BEFORE+1)) "$LOG" | grep -qE "Application listener .* Exception|SEVERE.*startup.Catalina"; then
echo "Startup error detected in log"
tail -c +$((BEFORE+1)) "$LOG" | tail -80
exit 1
fi
sleep 2
done
case "$STATUS" in
200|302|401) ;;
*)
echo "Readiness probe failed within 300s, last HTTP status: $STATUS"
echo "--- last 300 lines of catalina log ---"
[ -f "$LOG" ] && tail -c +$((BEFORE+1)) "$LOG" | tail -300
echo "--- systemd unit status ---"
systemctl --user status eapim-admin --no-pager || true
echo "--- listening ports ---"
ss -tlnp 2>/dev/null | grep -E ":$DEPLOY_HTTP_PORT\\b" || echo "(port $DEPLOY_HTTP_PORT not listening)"
exit 1
;;
esac
echo "--- key boot log lines ---"
tail -c +$((BEFORE+1)) "$LOG" | grep -E "Server startup|Deployment of web" | head -10 || true
'''
}
}
}
post {
success {
script {
if (env.SHOULD_DEPLOY == 'true') {
sh '''
set -eu
if [ -f .jenkins-current-deploy-revisions ]; then
mv .jenkins-current-deploy-revisions "$REVISION_STATE_FILE"
fi
'''
}
}
}
}
}