Jenkins笔记

238 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

文本介绍: jenkins push tag:打包是提交git版本号到仓库的方法 jenkins master:线上环境集成打包发布到SVN,包括java和node jenkins dev:jenkins集成部署到测试环境,包括java和node

Jenkins Blue Ocean

jenkins.io/zh/doc/book…

BlueOcean重新考虑了Jenkins的用户体验。BlueOcean由Jenkins Pipeline设计,但仍然兼容自由式工作,减少了团队成员的混乱,增加了清晰度。

  • 连续交付(CD)Pipeline的复杂可视化,允许快速和直观地了解Pipeline的状态。
  • Pipeline编辑器通过引导用户直观和可视化的过程创建Pipeline,使创建Pipeline平易近人。
  • 个性化,以适应团队每个成员的角色需求。
  • 需要干预和/或出现问题时确定精度。BlueOcean显示了Pipeline需要注意的地方,便于异常处理和提高生产率。
  • 用于分支和拉取请求的本地集成可以在GitHub和Bitbucket中与其他人进行代码协作时最大限度提高开发人员的生产力。

jenkins push tag

1. 配置存储凭证

避免git push总要输入账号密码

git config --global credential.helper store cat ~/.gitconfig

[user]
	email = xxx
	name = xxx
[url "https://"]
	insteadOf = git://
[credential]
	helper = store

2. 配置项目Post Steps

  • Run only if build succeeds
  • java pom.xml
v=`cat ./pom.xml | sed 's/xmlns=".*"//g' | xmllint --xpath '/project/properties/light-service.version/text()' -`
if [ "x$v" == "x" ]; then
    echo "#### not get version"
    exit 0
fi
echo '### get version:$v'
if [ "x`git tag -l $v`" != "x" ]; then
    echo "#### version $v submitted"
    exit 0
fi
git tag v$v
git push origin v$v
  • nodejs package.json
v=`grep -Po 'version[" :]+\K[^"]+' server/package.json`
if [ "x$v" == "x" ]; then
    echo "#### not get version"
    exit 0
fi
echo '### get version:$v'
if [ "x`git tag -l $v`" != "x" ]; then
    echo "#### version $v submitted"
    exit 0
fi
git tag v$v
git push origin v$v

jenkins master

jenkins集成打包发布到SVN

1. java(light-order-service-master)

Pre Steps

  • execute shell(拷贝文件)
cp ${WORKSPACE}/light-order/RELEASENOTE.md ${WORKSPACE}/light-order/light-order-service/target/;
cp ${WORKSPACE}/light-order/light-order-service/src/main/resources/application.yml ${WORKSPACE}/light-order/light-order-service/target/;
  • execute shell(提交TAG)
v=`cat ./light-order/pom.xml | sed 's/xmlns=".*"//g' | xmllint --xpath '/project/properties/light-service.version/text()' -`
if [ "x$v" == "x" ]; then
    echo "#### not get version"
    exit 0
fi
echo '### get version:$v'
if [ "x`git tag -l $v`" != "x" ]; then
    echo "#### version $v submitted"
    exit 0
fi
git tag v$v
git push origin v$v

build

  • Goals and options
clean package -pl light-order-service -am  -DskipTests=true

Post-build Actions

  • publisher to subversion repository

SVN URL:http://ip:port/svn/develop/light-order-service Target path:/data/jenkins/workspace/light-order-service-master/light-order/light-order-service/target/ Items: Pattern:.*[0-9].jar

2. nodejs(light-web-master)

build

  • execute shell(打包)
echo `pwd`;
cd tool/packager;
cnpm install;
npm run pack;
  • execute shell(提交TAG)
v=`grep -Po 'version[" :]+\K[^"]+' server/package.json`
if [ "x$v" == "x" ]; then
    echo "#### not get version"
    exit 0
fi
echo '### get version:$v'
if [ "x`git tag -l $v`" != "x" ]; then
    echo "#### version $v submitted"
    exit 0
fi
git tag v$v
git push origin v$v

Post-build Actions

  • publisher to subversion repository

SVN URL:http://ip:port/svn/develop/light-web Target path:WORKSPACE/dist/ Items: Pattern:.*[0-9].zip

jenkins dev

jenkins集成部署到测试环境

1. java(light-order-service-dev)

Pre Steps

  • execute shell
cd light-order;
mvn deploy -pl light-order-dto -am;

build

  • Goals and options
clean package -pl light-order-service -am  -DskipTests=true

Post-build Actions

  • send build artifacts over SSH

Source files:light-order/light-order-service/target/*.jar Remove prefix:light-order/light-order-service/target/ Remove directory:light-order-service Exec command:

echo '#### deploy begin...'
SERVICE_NAME="light-order-service"
SERVICE_PORT="8080"
SERVICE_HOME="/data/opt/$SERVICE_NAME"
CONFIG_HOME="/data/config/$SERVICE_NAME"

echo '#### check $SERVICE_NAME.service begin...'

if [ -f "/lib/systemd/system/$SERVICE_NAME.service" ]; then
    echo "service file exist.";
else    
    echo "service file doesn't exist, creating...";
    sudo touch /lib/systemd/system/${SERVICE_NAME}.service    
    SERVICE_FILE="/lib/systemd/system/${SERVICE_NAME}.service"
    echo $SERVICE_FILE
    echo "[Unit]" | sudo tee -a $SERVICE_FILE
    echo "Description=start/shutdown $SERVICE_NAME" | sudo tee -a $SERVICE_FILE
    echo "After=syslog.target network.target" | sudo tee -a $SERVICE_FILE

    echo "[Service]" | sudo tee -a $SERVICE_FILE
    echo "Type=forking" | sudo tee -a $SERVICE_FILE
    echo "Restart=on-failure" | sudo tee -a $SERVICE_FILE
    echo "ExecStart=/opt/monitor/sbin/startup.sh $SERVICE_NAME $SERVICE_PORT" | sudo tee -a $SERVICE_FILE
    echo "ExecStop=/opt/monitor/sbin/shutdown.sh $SERVICE_NAME" | sudo tee -a $SERVICE_FILE
    echo "ExecReload=/bin/kill -s HUP \$MAINPID" | sudo tee -a $SERVICE_FILE
    echo "PrivateTmp=true" | sudo tee -a $SERVICE_FILE
    echo "RestartSec=10s" | sudo tee -a $SERVICE_FILE

    echo "[Install]" | sudo tee -a $SERVICE_FILE
    echo "WantedBy=multi-user.target" | sudo tee -a $SERVICE_FILE
    echo "service file created."
fi
echo '#### check $SERVICE_NAME.service end.'
file=`find /data/source/$SERVICE_NAME  -name "$SERVICE_NAME*.jar" ! -name "*-sources.jar" |xargs ls -t|head -n 1`
if [ "x$file" == "x" ]; then
    echo "#### not find jar"
    exit 0
fi

if [ ! -d "$SERVICE_HOME" ]; then
  mkdir "$SERVICE_HOME"
fi

if [ ! -d "$CONFIG_HOME" ]; then
  mkdir "$CONFIG_HOME"
fi

sudo systemctl stop $SERVICE_NAME.service
if [ $? -ne 0 ]; then
    echo "#### shutdown failure"
else
    echo "### shutdown success..."
fi
sleep 5s

TEMP_PID=`ps -ef | grep -w "$SERVICE_NAME" | grep  -v "grep" | awk '{print $2}'`
if [ "$TEMP_PID" == "" ]; then
    echo "#### $SERVICE_NAME process not exists or stop success"
else
    echo "#### $SERVICE_NAME process pid is:$TEMP_PID"
    kill -9 $TEMP_PID
fi

cp -f /data/source/$SERVICE_NAME/application.yml /data/config/$SERVICE_NAME
ln -sf $file  /data/opt/$SERVICE_NAME/$SERVICE_NAME.jar
echo "### file linked."

sudo systemctl start $SERVICE_NAME.service
echo '#### deploy end'

服务器脚本

请移步:《Linux-systemctl启停项目》juejin.cn/editor/draf…

2. nodejs(light-web-dev)

build

  • execute shell
echo `pwd`;
cd tool/packager;
cnpm install;
npm run pack;

Post-build Actions

  • send build artifacts over SSH

Source files:dist/*.zip Remove prefix:dist/ Remove directory:light-web Exec command:

#!/bin/bash
SERVICE_NAME="light-web"
SERVICE_HOME="/data/opt/$SERVICE_NAME"

source /etc/profile
cd /data/opt/$SERVICE_NAME;
echo '### stop...'
pm2 delete $SERVICE_NAME;
#sleep 5;
file=`find /data/source/$SERVICE_NAME  -name "$SERVICE_NAME*.zip" |xargs ls -t|head -n 1`
if [ "x$file" == "x" ]; then
    echo "#### not find zip"
    exit 0
fi
folder=`basename $file .zip`

rm -rf $SERVICE_HOME/$SERVICE_NAME;
rm -rf /data/source/$SERVICE_NAME/$folder;
unzip $file -d /data/source/$SERVICE_NAME/$folder;

ln -s /data/source/$SERVICE_NAME/$folder $SERVICE_NAME;

cd $SERVICE_HOME/$SERVICE_NAME;
cp app.conf-example.js app.conf.js;
echo '### start...'
sh install.sh;