node {
	// 固定常量
	def credentialsId = "5fa0e212-cda7-4b2a-8232-f26dbb9d4793"
    def git_url = "http://192.168.2.103:8089/jianglw/hrhx-ui-hrproject.git"

	// 传入参数（需在jenkins配置）
//     def module_name = "hrhx-ui-jk-nginx"
//     def service_ip = "192.168.2.105"
//     def service_port = "5082"
//     def nginx_port = "5084"

    // 自定义参数（需跟环境配置一致）
    def proxy = "base"
  def baseApi = "hrhx-boot"
  def publicPath = ""
  def outputDir = "base"

  stage("Git pull") {
    echo "====================================================="
    echo "             拉取分支：【${git_branche}】              "
    echo "====================================================="
    checkout([
      $class: "GitSCM",
      branches: [[name: "${git_branche}"]],
      extensions: [],
      userRemoteConfigs: [[credentialsId: "${credentialsId}", url: "${git_url}"]]
    ])
    echo "拉取【${git_branche}】分支成功！"
  }

  stage("Yarn build") {
    sh "node -v"
    sh "npm -v"
    sh "npm config get registry"
    sh "yarn -v"
    sh "yarn config get registry"
    sh "yarn install"
    sh "rm -rf ${outputDir}"
    sh "yarn build:dev"
    echo "构建基础环境成功！"
  }

	stage("Nginx conf") {
	  if(proxy != ""){
	    proxy = "\\/" + proxy
	  }
    sh "rm -rf nginx/nginx-dev.conf"
    sh "cp nginx/nginx.conf nginx/nginx-dev.conf"
		sh "sed -i 's/SERVER_IP/${service_ip}/g' nginx/nginx-dev.conf"
		sh "sed -i 's/SERVER_PORT/${service_port}/g' nginx/nginx-dev.conf"
		sh "sed -i 's/PROXY/${proxy}/g' nginx/nginx-dev.conf"
		sh "sed -i 's/BASE_API/${baseApi}/g' nginx/nginx-dev.conf"
		sh "sed -i 's/PUBLIC_PATH/${publicPath}/g' nginx/nginx-dev.conf"
		sh "sed -i 's/OUTPUTDIR/${outputDir}/g' nginx/nginx-dev.conf"
		echo "修改Nginx配置文件成功！"
	}

  stage("Docker build") {
    sh "docker -H tcp://${param_ip_port} rm -f ${module_name} | true"
    sh "docker -H tcp://${param_ip_port} rmi -f ${module_name}:latest | true"
    sh "docker -H tcp://${param_ip_port} build --build-arg PROFILE=dev --build-arg OUTPUTDIR=${outputDir} -t ${module_name}:latest ."
    echo "构建Nginx镜像成功！"
  }

  stage("Docker run") {
    sh "docker -H tcp://${param_ip_port} run -d -p ${nginx_port}:80 --restart=always --name ${module_name} ${module_name}:latest"
    echo "容器【${module_name}】正在启动。。。"
  }
}

