大家好,又见面了,我是你们的朋友全栈君。
前言:这篇文章主要讲一讲基于jenkins的CICD使用
基于jenkins的CICD使用
一、简介
为了提升线下测试效率,缩短测试时间,提升提测代码质量,规范流程,缩短测试准备和执行时间,缩短问题定位时间,提供预测性指标,规范CICD流程,以提升整体团队效率。
二、CICD流程图
三、CICD效果
大致可查看到的job如下:
点击其中可查看具体编译情况:
四、jenkins 编译方式
五、新创建job
1、进入视图页面,找到对应的视图列表,点击左侧new item 新建job
2、填写对应的job名称,选择流水线模式,并勾选添加到当前视图中,点击ok
3、填写对应的job描述信息,配置对应的项目构建历史保留数
4、配置对应的pipeline 脚本
六、pipeline 脚本说明
1、开发环境
- pipeline 脚本参考
pipeline {
agent any
environment {
PATH = "/usr/local/git/bin:/opt/maven/apache-maven-3.6.3/bin:/opt/java/jdk1.8.0_221/bin:/usr/local/python/bin:$PATH"
GIT_URL = "https://xxxx.git"
BRANCH = "dev"
API_DIR = "xxx"
PROVIDER_DIR = "xxx"
WEB_DIR = ”xxx" DEPLOY_SCRIPTS_PID = "xxxx" DEPLOY_SCRIPTS_PORT = "xxxx" DEPLOY_SCRIPTS_OMO = "xxxx" PKG_NAME = "xxx" PORT = "xxx" } ## 拉取代码 stages { stage('Check out') { steps { echo 'git clone code and checkout' echo '拉取代码到jenkins服务器' checkout([$class: 'GitSCM', branches: [[name: "*/${
BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'scrm_gituser', url: "${
GIT_URL}"]]]) echo '拉取代码完成' echo '获取commit id' script { commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() } sh """
echo "获取到的commit id为: ${commit_id}"
""" } } ## 编译 stage('Build') { steps { echo 'package core' echo '打包core jar包' sh """
mvn -Dmaven.test.skip=true clean package
""" echo '打包core jar包完成' sh """
echo "打成的jar如下"
ls -al target/*.jar
""" } } ## 修改编译后的产物名称 stage('Change package name') { steps { echo 'change package name' echo '修改jar名字' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh "sh /opt/deploy/push_pkg.sh ${
env.JOB_NAME} ${
commit_id}" } } } ## 创建程序运行目录 stage('Create package dist dir') { steps { echo 'Create package dist dir' echo '创建部署jar包的路径' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh """
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@xxxxx 'if [ ! -d "/opt/deploy/${env.JOB_NAME}" ];then mkdir -pv /opt/deploy/${env.JOB_NAME};fi'
""" } echo '创建部署jar包路径完成' echo '部署jar包的路径如下' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh """
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@xxxxxx 'ls -al /opt/deploy/${env.JOB_NAME}'
""" } } } ## 推动包到目标服务器 stage('Push package') { steps { echo 'push package to servers' echo '推送jar包到目标服务器' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh """
scp -i ${
deploy_ssh_key} target/*-${
commit_id}.jar ${
deploy_ssh_user}@xxxx:/opt/deploy/${
env.JOB_NAME}
""" } } } ## 推动运行脚本到目标服务器 stage('Push deploy scripts') { steps { echo 'push deploy scripts to servers' echo '推动运行jar包的脚本到目标服务器' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh """
scp -i ${
deploy_ssh_key} /opt/deploy/${
DEPLOY_SCRIPTS}.sh ${
deploy_ssh_user}@xxxxx:/opt/deploy/${
env.JOB_NAME}
""" } } } ## 部署程序 stage('Deploy') { steps { echo 'deploy package to servers' echo '目标服务器上运行jar包' withCredentials([sshUserPrivateKey(credentialsId: 'jenkins_ssh_key', keyFileVariable: 'deploy_ssh_key', passphraseVariable: '', usernameVariable: 'deploy_ssh_user')]) { sh """
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@xxxxx sh /opt/deploy/${
env.JOB_NAME}/${
DEPLOY_SCRIPTS}.sh ${
commit_id} ${
PROVIDER_PKG_NAME} ${
env.JOB_NAME} ${
PORT}
""" } echo '部署jar包完成' } } } ## 编译状态通知 post { always { withCredentials([usernamePassword(credentialsId: 'jenkins_api_user', passwordVariable: 'jenkins_pwd', usernameVariable: 'jenkins_user')]) { sh """python3 /opt/deploy/notification_jenkins.py ${
jenkins_user} ${
jenkins_pwd} ${
env.JOB_NAME}
"""
}
}
}
}
- 项目编译步骤
- 配置环境变量
- 拉取代码
- 编译(打包和推送到私服)
- 编译产物修改名称(用作部署指定版本)
- 创建程序运行目录
- 推送包到目标服务器
- 推送脚本到目标服务器
- 运行程序
- 反馈编译状态结果
- 环境变量说明
- GIT_URL 拉取项目仓库地址
- BRANCH 分支名称
- API_DIR 对应多模块中api模块目录名
- PROVIDER_DIR 对应多模块中provider模块目录名
- WEB_DIR 对应多模块中web模块目录名
- DEPLOY_SCRIPT_PORT 对应运行程序中有开启端口服务脚本
- DEPLOY_SCRIPT_PID 对应运行程序中没有开启端口服务脚本
- DEPLOY_SCRIPT_OMO 对应OMO系统中运行脚本
- PROVIDER_PKG_NAME 对应provider模块编译生成的jar包名称
- WEB_PKG_NAME 对应web模块编译生成的jar包名称
- OMO_PKG_NAME 对应omo项目core模块编译生成的jar包名称
- FRONT_DIR 对应前端项目目录
- FRONT_PKG_NAME 对应前端包名
- FRONT_CONF 对应前端配置文件
- DEPLOY_SCIPTS_FRONT 对应前端部署脚本
2、测试环境
- pipeline 脚本参考
pipeline {
agent any
## 注入参数
parameters {
choice(
description: '发布指定的分支\n说明:\ntest对应0.2.2版本\ntest1对应0.2.3版本\n默认值test',
name: 'BRANCH',
choices: ['test', 'test1']
)
choice(
description: '发布到指定的节点\n说明:\ntest对应0.2.2版本\ntest1对应0.2.3版本\n默认值test-0.2.2',
name: 'DEPLOY_TO',
choices: ['test-0.2.2', 'test-0.2.3']
)
}
## 注入环境变量
environment {
PATH = "/usr/local/git/bin:/usr/local/src/apache-maven-3.6.3/bin:/usr/local/src/jdk1.8.0_221/bin:/usr/local/python/bin:$PATH"
GIT_URL = ""
BRANCH = "${params.BRANCH}"
NODE = "${params.DEPLOY_TO}"
PROVIDER_DIR = ""
DEPLOY_SCRIPTS = ""
PROVIDER_PKG_NAME = ""
PORT = ""
ENV = ""
}
stages {
## 拉取代码
stage('Check out') {
steps {
echo 'git clone code and checkout'
echo '拉取代码到jenkins服务器'
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'scrm_gituser', url: "${GIT_URL}"]]])
echo '拉取代码完成'
echo '获取commit id'
script {
commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
sh """ echo "获取到的commit id为: ${
commit_id}" """
}
}
## 编译
stage('Build') {
steps {
echo 'deply api to nexus'
echo '将api包打包并上传到nexus'
echo 'package core'
sh """ mvn -Dmaven.test.skip=true clean deploy """
echo '上传api包到私服完成'
echo '打包core jar包完成'
sh """ echo "打成的jar如下" ls -al ${PROVIDER_DIR}/target/*.jar """
}
}
## 修改名称
stage('Change package name') {
steps {
echo 'change package name'
echo '修改jar名字'
echo "${commit_id}"
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_change_pkg',
extraVars:
[
PROVIDER_DIR: "${PROVIDER_DIR}",
job_name: "${env.JOB_NAME}",
commit_id: "${commit_id}",
]
)
}
}
## 创建部署目录
stage('Create package dist dir') {
steps {
echo 'Create package dist dir'
echo '创建部署jar包的路径'
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_create_pkg_dir',
extraVars: [ PROVIDER_DIR: "${env.JOB_NAME}"]
)
echo '创建部署jar包路径完成'
echo '部署jar包的路径如下'
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_list_pkg_dir',
extraVars: [ PROVIDER_DIR: "${env.JOB_NAME}"]
)
}
}
## 推动jar包
stage('Push package') {
steps {
echo 'push package to servers'
echo '推送jar包到目标服务器'
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_push_pkg',
extraVars:
[
PROVIDER_DIR: "${PROVIDER_DIR}",
job_name: "${env.JOB_NAME}",
commit_id: "${commit_id}",
]
)
}
}
## 推送运行脚本
stage('Push deploy scripts') {
steps {
echo 'push deploy scripts to servers'
echo '推动运行jar包的脚本到目标服务器'
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_push_scripts',
extraVars:
[
DEPLOY_SCRIPTS: "${DEPLOY_SCRIPTS}",
job_name: "${env.JOB_NAME}"
]
)
}
}
## 部署
stage('Deploy') {
steps {
echo 'deploy package to servers'
echo '目标服务器上运行jar包'
ansiblePlaybook(
become: true,
credentialsId: 'jenkins_ssh_key',
disableHostKeyChecking: true,
forks: 10,
inventory: "/opt/deploy/test_scrm/inventory/${NODE}.yml",
playbook: '/opt/deploy/test_scrm/playbook/deploy.yml',
sudoUser: null,
tags: 'backend_deploy_omo',
extraVars:
[
DEPLOY_SCRIPTS: "${DEPLOY_SCRIPTS}",
job_name: "${env.JOB_NAME}",
commit_id: "${commit_id}",
PROVIDER_PKG_NAME: "${PROVIDER_PKG_NAME}",
ENV: "${ENV}",
PORT: "${PORT}"
]
)
echo '部署jar包完成'
}
}
}
## 发送通知
// post {
// always {
// withCredentials([usernamePassword(credentialsId: 'jenkins_api_user', passwordVariable: 'jenkins_pwd', usernameVariable: 'jenkins_user')]) {
// sh """python3 /opt/deploy/notification_jenkins.py ${jenkins_user} ${jenkins_pwd} ${env.JOB_NAME} // """
// }
// }
// }
}
- 项目编译步骤
- 配置构建参数
- 配置环境变量
- 拉取代码
- 编译(打包和推送到私服)
- 编译产物修改名称(用作部署指定版本)
- 创建程序运行目录
- 推送包到目标服务器
- 推送脚本到目标服务器
- 运行程序
- 反馈编译状态结果
- 环境变量说明
- GIT_URL 拉取项目仓库地址
- BRANCH 分支名称
- API_DIR 对应多模块中api模块目录名
- PROVIDER_DIR 对应多模块中provider模块目录名
- WEB_DIR 对应多模块中web模块目录名
- DEPLOY_SCRIPT_PORT 对应运行程序中有开启端口服务脚本
- DEPLOY_SCRIPT_PID 对应运行程序中没有开启端口服务脚本
- DEPLOY_SCRIPT_OMO 对应OMO系统中运行脚本
- PROVIDER_PKG_NAME 对应provider模块编译生成的jar包名称
- WEB_PKG_NAME 对应web模块编译生成的jar包名称
- OMO_PKG_NAME 对应omo项目core模块编译生成的jar包名称
- FRONT_DIR 对应前端项目目录
- FRONT_PKG_NAME 对应前端包名
- FRONT_CONF 对应前端配置文件
- DEPLOY_SCIPTS_FRONT 对应前端部署脚本
3、新建项目需要修改的地方
- 拉取代码
需要将git地址改成对应仓库的地址,对应分支改为正确的,修改的变量为GIT_URL,BRANCH - 编译阶段
如果是多模块需要将对应代码区块中的模块名改为对应的模块名称,需要改动三个环境变量, API_DIR | PROVIDER_DIR | WEB_DIR,有web服务添加WEB_DIR环境变量 - 部署阶段
# 如果是新scrm项目(无端口绑定),部署命令如下
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@192.168.0.245 sh /opt/deploy/${
env.JOB_NAME}/${
DEPLOY_SCRIPTS_PID}.sh ${
commit_id} ${
PROVIDER_PKG_NAME} ${
env.JOB_NAME}
# 如果是新scrm项目(有端口绑定),部署命令如下
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@192.168.0.245 sh /opt/deploy/${
DEPLOY_SCRIPTS_PORT}.sh ${
commit_id} ${
PROVIDER_PKG_NAME} ${
env.JOB_NAME} ${
PORT}
# 如果是omo项目,部署命令如下
ssh -i ${
deploy_ssh_key} ${
deploy_ssh_user}@192.168.0.245 sh /opt/deploy/${
DEPLOY_SCRIPTS_OMO}.sh ${
commit_id} ${
OMO_PKG_NAME} ${
env.JOB_NAME} ${
PORT}
七、疑问解答与加群交流学习
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130530.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...