jenkins安装相关插件,供后续使用(Dashboard - Manage Jenkins - Plugins)
Maven Integration plugin https://plugins.jenkins.io/maven-plugin
CloudBees Docker Build and Publish pluginhttps://plugins.jenkins.io/docker-build-publishNodeJS Pluginhttps://plugins.jenkins.io/nodejs
配置内置的工具及其版本 (Dashboard - Manage Jenkins - Tools)
比如 Maven JDK Git NodeJS Docker等
添加凭据,如SSH Private Key 用于拉取远程代码(Dashboard - Manage Jenkins - Credentials)
一、基础版本
java%E9%A1%B9%E7%9B%AE">(1)Java项目
# Build
# Root POM
pom.xml
# Goals and options
clean package -U -Dmaven.test.skip=true
Post Steps - Execute shell
#!/bin/bash
source /etc/profile
AppHome=/app/api/
AppName=api.jar
LogPath=logs/sys-info.log
echo "应用程序 $AppName 执行停止"
PID=""
query(){
PID=`ps -ef | grep java | grep $AppName | grep -v grep | awk '{print $2}'`
}
query
if [ x"$PID" != x"" ]; then
kill -TERM $PID
while [ x"$PID" != x"" ]
do
echo "应用程序 $AppName (pid:$PID) 正在停止"
sleep 1
query
done
echo "应用程序 $AppName 停止成功"
else
echo "应用程序 $AppName 已经停止"
fi
sleep 1
echo "应用程序 $AppName [test] 执行启动"
if [ ! -d $AppHome ]; then
echo "创建目录 $AppHome"
mkdir $AppHome
fi
if [ ! -d ${AppHome}logs ]; then
echo "创建目录 ${AppHome}logs"
mkdir ${AppHome}logs
echo "创建文件 $AppHome$LogPath"
touch $AppHome$LogPath
fi
mv $WORKSPACE/target/*.jar $AppHome$AppName
echo "正在启动"
if [ -f $AppHome$AppName ]; then
BUILD_ID=KeepAlive
nohup java -jar $AppHome$AppName --spring.profiles.active=prod --server.port=$port > /dev/null 2>&1 &
sleep 5
tail -50 $AppHome$LogPath
sleep 5
tail -50 $AppHome$LogPath
sleep 5
tail -50 $AppHome$LogPath
else
echo "应用程序 $AppName 启动失败:文件不存在"
fi
(2)Vue项目
Post Steps - Execute shell
#!/bin/bash
source /etc/profile
AppHome=/app/ui/
echo "开始重新打包"
npm install
echo "清理工作区缓存"
rm -rf $WORKSPACE/dist/*
echo "开始打包:prod"
npm run build:prod
echo "删除上次复制的文件"
rm -rf $AppHome
if [ ! -d $AppHome ]; then
echo "创建目录 $AppHome"
mkdir $AppHome
fi
echo "开始重新复制"
cp -rf $WORKSPACE/dist/* $AppHome
echo "部署成功"
二、使用Docker + Pipline
Dockerfile参考上文。
(1)Java项目
流水线 Pipeline script
pipeline {
agent any
environment {
// 在 Jenkins 全局工具配置中定义的插件安装的名称
MAVEN_HOME = tool 'maven3.8.7'
PATH="${MAVEN_HOME}/bin:${env.PATH}"
GIT_SSH_***MAND = 'ssh -i /var/lib/jenkins/.ssh/id_rsa'
}
stages {
stage('MAVEN Versions') {
steps {
echo 'MAVEN version:'
sh 'mvn -v'
}
}
stage('Clone Repository') {
steps {
script {
// 使用指定的私钥克隆仓库并切换到分支
checkout([$class: 'GitSCM', branches: [[name: 'pc']], userRemoteConfigs: [[credentialsId: 'f2564736-xxxx-xx-xx', url: 'git@xxxxxx.git']]])
}
}
}
stage('Build War') {
steps {
sh 'mvn clean package -U -Dmaven.test.skip=true'
}
}
stage('Docker Build and Deploy') {
steps {
script {
def imageName = 'api'
def ctName = 'api-ct'
// Stop and remove the previous container
sh "docker stop ${ctName} || true"
sh "docker rm ${ctName} || true"
sh "docker rmi ${imageName} || true"
// Build Docker image
sh "docker build -f Dockerfile -t ${imageName} ."
// Start a new container
sh "docker run --restart always -d -p 9008:9008 -e ACTIVE_PROFILE='pc' -e SERVICE_PORT='9008' --name $ctName --ulimit nofile=1024 --***work host $imageName"
}
}
}
}
post {
su***ess {
echo 'Deployment su***essful!'
}
failure {
echo 'Deployment failed!'
}
}
}
(2)Vue项目
流水线 Pipeline script
pipeline {
agent any
environment {
// 在 Jenkins 全局工具配置中定义的 Node.js 安装的名称
NODEJS_HOME = tool 'node16.3.0'
PATH="${NODEJS_HOME}/bin:${env.PATH}"
GIT_SSH_***MAND = 'ssh -i /var/lib/jenkins/.ssh/id_rsa'
}
stages {
stage('Set npm Registry') {
steps {
script {
sh 'npm config set registry https://registry.npmmirror.***'
sh 'npm config get registry'
}
}
}
stage('Node and NPM Versions') {
steps {
echo 'Node version:'
sh 'node -v'
echo 'NPM version:'
sh 'npm -v'
}
}
stage('Clone Repository') {
steps {
script {
// 使用指定的私钥克隆仓库并切换到 pc 分支
checkout([$class: 'GitSCM', branches: [[name: 'pc']], userRemoteConfigs: [[credentialsId: 'f2564736-xx-x-xx-xxx', url: 'git@xxxxx.git']]])
}
}
}
stage('Install Dependencies') {
steps {
sh 'npm install --verbose --legacy-peer-deps'
}
}
stage('Build') {
steps {
sh 'npm run build:hylt-pc'
}
}
stage('Docker Build and Deploy') {
steps {
script {
def imageName = 'ui'
def ctName = 'ui-ct'
// Stop and remove the previous container
sh "docker stop ${ctName} || true"
sh "docker rm ${ctName} || true"
sh "docker rmi ${imageName} || true"
// Build Docker image
sh "docker build -f Dockerfile -t ${imageName} ."
// Start a new container
sh "docker run --restart always -d -p 1908:1908 --name ${ctName} --***work host ${imageName}"
}
}
}
}
post {
su***ess {
echo 'Deployment su***essful!'
}
failure {
echo 'Deployment failed!'
}
}
}
注意此处使用的jenkins内置的nodejs和npm,Vue项目在部署过程中使用到了项目根目录下的Dockerfile和nginx.conf两个文件