简介
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件项目可以进行持续集成
第一种模式(自由风格)
新建任务—自由风格—确认
1.1 配置参数,保留最大构建10次
1.2 参数化构建,布尔值和字符参数配置,这里字符参数和布尔值后面会调用
1.3 git 参数配置
1.4 编写job脚本,这个是自由风格shell的写法
pwd
if [ $confirm = true ];then
pwd
dir="/home/dingding/s5-iot"
project="s5-iot"
jar_name="pms-interface.jar"
ip="192.168.1.1"
echo "$project 项目编译"
. /etc/profile.d/java.sh
. /etc/profile.d/maven.sh
cd ${WORKSPACE}
mvn -s "/usr/local/maven/conf/settings-test.xml" clean package -Dmaven.test.skip=true
echo "copy $project 项目"
ssh dingding@$ip "if [ ! -e ${project} ]; then mkdir ${project};fi"
ssh dingding@$ip "cd ${project} && if [ -f ${jar_name} ];then rsync -r --delete ${jar_name} ${jar_name}.bak;fi "
cd pms-interface/target
rsync -avz ${jar_name} dingding@$ip:$dir
echo "停止$project 服务"
ssh root@$ip "systemctl stop s5-iot.service "
echo "启动$project 服务"
ssh root@$ip "systemctl start s5-iot.service "
echo "检测$project 服务"
else
echo "no"
fi
第二种模式(流水线语法)
前面几种配置和第一种模式差不多一样,脚本有些变化
pipeline {
agent {
label 'saas3.0_jenkins'
}
environment {
GITURL = 'git@code.dding.net:xxxx/xxxx/xxxxx.git'
CREDID = '2c516a80-c76d-4b14-xxxx-dfdc6882a6c3'
SERVER_DOMAIN = 'openapi.dding.net'
SERVER_DOMAIN_2 = 's3-openapi-in.dding.net'
}
stages {
stage('CheckOut') {
steps {
script {
try {
git credentialsId: env.CREDID,url: env.GITURL
sh '[ -n "${commit_id}" ] && git checkout "${commit_id}"'
sh 'git submodule update --init'
}
catch (exc) {
echo "commit_id of the project do not exist"
sh 'exit 1'
}
}
}
}
stage('remove-lb1') {
steps {
script {
try {
sh 'deploy-kong ${SERVER_DOMAIN} ${SERVERIPONE} ${SERVERPORT} 0'
sh 'deploy-kong ${SERVER_DOMAIN_2} ${SERVERIPONE} ${SERVERPORT} 0'
}
catch (exc) {
echo "remove lb1 fails"
sh 'exit 2'
}
}
}
}
stage('deploy-ecs1') {
steps {
script {
try {
sh 'deploy-saas3.0-openapi ${SERVERIPONE} ${SERVERPORT}'
}
catch (exec) {
echo "deploy ecs1 fails"
sh 'exit 3'
}
}
}
}
stage('add-lb1') {
steps {
script {
try {
sh 'deploy-kong ${SERVER_DOMAIN} ${SERVERIPONE} ${SERVERPORT} 100'
sh 'deploy-kong ${SERVER_DOMAIN_2} ${SERVERIPONE} ${SERVERPORT} 100'
}
catch (exec) {
echo "add lb1 fails"
sh 'exit 4'
}
}
}
}
stage('remove-lb2') {
steps {
script {
try {
sh 'deploy-kong ${SERVER_DOMAIN} ${SERVERIPTWO} ${SERVERPORT} 0'
sh 'deploy-kong ${SERVER_DOMAIN_2} ${SERVERIPTWO} ${SERVERPORT} 0'
}
catch (exec) {
echo "remove lb2 fails"
sh 'exit 2'
}
}
}
}
stage('deploy-ecs2') {
steps {
script {
try {
sh 'deploy-saas3.0-openapi ${SERVERIPTWO} ${SERVERPORT}'
}
catch (exec) {
echo "deploy ecs2 fails"
sh 'exit 3'
}
}
}
}
stage('add-lb2') {
steps {
script {
try {
sh 'deploy-kong ${SERVER_DOMAIN} ${SERVERIPTWO} ${SERVERPORT} 100'
sh 'deploy-kong ${SERVER_DOMAIN_2} ${SERVERIPTWO} ${SERVERPORT} 100'
}
catch (exec) {
echo "add lb2 fails"
sh 'exit 4'
}
}
}
}
}
}
第三种模式(集合发布)
前提 多个任务共用一个commit_id
流水线脚本如下
pipeline {
agent any
stages {
stage('test-saas5-web') {
steps{
build job: 'test-saas5-web', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-dashboard') {
steps{
build job: 'test-saas5-web-dashboard', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-device') {
steps{
build job: 'test-saas5-web-device', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-enterprise') {
steps{
build job: 'test-saas5-web-enterprise', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-setting') {
steps{
build job: 'test-saas5-web-setting', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-source') {
steps{
build job: 'test-saas5-web-source', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-wallet') {
steps{
build job: 'test-saas5-web-wallet', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
stage('test-saas5-web-workorder') {
steps{
build job: 'test-saas5-web-workorder', parameters: [
[$class: 'BooleanParameterValue', name: 'confirm', value: true],
[$class: 'StringParameterValue', name: 'commit_id', value: "${commit_id}"]
]
}
}
}
}
jenkins 流程线高级用法
通过参数,when,if等条件的判断实现对jenkins任务精细的控制,更方便我们进行服务的部署
最后效果如下
编写pipeline语法脚本
def getiplist() {
return ["192.168.1.1","192.168.1.2"]
}
pipeline {
agent {
label 'saas3.0_jenkins'
}
environment {
GITURL = 'git@code.xxxx.net:xxx/yuxin-xxxxx.git'
CREDID = '2c516a80-xxxx-xxxx-xxxxx-xxxx'
ip_1="192.168.1.2"
ip_2="192.168.1.2"
iplst = "192.168.1.1,192.168.1.2"
app1 = "yuxin-xxxx"
app1_dir="yuxin-service/yuxin-service-activity/target/yuxin-activity.jar"
app2="yuxin-gateway"
app2_dir="yuxin-web/yuxin-gateway/target/yuxin-gateway.jar"
app3="yuxin-oa"
app3_dir="yuxin-service/yuxin-service-oa/target/yuxin-oa.jar"
_activity="${sh(script:' echo $app_name|grep -w -o "activity" || echo "error" ', returnStdout: true).trim()}"
_gateway="${sh(script:' echo $app_name|grep -w -o "gateway" || echo "error" ', returnStdout: true).trim()}"
_oa="${sh(script:' echo $app_name|grep -w -o "oa" || echo "error" ', returnStdout: true).trim()}"
}
stages {
stage('pull最新代码') {
steps {
script {
try {
git credentialsId: env.CREDID,url: env.GITURL
sh '[ -n "${commit_id}" ] && git checkout "${commit_id}"'
sh 'git submodule update --init'
}
catch (exc) {
echo "commit_id of the project do not exist"
sh 'exit 1'
}
}
}
}
stage('编译saas5.0') {
steps {
script{
sh '''. /etc/profile.d/java11.sh
. /etc/profile.d/maven.sh
cd ${WORKSPACE}/
mvn -s "/usr/local/maven/conf/settings-s5.xml" clean package -Dmaven.test.skip=true -Plockin-prod
'''
}
}
}
stage("获取部署ip列表") {
steps {
script {
iplist = ["192.168.1.1","192.168.1.2"]
echo "$iplist"
}
}
}
stage('部署01-activity') {
when { environment name: '_activity', value: "activity" }
steps {
script{
println(iplist.size())
for (ip in iplist) {
sh "rsync -avz $app1_dir 'root@$ip:/opt' "
sh "ssh root@$ip 'ls /opt' "
}
}
}
}
stage('部署02-gateway') {
when { environment name: '_gateway', value: "gateway" }
steps {
script{
println(iplist.size())
for (ip in iplist) {
sh "rsync -avz $app2_dir \"root@$ip:/opt/\""
sh "ssh root@$ip \"ls /opt\" "
}
}
}
}
stage('部署03-oa') {
when { environment name: '_oa', value: "oa" }
steps {
script{
println(iplist.size())
for (ip in iplist) {
sh "rsync -avz $app3_dir \"root@$ip:/opt/\""
sh "ssh root@$ip \"ls /opt\" "
}
}
}
}
}
}
在以上的脚本中使用了 when语法进行判断以及 for循环进行遍历执行脚本
执行最终结果如下
能够达到我们最终想要的效果