jenkins自动化pipeline(一)
配置pipeline
-
配置版本号参数化
image.png -
配置pipeline脚本
image.png
#!groovy
pipeline{
agent {label "masterAgent"}
tools {
maven 'maven3.6.3'
}
environment {
dockerRegistryUrl = "registry.cn-hangzhou.aliyuncs.com/weeki/demo1"
dockerHarbor = "registry.cn-hangzhou.aliyuncs.com"
appName = "demo"
image = "${dockerRegistryUrl}/${appName}"
scmBranch=getScmBranch(params.demo_version)
// harbor 凭证,需提前配置好
HARBOR_AUTH=credentials('docker_devops')
// 本次构建版本号
current_build_version=getCurrentBranchVersion(params.demo_version)
// 企业微信机器人通知地址
robtLink="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=******"
lastMsg=''
BUILD_USER=''
}
options {
timestamps() // 日志会有时间
timeout(time:1,unit:'HOURS') //设置流水线超时时间
}
stages{
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: scmBranch]], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: 'http://gitee.com/weeki/demo.git']]])
}
}
stage('构建镜像并上传') {
steps {
script {
def mvnCommend = "mvn clean package jib:build \
-Djib.to.image=${image}:${current_build_version} \
-Djib.from.image=registry.cn-hangzhou.aliyuncs.com/weeki/demo1/jre:8 "
sh (script:mvnCommend+'-Djib.to.auth.username=$HARBOR_AUTH_USR\
-Djib.to.auth.password=$HARBOR_AUTH_PSW')
}
}
}
stage('服务升级'){
agent {label "agent1"}
steps{
script {
sh "sed -ie 's/image: registry.cn-hangzhou.aliyuncs.com\\/weeki\\/demo1:.*/image: registry.cn-hangzhou.aliyuncs.com\\/weeki\\/demo1:${current_build_version}/g' /data/docker-compose.yaml"
}
}
}
stage('服务启动'){
agent {label "agent1"}
steps{
script {
sh "docker-compose -f /data/docker-compose.yaml stop ${appName}"
sh "docker login --username=$HARBOR_AUTH_USR ${dockerHarbor} --password=$HARBOR_AUTH_PSW"
sh "docker-compose -f /data/docker-compose.yaml up -d ${appName}"
echo "启动完成!"
}
}
}
stage('系统检查'){
agent {label "agent1"}
steps{
script {
echo "开始检查。。。"
sh "sleep 30"
def ServStatus = sh(script: "docker ps -a|grep '0.0.0.0:8080'|grep Up", returnStatus: true)
if (ServStatus == 0) {
echo "服务正常运行中"
}else {
echo "服务异常..."
exit 1
}
}
}
}
stage('发送微信通知') {
steps {
script {
wrap([$class: 'BuildUser']) {
script {
BUILD_USER = "${env.BUILD_USER}"
}
}
def changeMsgs = getCommitMessage();
sh "curl '${robtLink}' -H 'Content-Type: application/json' -d '{ \"msgtype\": \"markdown\", \"markdown\": { \"content\": \"系统升级消息\n >动作:<font color='\"info\"'>镜像构建并升级agent1完成</font>\n >服务名:<font color='\"info\"'>后端【demo1】</font>\n >版本号:<font color='\"comment\"'> '\"${params.demo_version}\"'</font>\n >构建号:<font color='\"comment\"'> '\"${env.BUILD_ID}\"'</font>\n >构建人:<font color='\"comment\"'> '\"${BUILD_USER}\"'</font>\n >版本描述:\n<font color='\"comment\"'>'\"${changeMsgs}\"'</font>\"}}'"
}
}
}
}
}
def getCurrentBranchVersion(def branch) {
return "${branch}-${env.BUILD_ID}"
}
def getScmBranch(def version){
return "*/${version}";
}
// 获取修改内容
String getCommitMessage(){
def commitMessage = ""
def formatStr = "%s - 修改人:%s\\r\\n";
for ( changeLogSet in currentBuild.changeSets){
if (commitMessage.length() <= 0){
commitMessage = "\\r\\n";
}
for (entry in changeLogSet.getItems()){
commitMessage += String.format(formatStr, entry.msg, entry.author.fullName);
}
}
if (commitMessage.length() <= 0){
commitMessage = "没有任何修改\\r\\n";
}
return commitMessage
}
配置docker habor 凭证
image.pngdocker-compose.yaml配置
按需配置就可以了,
- 镜像id,pipeline升级会自动更新成最新的版本号;
- 容器名,配合pipeline中使用的容器名(appName = "demo")
网友评论