持续集成交付CICD:Jenkins使用基于SaltStack的CD流水线部署前后端应用

本文主要是介绍持续集成交付CICD:Jenkins使用基于SaltStack的CD流水线部署前后端应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、实验

1.Jenkins使用基于SaltStack的CD流水线部署后端应用

2.Jenkins使用基于SaltStack的CD流水线部署前端应用


一、实验

1.Jenkins使用基于SaltStack的CD流水线部署后端应用

(1)GitLab添加Token

(2)Jenkins添加凭据

(3)devops03-env 添加脚本

# sh service.sh devops03-devops-service 1.1.1 9099 start
APPNAME=$1
VERSION=$2
PORT=$3start(){port_result=`netstat -anlpt | grep "${PORT}" || echo false`if [[ $port_result == "false" ]];thennohup java -jar -Dserver.port=${PORT}  ${APPNAME}-${VERSION}.jar >${APPNAME}.log.txt 2>&1 &elsestopsleep 5nohup java -jar -Dserver.port=${PORT}  ${APPNAME}-${VERSION}.jar >${APPNAME}.log.txt 2>&1 &fi
}stop(){pid=`netstat -anlpt | grep "${PORT}" | awk '{print $NF}' | awk -F '/' '{print $1}'`kill -15 $pid
}check(){proc_result=`ps aux | grep java | grep "${APPNAME}" | grep -v grep || echo false`port_result=`netstat -anlpt | grep "${PORT}" || echo false`url_result=`curl -s http://localhost:${PORT} || echo false `if [[ $proc_result == "false" || $port_result == "false" || $url_result == "false" ]];thenecho "error"elseecho "ok"fi
}case $4 instart)startsleep 5check;;stop)stopsleep 5check;;restart)stopsleep 5startsleep 5check;;check)check;;*)echo "sh service.sh {start|stop|restart|check}";;
esac

(4)Jenkins给后端项目CD流水线添加参数

(5)Jenkins安装插件HTTP Request

(5) 修改流水线代码

pipeline {agent { label "build" }stages{stage("PullArtifacts"){steps{script{repoName = "${JOB_NAME}".split("/")[0]env.projectName ="${JOB_NAME}".split("/")[-1].split("_")[0]if ("${env.projectType}" == "maven"){type="jar"}if ("${env.projectType}" == "npm"){type="tar.gz"}PullArtifacts("${env.releaseVersion}","${env.projectName}",repoName,type)env.pkgName="${env.projectName}-${env.releaseVersion}.${type}"}}}stage("DeployHost"){steps{script{print("DeployHost")// 文件内容写到本地response = GetRepoFile(21, "service.sh", "master")writeFile file: 'service.sh', text: "${response}"sh "ls -a "if ("${env.deployTool}" == "saltstack"){targetHosts = "${env.saltHosts}"println(targetHosts)localDeployDir = "/srv/salt/${env.projectName}"sh """[ -d ${localDeployDir} ] || mkdir -p ${localDeployDir}mv service.sh ${env.pkgName} ${localDeployDir}# 清理发布目录salt -L "${targetHosts}" cmd.run  "rm -fr ${targetDir}/${env.projectName}/* &&  mkdir -p ${targetDir}/${env.projectName} || echo file is exists"# 发布应用salt -L "${targetHosts}" cp.get_file salt://${env.projectName}/${env.pkgName} ${targetDir}/${env.projectName}# 发布启动脚本salt -L "${targetHosts}" cp.get_file salt://${env.projectName}/service.sh ${targetDir}/${env.projectName}# 启动服务salt -L "${targetHosts}" cmd.run  "cd ${targetDir}/${env.projectName} ;source /etc/profile  && sh service.sh ${env.projectName} ${env.releaseVersion} ${env.port} start"# 检查服务sleep 5salt -L "${targetHosts}" cmd.run  "cd ${targetDir}/${env.projectName} ;source /etc/profile  && sh service.sh ${env.projectName} ${env.releaseVersion} ${env.port} check""""}}}}stage("ServiceCtrl"){steps{script{print("ServiceCtrl")}}}stage("HealthCheck"){steps{script{print("HealthCheck")}}}}
}//下载制品
def PullArtifacts(version,projectName,repoName,type){repoUrl = "http://192.168.204.13:8081/repository"pkgPath = "${repoUrl}/${repoName}/${projectName}/${version}/${projectName}-${version}.${type}"sh "wget --http-user=admin --http-passwd=Admin123 ${pkgPath} -q"
}// 封装HTTP
def HttpReq(reqType, reqUrl,reqBody ){def gitServer = "http://192.168.204.8:82/api/v4"withCredentials([string(credentialsId: '02dce3ff-4e46-4de2-b079-5dd6093d4f64', variable: 'GITLABTOKEN')]) {response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',consoleLogResponseBody: true,contentType: 'APPLICATION_JSON_UTF8',customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],httpMode: "${reqType}",url: "${gitServer}/${reqUrl}",wrapAsMultipart: false,requestBody: "${reqBody}"}return response
}//获取文件内容
def GetRepoFile(projectId,filePath, branchName ){//GET /projects/:id/repository/files/:file_path/rawapiUrl = "/projects/${projectId}/repository/files/${filePath}/raw?ref=${branchName}"response = HttpReq('GET', apiUrl, "")return response.content
}

(6)手动构建流水线

(7)watch命令观察

# watch -n 1 "ps aux | grep 9099"

(8) 成功

(9) GitLab后端项目新建分支

(10)Jenkins后端项目CI流水线手动构建

(11) 成功

(12)Nexus查看

(13)Jenkins手动构建后端项目CD流水线

(14)成功

(15)查看本地目录

# cd /optdevops03-devops-service/
# ls

2.Jenkins使用基于SaltStack的CD流水线部署前端应用

(1)主机安装nginx

1)添加 nginx 到 yum 源中
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm2)安装 nginx (在把nginx添加到 yum 源之后,就可以使用 yum 安装了)
sudo yum install -y nginx3)稍等一会,即可安装完成4)启动 nginx
sudo systemctl start nginx.service5)设置 nginx 开机自启动
sudo systemctl enable nginx.service

(2)nginx 配置信息


1)网站文件存放默认位置(Welcome to nginx 页面)
/usr/share/nginx/html2)网站默认站点配置
/etc/nginx/conf.d/default.conf3)自定义 nginx 站点配置文件存放目录
/etc/nginx/conf.d/4)nginx 全局配置文件
/etc/nginx/nginx.conf5)启动 nginx
service nginx start6)关闭 nginx
service nginx stop7)重启 nginx
service nginx restart
service nginx reload8) 加成nginx
nginx -t

(3)Jenkins新建前端项目CD流水线

(4)添加参数

(5)修改nginx配置文件

# /etc/nginx/nginx.conf
  1 2 user  nginx;3 worker_processes  auto;4 5 error_log  /var/log/nginx/error.log notice;6 pid        /var/run/nginx.pid;7 8 9 events {10     worker_connections  1024;11 }12 13 14 http {15     include       /etc/nginx/mime.types;16     default_type  application/octet-stream;17 18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '19                       '$status $body_bytes_sent "$http_referer" '20                       '"$http_user_agent" "$http_x_forwarded_for"';21 22     access_log  /var/log/nginx/access.log  main;23 24     sendfile        on;25     tcp_nopush      on;26     tcp_nodelay     on;27     types_hash_max_size 2048;28 29 30     keepalive_timeout  65;31 32     #gzip  on;33 34     include /etc/nginx/conf.d/*.conf;35 36 37 server {38     listen 8099;39     server_name _;40     root /usr/share/nginx/html/devops03-devops-ui;41 42     # Load configuration files for the default server block.43     include /etc/nginx/default.d/*.conf;44 45     location / {46     }47 48     error_page 404 /404.html;49         location = /40x.html {50     }51 52     error_page 500 502 503 504 /50x.html;53         location = /50x.html {54     }55  }

# nginx -t# curl 127.0.0.1:8099

验证:

(6)修改前端项目CD流水线代码

pipeline {agent { label "build" }stages{stage("PullArtifacts"){steps{script{repoName = "${JOB_NAME}".split("/")[0]env.projectName ="${JOB_NAME}".split("/")[-1].split("_")[0]if ("${env.projectType}" == "maven"){type="jar"}if ("${env.projectType}" == "npm"){type="tar.gz"}PullArtifacts("${env.releaseVersion}","${env.projectName}",repoName,type)env.pkgName="${env.projectName}-${env.releaseVersion}.${type}"}}}stage("DeployHost"){steps{script{print("DeployHost")if ("${env.deployTool}" == "saltstack"){targetHosts = "${env.saltHosts}"println(targetHosts)localDeployDir = "/srv/salt/${env.projectName}"sh """[ -d ${localDeployDir} ] || mkdir -p ${localDeployDir}mv ${env.pkgName} ${localDeployDir}# 清理发布目录salt -L "${targetHosts}" cmd.run  "rm -fr ${targetDir}/${env.projectName}/* &&  mkdir -p ${targetDir}/${env.projectName} || echo file is exists"# 发布应用salt -L "${targetHosts}" cp.get_file salt://${env.projectName}/${env.pkgName} ${targetDir}/${env.projectName}"""}}}}stage("ServiceCtrl"){steps{script{print("ServiceCtrl")localDeployDir = "/srv/salt/${env.projectName}"if ("${env.projectType}" == "maven") {// 文件内容写到本地response = GetRepoFile(21, "service.sh", "master")writeFile file: 'service.sh', text: "${response}"sh "ls -a "sh """mv service.sh  ${localDeployDir}# 发布启动脚本salt -L "${targetHosts}" cp.get_file salt://${env.projectName}/service.sh ${targetDir}/${env.projectName}# 启动服务salt -L "${targetHosts}" cmd.run  "cd ${targetDir}/${env.projectName} ;source /etc/profile  && sh service.sh ${env.projectName} ${env.releaseVersion} ${env.port} start"# 检查服务sleep 5salt -L "${targetHosts}" cmd.run  "cd ${targetDir}/${env.projectName} ;source /etc/profile  && sh service.sh ${env.projectName} ${env.releaseVersion} ${env.port} check""""}if ("${env.projectType}" == "npm") {sh """# 启动服务salt -L "${targetHosts}" cmd.run  "cd ${targetDir}/${env.projectName} ;tar zxf ${env.pkgName}""""}}}}stage("HealthCheck"){steps{script{print("HealthCheck")}}}}
}//下载制品
def PullArtifacts(version,projectName,repoName,type){repoUrl = "http://192.168.204.13:8081/repository"pkgPath = "${repoUrl}/${repoName}/${projectName}/${version}/${projectName}-${version}.${type}"sh "wget --http-user=admin --http-passwd=Admin123 ${pkgPath} -q"
}// 封装HTTP
def HttpReq(reqType, reqUrl,reqBody ){def gitServer = "http://192.168.204.8:82/api/v4"withCredentials([string(credentialsId: '02dce3ff-4e46-4de2-b079-5dd6093d4f64', variable: 'GITLABTOKEN')]) {response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',consoleLogResponseBody: true,contentType: 'APPLICATION_JSON_UTF8',customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],httpMode: "${reqType}",url: "${gitServer}/${reqUrl}",wrapAsMultipart: false,requestBody: "${reqBody}"}return response
}//获取文件内容
def GetRepoFile(projectId,filePath, branchName ){//GET /projects/:id/repository/files/:file_path/rawapiUrl = "/projects/${projectId}/repository/files/${filePath}/raw?ref=${branchName}"response = HttpReq('GET', apiUrl, "")return response.content
}

(7)Jenkins手动构建前端项目CD流水线

(8)成功

(9)GitLab前端项目新建分支

(10)Jenkins手动构建前端项目CI流水线

(11)成功

(12)Nexus查看

(13)Jenkins手动构建前端项目CD流水线

(14)命令观察

# watch -n 1 "ls devops03-devops-ui"

# for i in `seq 1000`; do sleep 1 ; curl http://127.0.0.1:8099; echo -e "\n\n\n";done

(15) 查看本地目录

# cd /usr/share/nginx/html/
# ls

这篇关于持续集成交付CICD:Jenkins使用基于SaltStack的CD流水线部署前后端应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/499458

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推