jenkins的pipeline简单编写
作者:互联网
前端发布脚本
pipeline {
agent any
environment {
git_url = 'http://192.168.1.2/web/test.git'
deployenv = 'test' //部署环境
servicename = 'test' //定义服务名称
}
parameters {
// git参数
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH', type: 'PT_BRANCH'
}
stages {
stage('git colne') {
steps {
git branch: "${params.BRANCH}", credentialsId: 'e32346ef-255b-407a-9425-asdsa2546s5da', url: "${git_url}" // 获取最新代码
}
}
stage('nodejs build ') {
steps {
script {
// 执行shell命令
sh '''
ls -a
'''
}
nodejs('nodejs') {
sh 'npm install --registry=https://registry.npm.taobao.org'
sh 'npm run build'
}
script {
sh "tar -czvf ${servicename}.dist.tar.gz dist"
sh "ansible front -m file -a \"path=/home/www/${servicename} state=absent \""
sh "ansible front -m file -a \"path=/home/www/${servicename} state=directory\" "
sh "ansible front -m file -a \"path=/opt/${servicename}.dist.tar.gz state=absent \""
sh "ansible front -m synchronize -a \"src=${workspace}/${servicename}.dist.tar.gz dest=/opt\""
sh "ansible front -m shell -a \"tar -zxf /opt/${servicename}.dist.tar.gz -C /home/www/${servicename}\""
}
}
}
}
post{
success{
wrap([$class: 'BuildUser']) {
script {
//获取当前登录用户账户、姓名、邮箱
Applier_name = "${env.BUILD_USER}"
}
}
// 集成钉钉发送消息
dingtalk (
robot: '98663a2f-ab36-4b50-96df-295345090a10',
type:'LINK',
atAll: false,
title: "success: ${JOB_NAME}",
messageUrl: '${BUILD_URL}',
text: ["- 成功构建:${JOB_NAME}项目!\n- 执行人:${Applier_name}\n- 任务ID:${BUILD_ID}"]
)
}
failure {
wrap([$class: 'BuildUser']) {
script {
//获取当前登录用户账户、姓名、邮箱
Applier_name = "${env.BUILD_USER}"
}
}
dingtalk (
robot: '98663a2f-ab36-4b50-96df-295345090a10',
type:'LINK',
atAll: false,
title: "fail: ${JOB_NAME} ${BUILD_ID}",
messageUrl: '${BUILD_URL}',
text: ["- 构建失败:${JOB_NAME}项目!\n - 执行人:${Applier_name}"]
)
}
}
}
后端发布脚本
def createVersion() {
// 定义一个版本号作为当次构建的版本,输出结果 20191210175842_69
// return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
return new Date().format('yyyyMMdd') + "_${env.BUILD_ID}"
}
pipeline {
agent any
environment {
buildTime = createVersion() //获取时间戳
imageurl = '192.168.1.2:8443/' //定义镜像url
git_url = 'http://192.168.1.2/test/test.git'
git_branch = 'feature/devops' //git 分支号
deployenv = 'test' //部署环境
nacosserver = '192.168.1.3:8848'
servicename = 'test' //定义服务名称
serviceport = '8080' // 服务端口号
}
stages {
stage('git colne') {
steps {
git branch: "${git_branch}", credentialsId: 'e32346ef-255b-407a-9425-5684dsa58554d6', url: "${git_url}" // 获取最新代码
script{
service_v = readMavenPom().getVersion() // 获取maven版本号
imagename = "${imageurl}${deployenv}/${servicename}:${service_v}_${buildTime}" //镜像名称
}
}
}
stage('maven清理并编译') {
steps {
script {
// 删除旧镜像,防止镜像缓存导致更新镜像不成功
sh """
imageid=$(docker images | grep "${imageurl}${deployenv}/${servicename}" | awk '{print $3}')
if [ "$imageid" != "" ];then
docker rmi $imageid
fi
"""
}
withMaven(maven: 'maven3') {
sh 'mvn clean compile package -U -DskipTests=true'
}
}
}
stage('create dockerfile') {
steps {
// 动态生产dockerfile
sh """
echo '''FROM 192.168.1.2:8443/library/jdk:201
WORKDIR /home
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY *-bootstrap/target/*-bootstrap-*.jar ${servicename}-bootstrap.jar
EXPOSE ${serviceport}
ENV JAVA_TOOL_OPTIONS="-Xms512m -Xmx512m -Dspring.profiles.active=${deployenv} -Dspring.cloud.nacos.server-addr=${nacosserver}"
ENTRYPOINT java -jar \$JAVA_TOOL_OPTIONS ${servicename}-bootstrap.jar
''' > Dockerfile
"""
}
}
stage('docker build and push register') {
steps {
script {
echo "${imagename}"
//构建镜像并推送到harbor仓库
sh "docker build -t ${imagename} ."
sh "docker push ${imagename}"
}
}
}
stage('ansible run docker container') {
steps {
// 运行docker run
script{
echo "delploy to new test env"
sh """
echo '''version: "3"
services:
${servicename}:
image: ${imagename}
container_name: ${servicename}
hostname: ${servicename}
#ports:
# - "${serviceport}:${serviceport}"
volumes:
- "/home/app_logs:/root/logs/app"
- "/etc/localtime:/etc/localtime"
restart: on-failure
network_mode: "host"
#environment:
#PROFILE: q
deploy:
resources:
limits:
cpus: '1'
memory: '1g'
reservations:
cpus: '0.5'
memory: '1g'
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:${serviceport}/actuator/health || exit 1"]
interval: "60s"
timeout: "3s"
start_period: "5s"
retries: 3''' >docker-${servicename}.yml
"""
echo "ansible push docker-compose file to test env"
sh "ansible ${deployenv} -m synchronize -a \"src=${workspace}/docker-${servicename}.yml dest=/opt\""
sh "ansible ${deployenv} -m shell -a \"docker-compose -f /opt/docker-${servicename}.yml up -d && docker image prune -a -f \""
}
}
}
}
post{
success{
wrap([$class: 'BuildUser']) {
script {
//获取当前登录用户账户、姓名、邮箱
Applier_name = "${env.BUILD_USER}"
}
}
dingtalk (
robot: '98663a2f-ab36-4b50-96df-25661232516',
type:'LINK',
atAll: false,
title: "success: ${JOB_NAME}",
messageUrl: '${BUILD_URL}',
text: ["- 成功构建:${JOB_NAME}项目!\n- 执行人:${Applier_name}\n- 任务ID:${BUILD_ID}"]
)
}
failure {
wrap([$class: 'BuildUser']) {
script {
//获取当前登录用户账户、姓名、邮箱
Applier_name = "${env.BUILD_USER}"
}
}
dingtalk (
robot: '98663a2f-ab36-4b50-96df-25661232516',
type:'LINK',
atAll: false,
title: "fail: ${JOB_NAME} ${BUILD_ID}",
messageUrl: '${BUILD_URL}',
text: ["- 构建失败:${JOB_NAME}项目!\n - 执行人:${Applier_name}"]
)
}
}
}
标签:pipeline,git,name,sh,servicename,BUILD,编写,jenkins,docker 来源: https://www.cnblogs.com/pgy674477230/p/16350965.html