码迷,mamicode.com
首页 > 其他好文 > 详细

jenkins_pipeline前端_docker

时间:2020-12-29 11:15:03      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:history   int   name   agent   push   use   ges   git   awk   

pipeline {
  agent any

  environment {
    imagename = ‘镜像名‘
    tag = "v${BUILD_NUMBER}" #版本号
  }

  stages {
    stage(‘Pull code‘) {
      steps {
        echo "${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=${imagename}:${tag} .‘
        sh ‘docker tag ${imagename}:${tag} 仓库地址/${imagename}:${tag}‘
        sh ‘docker tag ${imagename}:${tag} 仓库地址/${imagename}:latest‘
      }
    }

    stage(‘Push docker image‘) {
      steps {
        #上传到镜像仓库
        sh ‘docker push 仓库地址/${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 仓库地址/${imagename}:latest || echo ‘DONE‘"
          sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:latest"
          sshCommand remote: remote, command: "docker pull 仓库地址/${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 ${imagename} -p 7003: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 仓库地址/${imagename} --filter \"before=仓库地址/${imagename}:latest\" -q | xargs docker rmi || echo ‘DONE‘"
        }
      }
    }
  }
#配置bearychat报警
  post {
    unstable {
      bearychatSend color: ‘red‘, message: "${env.JOB_NAME},构建不稳定"
    }
    failure {
      bearychatSend color: ‘red‘, message: "${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;"]

 

jenkins_pipeline前端_docker

标签:history   int   name   agent   push   use   ges   git   awk   

原文地址:https://www.cnblogs.com/sqbk/p/14177589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!