pipeline {
agent any
environment {
imagename = ‘镜像名’
tag = “v${BUILD_NUMBER}” #版本号
}
stages {
stage(‘Pull code’) {
steps {
echo “
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag}”
git credentialsId: ‘git_key’, url: ‘git地址’
}
}
stage(‘Prepare’) {
steps {
#修改版本
sh “sed -i ‘s/VERSION: “[0-9]*”/VERSION: “${BUILD_NUMBER}”/g’ config/cdn.cfg.js”
sh ‘cat config/cdn.cfg.js’
}
}
stage(‘Clean local history images’) {
steps {
#清理仓库中旧的镜像
sh ‘docker images | grep ${imagename} | awk ‘{print $1":"$2}’ | xargs docker rmi || echo “DONE”’
}
}
stage(‘Build docker image’) {
steps {
#打包项目到镜像并作标签
sh ‘docker build --tag=
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag} .’
sh ‘docker tag
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag} 仓库地址/
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag}’
sh ‘docker tag
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag} 仓库地址/${imagename}:latest’
}
}
stage(‘Push docker image’) {
steps {
#上传到镜像仓库
sh ‘docker push 仓库地址/
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag}’
sh ‘docker push 仓库地址/${imagename}:latest’
}
}
stage (‘Deploy’) {
steps {
script {
def remote = [:]
remote.name = ‘服务器命名’
remote.host = ‘项目机器IP’
remote.user = ‘user’
remote.password = ‘passwd’
remote.allowAnyHosts = true
#删除本地旧镜像,下载新镜像
sshCommand remote: remote, command: “docker rmi 仓库地址/
i
m
a
g
e
n
a
m
e
:
l
a
t
e
s
t
∣
∣
e
c
h
o
′
D
O
N
E
′
"
s
s
h
C
o
m
m
a
n
d
r
e
m
o
t
e
:
r
e
m
o
t
e
,
c
o
m
m
a
n
d
:
"
d
o
c
k
e
r
p
u
l
l
仓
库
地
址
/
{imagename}:latest || echo 'DONE'" sshCommand remote: remote, command: "docker pull 仓库地址/
imagename:latest∣∣echo′DONE′" sshCommandremote:remote,command:"dockerpull仓库地址/{imagename}:latest”
sshCommand remote: remote, command: “docker pull 仓库地址/
i
m
a
g
e
n
a
m
e
:
{imagename}:
imagename:{tag}”
}
}
}
stage (‘Restart service’) {
steps {
script {
def remote = [:]
remote.name = ‘服务器命名’
remote.host = ‘项目机器IP’
remote.user = ‘user’
remote.password = ‘passwd’
remote.allowAnyHosts = true
#停止旧的容器并删除,启动新容器
sshCommand remote: remote, command: “docker stop ${imagename} || echo ‘DONE’”
sshCommand remote: remote, command: “docker rm ${imagename} || echo ‘DONE’”
sshCommand remote: remote, command: “docker run -td --name
i
m
a
g
e
n
a
m
e
−
p
7003
:
80
仓
库
地
址
/
{imagename} -p 7003:80 仓库地址/
imagename−p7003:80仓库地址/{imagename}:latest”
}
}
}
stage (‘Clean history docker images on romote server’) {
steps {
script {
def remote = [:]
remote.name = ‘服务器命名’
remote.host = ‘项目IP’
remote.user = ‘user’
remote.password = ‘passwd’
remote.allowAnyHosts = true
#删除旧版本
sshCommand remote: remote, command: “docker images 仓库地址/KaTeX parse error: Can't use function '\"' in math mode at position 22: …name} --filter \̲"̲before=仓库地址/{imagename}:latest” -q | xargs docker rmi || echo ‘DONE’"
}
}
}
}
#配置bearychat报警
post {
unstable {
bearychatSend color: ‘red’, message: “KaTeX parse error: Expected 'EOF', got '}' at position 27: …ME},构建不稳定" }̲ failure { …{env.JOB_NAME},构建失败”
}
}
}
dockerfile__________
FROM node:10.16.2 as build-stage
ADD . /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
#RUN npm install --ignore-scripts --unsafe-perm
RUN npm install --registry=http://registry.npm.taobao.org --ignore-scripts --unsafe-perm
RUN npm run build
RUN gulp publish:production
FROM nginx:stable-alpine as production-stage
COPY config/nginx/nginx.conf /etc/nginx/conf.d/app.conf
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD [“nginx”, “-g”, “daemon off;”]