jenkins_pipeline前端_docker

  1 pipeline {
  2   agent any
  3 
  4   environment {
  5     imagename = '镜像名'
  6     tag = "v${BUILD_NUMBER}" #版本号
  7   }
  8 
  9   stages {
 10     stage('Pull code') {
 11       steps {
 12         echo "${imagename}:${tag}"
 13         git credentialsId: 'git_key', url: 'git地址'
 14       }
 15     }
 16     stage('Prepare') {
 17       steps {
 18         #修改版本
 19         sh "sed -i 's/VERSION: \"[0-9]*\"/VERSION: \"${BUILD_NUMBER}\"/g' config/cdn.cfg.js"
 20         sh 'cat config/cdn.cfg.js'
 21       }
 22     }
 23     stage('Clean local history images') {
 24       steps {
 25         #清理仓库中旧的镜像
 26         sh 'docker images | grep ${imagename} | awk \'{print $1":"$2}\' | xargs docker rmi || echo "DONE"'
 27       }
 28     }
 29 
 30     stage('Build docker image') {
 31       steps {
 32         #打包项目到镜像并作标签
 33         sh 'docker build --tag=${imagename}:${tag} .'
 34         sh 'docker tag ${imagename}:${tag} 仓库地址/${imagename}:${tag}'
 35         sh 'docker tag ${imagename}:${tag} 仓库地址/${imagename}:latest'
 36       }
 37     }
 38 
 39     stage('Push docker image') {
 40       steps {
 41         #上传到镜像仓库
 42         sh 'docker push 仓库地址/${imagename}:${tag}'
 43         sh 'docker push 仓库地址/${imagename}:latest'
 44       }
 45     }
 46 
 47     stage ('Deploy') {
 48       steps {
 49         script {
 50           def remote = [:]
 51           remote.name = '服务器命名'
 52           remote.host = '项目机器IP'
 53           remote.user = 'user'
 54           remote.password = 'passwd'
 55           remote.allowAnyHosts = true
 56           #删除本地旧镜像,下载新镜像
 57           sshCommand remote: remote, command: "docker rmi 仓库地址/${imagename}:latest || echo 'DONE'"
 58           sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:latest"
 59           sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:${tag}"
 60         }
 61       }
 62     }
 63 
 64     stage ('Restart service') {
 65       steps {
 66         script {
 67           def remote = [:]
 68           remote.name = '服务器命名'
 69           remote.host = '项目机器IP'
 70           remote.user = 'user'
 71           remote.password = 'passwd'
 72           remote.allowAnyHosts = true
 73           #停止旧的容器并删除,启动新容器
 74           sshCommand remote: remote, command: "docker stop ${imagename} || echo 'DONE'"
 75           sshCommand remote: remote, command: "docker rm ${imagename} || echo 'DONE'"
 76           sshCommand remote: remote, command: "docker run -td --name ${imagename} -p 7003:80 仓库地址/${imagename}:latest"
 77         }
 78       }
 79     }
 80 
 81     stage ('Clean history docker images on romote server') {
 82       steps {
 83         script {
 84           def remote = [:]
 85           remote.name = '服务器命名'
 86           remote.host = '项目IP'
 87           remote.user = 'user'
 88           remote.password = 'passwd'
 89           remote.allowAnyHosts = true
 90           #删除旧版本
 91           sshCommand remote: remote, command: "docker images 仓库地址/${imagename} --filter \"before=仓库地址/${imagename}:latest\" -q | xargs docker rmi || echo 'DONE'"
 92         }
 93       }
 94     }
 95   }
 96 #配置bearychat报警
 97   post {
 98     unstable {
 99       bearychatSend color: 'red', message: "${env.JOB_NAME},构建不稳定"
100     }
101     failure {
102       bearychatSend color: 'red', message: "${env.JOB_NAME},构建失败"
103     }
104   }
105 
106 }
107 
108 __________________________dockerfile____________________________________
109 
110 FROM node:10.16.2 as build-stage
111 
112 ADD . /app
113 WORKDIR /app
114 ENV PATH /app/node_modules/.bin:$PATH
115 #RUN npm install --ignore-scripts --unsafe-perm
116 RUN npm install --registry=http://registry.npm.taobao.org --ignore-scripts --unsafe-perm
117 RUN npm run build
118 RUN gulp publish:production
119 
120 FROM nginx:stable-alpine as production-stage
121 COPY config/nginx/nginx.conf /etc/nginx/conf.d/app.conf
122 COPY --from=build-stage /app/dist /usr/share/nginx/html
123 
124 EXPOSE 80
125 CMD ["nginx", "-g", "daemon off;"]
126 
127  

 

posted @ 2020-12-23 11:24  风儿飘  阅读(259)  评论(0编辑  收藏  举报