Skip to content

Jenkins备忘录

记录Jenkins使用遇到的问题以及解决方案。

Jenkins安装

shell
sudo docker run \
  -u root \
  --name jenkins-blueocean \
  --restart=on-failure \
  --detach \
  --publish 8080:8080 \
  --volume /root/jenkins-data:/var/jenkins_home \
  jenkins/jenkins:latest

基础配置

2023061410112820230614101204

构建流水线

前端

Groovy
def lastCommitMessage
def remote = [:]
remote.name = '测试服务器'
remote.host = '***.***.***.***'
remote.user = 'root'
remote.password = 'root'
remote.allowAnyHosts = true

pipeline {
    agent any
    
    stages {
        stage('拉取代码') {
            steps {
                git branch: 'develop', credentialsId: 'github', url: 'https://github.com/vuejs/vitepress'
                echo pwd()
            }
        }
        stage('获取最后一次提交的消息') {
            steps {            
                script {
                    lastCommitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
                    echo "Last Commit Message: ${lastCommitMessage}"
                }
            }
        }
        stage('构建') {
            steps {
                nodejs('v18.16.0') {
                    sh 'pnpm i --no-frozen-lockfile && pnpm build:test'
                }
                dir('./dist/') {
                    echo pwd()
                    zip defaultExcludes: false, dir: '', exclude: '', glob: '', zipFile: 'dist.zip'
                }
            }
        }
        stage('发布到服务器') {
            steps {
                sshCommand remote: remote, command: "rm -rf /root/docker/nginx/www/***/*"
                dir('./dist/') {
                    sshPut remote: remote, from: 'dist.zip', into: '/root/docker/nginx/www/***/'
                }
                sshCommand remote: remote, command: "cd /root/docker/nginx/www/***/ && unzip dist.zip && rm dist.zip"
            }
        }
        
    }
    post {
        success {
            qyWechatNotification mentionedId: '', mentionedMobile: '18888888888', moreInfo: '', webhookUrl: '企业微信webhookUrl'
            wechatWebhook url:"企业微信webhookUrl",content:"""## 本次更新内容:
            > ${lastCommitMessage}"""
        }
        failure {
            qyWechatNotification mentionedId: '', mentionedMobile: '18888888888', moreInfo: '', webhookUrl: '企业微信webhookUrl'
        }
    }
}

java

Groovy
def lastCommitMessage
def remote = [:]
remote.name = '测试服务器'
remote.host = '***.***.***.***'
remote.user = 'root'
remote.password = 'root'
remote.allowAnyHosts = true

pipeline {
    agent any
    
    stages {
        stage('拉取代码') {
            steps {
                git branch: 'develop', credentialsId: 'github.com', url: 'https://github.com/vuejs/vitepress'
                echo pwd()
            }
        }
        stage('获取最后一次提交的消息') {
            steps {            
                script {
                    lastCommitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
                    echo "Last Commit Message: ${lastCommitMessage}"
                }
            }
        }
        stage('构建') {
            steps {
                withMaven(globalMavenSettingsConfig: '', jdk: '', maven: '3.9.2') {
                    sh 'mvn clean install -Dmaven.test.skip=true'
                }
            }
        }
        stage('发布到测试服务器') {
            steps {
                sshCommand remote: remote, command: "rm -rf /root/application/***/***.jar"
                dir('./***/target/') {
                    sshPut remote: remote, from: '***.jar', into: '/root/application/***/'
                }
                sshCommand remote: remote, command: "/root/application/***/starter.sh"
            }
        }
        
    }
    post {
        success {
            qyWechatNotification mentionedId: '', mentionedMobile: '18888888888', moreInfo: '', webhookUrl: '企业微信webhookUrl'
            wechatWebhook url:"企业微信webhookUrl",content:"""## 本次更新内容:
            > ${lastCommitMessage}"""
        }
        failure {
            qyWechatNotification mentionedId: '', mentionedMobile: '18888888888', moreInfo: '', webhookUrl: '企业微信webhookUrl'
        }
    }
}

starter.sh

shell
#!/bin/bash

# 检查并关闭旧的 Java 服务
OLD_PROCESS=$(ps -ef | grep java | grep ***.jar | awk '{print $2}')

if [ -n "$OLD_PROCESS" ]; then
  echo "Closing old Java service..."
  kill "$OLD_PROCESS"
  sleep 5  # 等待旧的进程关闭
fi

# 启动新的 JAR 包
echo "Starting new Java service..."
nohup java -jar ***.jar > output.log &

echo "New Java service started."

还有个问题?执行完启动java的命令后,终端处于不能编辑的状态,jenkins无法判定命令是否执行完毕,一直处于执行中