#!/usr/bin/groovy
@Library('pipeline-library')
def utils = new io.fabric8.Utils()
clientsNode{

  def env = 'shanghaiwuye'

  def projectName = "${JOB_BASE_NAME}"
  def newVersion = ''
  def deployType = ""
  try {
    deployType = DEPLOY_TYPE
  } catch (Throwable e) {
    deployType = "TEST"
  }

  checkout scm


  stage '构建版本'
  echo 'NOTE: running pipelines for the first time will take longer as build and base docker images are pulled onto the node'
  if (!fileExists ('Dockerfile')) {
    writeFile file: 'Dockerfile', text: '''FROM 168.168.112.64:5000/node-nginx:8.12.0

WORKDIR /app

# 将当前目录下的所有文件拷贝到工作目录下
COPY . /app/
# 1.安装依赖
# 2.运行 npm run build
# 3.将 dist 目录的所有文件拷贝到 nginx 的目录下
# 4.删除工作目录的文件，尤其是 node_modules 以减小镜像体积
# 由于镜像构建的每一步都会产生新层
# 为了减小镜像体积，尽可能将一些同类操作，集成到一个步骤中，如下、
RUN  cp -r dist/* /var/www/html \\
     '''
  }

  newVersion = performCanaryRelease {}

  def rc = getKubernetesJson {
    port = 80
    label = 'nodejs'
    version = newVersion
    name = projectName
    imageName = clusterImageName
  }

  stage '部署'
  kubernetesApply(file: rc, environment: env)


}
