From 49620c5e217d3511fd7e5f4d47f46f59d00251a4 Mon Sep 17 00:00:00 2001 From: rinjae Date: Thu, 18 Jun 2026 12:01:57 +0900 Subject: [PATCH] =?UTF-8?q?Jenkinsfile=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..2606e74 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,165 @@ +pipeline { + agent { label 'djb-vm' } + + triggers { + pollSCM('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/apigw' + CATALINA_HOME = '/prod/eapim/apache-tomcat-9.0.116' + CATALINA_PID = '/prod/eapim/apigw/logs/catalina.pid' + DEPLOY_HTTP_PORT = '39110' + } + + stages { + stage('Checkout') { + steps { + checkout scm + } + } + + stage('Checkout modules') { + steps { + sh ''' + set -eu + + for REPO in elink-online-core elink-online-transformer elink-online-common elink-online-emsclient elink-online-core-jpa; do + if [ ! -e "$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('Verify toolchain') { + steps { + sh ''' + set -eu + java -version + gradle --version + ''' + } + } + + stage('Build WAR') { + steps { + sh 'gradle clean build -x test --no-daemon' + } + post { + success { + sh ''' + set -eu + cd build/libs + sha1sum eapim-online.war > SHA1SUMS + sha256sum eapim-online.war > SHA256SUMS + ''' + archiveArtifacts artifacts: 'build/libs/eapim-online.war,build/libs/SHA1SUMS,build/libs/SHA256SUMS', fingerprint: true + } + } + } + + stage('Stop Tomcat') { + steps { + sh ''' + set +e + systemctl --user stop eapim-apigw 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 ROOT.war') { + steps { + sh ''' + set -eu + DEPLOY_DIR="$CATALINA_BASE/webapps" + rm -rf "$DEPLOY_DIR/ROOT" "$DEPLOY_DIR/ROOT.war" + cp build/libs/eapim-online.war "$DEPLOY_DIR/ROOT.war" + ls -la "$DEPLOY_DIR" + ''' + } + } + + stage('Start Tomcat and readiness') { + 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-apigw + + PROBE_URL="http://localhost:$DEPLOY_HTTP_PORT/" + 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|403|404) + 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|403|404) ;; + *) + 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-apigw --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 + ''' + } + } + } +}