#!/usr/bin/env groovy def projectProperties = [ [$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5']], parameters([ string(name: 'branch', description: '分支'), booleanParam(name: 'yarnInstall', description: '是否更新node_modules'), string(name: 'backUrl', defaultValue: 'https://api1.sino-assist.com', description: 'backend server url') ]) ] def deploy_server = "192.168.3.132" def profile = "review" def NAMESPACE = "review" def REGISTRY_URL = "harbor.sino-assist.com" node { def workspace = pwd() def DOCKER_CREDENTIAL_ID = 'harbor' stage('checkout') { git branch: branch, credentialsId: 'gitlab', url: 'https://git.sino-assist.com/server/sa-cc.git' } if(params.build==true){ stage('build') { nodejs(nodeJSInstallationName: 'nodejs-v22') { if(params.yarnInstall == true){ sh "yarn" } sh "export NODE_OPTIONS=--openssl-legacy-provider" String backUrl = params.backUrl if(profile == 'prod'){ sh "sed -i 's|VUE_APP_BACK_REST_URL_PLACE_HOLDER|${backUrl}|' ${workspace}/.env.prod" sh "yarn build-prod" }else{ sh "sed -i 's|VUE_APP_BACK_REST_URL_PLACE_HOLDER|${backUrl}|' ${workspace}/.env.alpha" sh "yarn build" } } } stage('docker-login') { withCredentials([usernamePassword(passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME', credentialsId: "${DOCKER_CREDENTIAL_ID}",)]) { sh "echo '$DOCKER_PASSWORD' | docker login ${REGISTRY_URL} -u '$DOCKER_USERNAME' --password-stdin" } } stage('docker-build') { sh " docker build -f k8s/Dockerfile -t ${REGISTRY_URL}/new-sino/sa-cc:${NAMESPACE} ." } stage('docker-push') { sh "docker push ${REGISTRY_URL}/new-sino/sa-cc:${NAMESPACE}" } } stage('deploy') { String yml = """ version: \\"3.8\\" services: svc: image: ${REGISTRY_URL}/new-sino/sa-cc:${NAMESPACE} ports: - '8081:8080' environment: - TZ=Asia/Shanghai deploy: mode: replicated replicas: 1 restart_policy: condition: on-failure delay: 5s max_attempts: 3 update_config: order: start-first resources: limits: cpus: \\"1\\" memory: "300M" reservations: cpus: \\"4\\" memory: "4G" placement: constraints: - "node.labels.${NAMESPACE}_sa-cc==1" networks: default: name: ${NAMESPACE} external: true """ String serverName = "${NAMESPACE}_ss_sa-cc" String ymlFile = "/data/swarm/${serverName}.yml" String deploy = "sshpass -p 'Sino.2025' ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@${deploy_server} \" mkdir -p /data/swarm/ && echo '''${yml}''' > ${ymlFile} && docker stack deploy -c ${ymlFile} ${serverName} --prune --with-registry-auth\"" echo deploy sh deploy } }