Jenkin 配置
需要安装相关插件(可选)
Ant Plugin
Build Timeout
Build Timestamp Plugin
Docker plugin
Email Extension
GitHub Branch Source
Gitlab Hook Plugin
GitLab Plugin
Gradle Plugin
LDAP Plugin
Maven Integration
PAM Authentication plugin
Pipeline
Pipeline: GitHub Groovy Libraries
Publish Over SSH
Subversion
Timestamper
Workspace Cleanup Plugin
Jenkin 全局工具配置
linux 配置相关工具地址
系统设置
Jenkins集成gitLab/git
gitLab
或者
git
可以使用key
也可使用用户名密码
Publish over SSH
配置代码发布远程服务器地址,项目将会发布到该服务器
配置连接服务器秘钥
Springboot项目发布案例
演示从gitLab
拉取项目,Jenkins
自动化脚本发布
新建任务–>构建一个maven项目
配置代码
gitLab
地址
构建触发器有很多种规则,手动构建,代码提交自动构建,轮询构建等
构建命令和代码发布到远程服务器
Jenkins
执行 clean package
命令之后,会在/home/user01
目录下生成blankframework-0.0.1-SNAPSHOT.jar
文件
Transfers
中remote directory
设置为./
当前目录,SSH Server
配置的remote directory
为/home/user01
pom.xml
clean package -Dmaven.test.skip=true -Ptest
BUILD_ID=dontKillMe
#复制jar包到 /usr/local/peng/
sudo mv /home/user01/blankframework-0.0.1-SNAPSHOT.jar /usr/local/peng/
cd /usr/local/peng/
#删除旧jar包
sudo rm -f template.jar
#重命名
sudo mv blankframework-0.0.1-SNAPSHOT.jar template.jar
#启动脚本
sudo chmod 777 restart.sh
sudo ./restart.sh
restart.sh
脚本和最后的项目jar
包都存放在/usr/local/peng
目录下
restart.sh
#!/bin/bash
#进入当前目录
cd `dirname $0`
SERVER_NAME="template.jar"
current=`date "+%Y-%m-%d_%H:%M:%S"`
#备份
sudo cp ${SERVER_NAME} backup/tempalate_${current}.jar
#赋予权限
chmod 777 stop.sh
chmod 777 start.sh
echo "开始执行脚本....."
./stop.sh
./start.sh test 8084
start.sh
#!/bin/bash
cd `dirname $0`
BIN_DIR=`pwd`
DEPLOY_DIR=`pwd`
profiles=$1
port=$2
echo "启动环境profiles-->:${profiles}"
echo "启动端口port-->:${port}"
APP=template
SERVER_NAME=${APP}".jar"
if [ -z "$SERVER_NAME" ]; then
SERVER_NAME=`hostname`
fi
PIDS=`ps -ef|grep $SERVER_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $PIDS"
exit 1
fi
#LOGS_DIR=$DEPLOY_DIR/testLog
#
#if [ ! -d $LOGS_DIR ]; then
# mkdir $LOGS_DIR
#fi
STDOUT_FILE=${DEPLOY_DIR}/testLog/stdout.log
echo -e "Starting the $SERVER_NAME ...\c"
nohup java -jar ${SERVER_NAME} --spring.profiles.active=${profiles} --server.port=${port}> $STDOUT_FILE 2>&1&
echo "OK!"
PIDS=`ps -f | grep java | grep $SERVER_NAME | awk '{print $2}'`
echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"
stop.sh
#!/bin/bash
APP=template
SERVER_NAME=${APP}".jar"
PIDS=$(ps faux | grep java | grep $SERVER_NAME | awk -F " " '{print $2}')
if [ -z "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME does not started!"
exit 1
fi
echo -e "Stopping the $SERVER_NAME ...\c"
for PID in $PIDS ; do
kill $PID > /dev/null 2>&1
done
COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps -f -p $PID | grep java`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
echo "OK!"
echo "PID: $PIDS"