jenkins_pipeline前端_docker

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:latestechoDONE"          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 仓库地址/ imagenamep7003: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;”]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值