88 lines
4.5 KiB
YAML
88 lines
4.5 KiB
YAML
name: 生产环境回退上一个版本
|
|
|
|
"on":
|
|
workflow_dispatch:
|
|
inputs:
|
|
auto_rollback:
|
|
description: 是否由自动失败流程触发
|
|
required: false
|
|
type: boolean
|
|
|
|
concurrency:
|
|
group: release-prod
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
rollback:
|
|
runs-on: jayucloud
|
|
container:
|
|
options: >-
|
|
-v /opt/software/panel/apps/gitea/act_runner/salon-release:/salon-release
|
|
env:
|
|
GITHUB_SERVER_URL: http://192.168.5.15:3000
|
|
RELEASE_ENVIRONMENT: prod
|
|
RELEASE_STORE_ROOT: /salon-release
|
|
NACOS_ENVIRONMENT: prod
|
|
AUTO_ROLLBACK: ${{ inputs.auto_rollback }}
|
|
WEB_TARGET_HOST: 10.66.0.3
|
|
WEB_TARGET_PORT: 22
|
|
WEB_TARGET_USER: root
|
|
WEB_REMOTE_DIR: /opt/jc-salon-frontend
|
|
SALON_PROD_SSH_PASSWORD: ${{ secrets.SALON_PROD_SSH_PASSWORD }}
|
|
NACOS_PROD_URLS: ${{ secrets.NACOS_PROD_URLS }}
|
|
NACOS_PROD_USERNAME: ${{ secrets.NACOS_PROD_USERNAME }}
|
|
NACOS_PROD_PASSWORD: ${{ secrets.NACOS_PROD_PASSWORD }}
|
|
NACOS_DR_URLS: ${{ secrets.NACOS_DR_URLS }}
|
|
NACOS_DR_USERNAME: ${{ secrets.NACOS_DR_USERNAME }}
|
|
NACOS_DR_PASSWORD: ${{ secrets.NACOS_DR_PASSWORD }}
|
|
steps:
|
|
- name: 检出回退脚本
|
|
shell: bash
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
run: |
|
|
set -euo pipefail
|
|
git init .
|
|
git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git"
|
|
git -c http.extraheader="Authorization: token $GITEA_TOKEN" fetch --no-tags --depth=1 origin "$GITHUB_SHA"
|
|
git checkout --detach "$GITHUB_SHA"
|
|
- name: 读取上一成功版本
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
current=/salon-release/records/prod/latest-success-deploy.json
|
|
test -f "$current"
|
|
node - <<'NODE' >> "$GITHUB_ENV"
|
|
const fs = require('fs');
|
|
const latest = JSON.parse(fs.readFileSync('/salon-release/records/prod/latest-success-deploy.json'));
|
|
const id = process.env.AUTO_ROLLBACK === 'true' ? latest.releaseId : latest.previousReleaseId;
|
|
if (!id) throw new Error('没有可回退的 PROD 成功版本');
|
|
const target = JSON.parse(fs.readFileSync(`/salon-release/records/prod/deploy/${id}.json`));
|
|
console.log(`PACKAGE_DIRECTORY=${target.packageDir}`);
|
|
console.log(`ROLLBACK_SNAPSHOT_ID=${target.activeSnapshotId}`);
|
|
console.log(`ROLLBACK_RELEASE_ID=${target.releaseId}`);
|
|
console.log(`NACOS_RELEASE_ID=${target.nacosReleaseId}`);
|
|
NODE
|
|
- name: 安装 SSH 密码工具
|
|
shell: bash
|
|
run: apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends sshpass
|
|
- name: 写入 PROD 目标清单
|
|
shell: bash
|
|
run: |
|
|
mkdir -p "$RUNNER_TEMP/salon-prod"
|
|
printf '[{"Name":"core01-01","Host":"192.168.10.24","Port":22,"User":"root","Environment":"prod"},{"Name":"core01-02","Host":"192.168.10.25","Port":22,"User":"root","Environment":"prod"},{"Name":"core01-03","Host":"192.168.20.16","Port":22,"User":"root","Environment":"prod"},{"Name":"gateway01-01","Host":"192.168.10.26","Port":22,"User":"root","Environment":"prod"},{"Name":"gateway01-02","Host":"192.168.10.27","Port":22,"User":"root","Environment":"prod"},{"Name":"gateway01-03","Host":"192.168.20.18","Port":22,"User":"root","Environment":"prod"},{"Name":"salon-web","Host":"%s","Port":%s,"User":"%s","Environment":"prod"}]\n' "$WEB_TARGET_HOST" "$WEB_TARGET_PORT" "$WEB_TARGET_USER" > "$RUNNER_TEMP/salon-prod/servers.json"
|
|
- name: 恢复 PROD Nacos
|
|
shell: bash
|
|
env:
|
|
NACOS_SNAPSHOT_ID: ${{ env.ROLLBACK_SNAPSHOT_ID }}
|
|
NACOS_RELEASE_ID: ${{ env.NACOS_RELEASE_ID }}
|
|
AUTO_ROLLBACK: 'true'
|
|
NACOS_ROLLBACK_INVALIDATE: 'true'
|
|
run: node .gitea/scripts/rollback-nacos.mjs
|
|
- name: 恢复 PROD 应用
|
|
shell: pwsh
|
|
run: ./docs/scripts/deploy-jayucloud-salon-production.ps1 -Deploy -SkipDatabaseInit -RequireSystemd -PackageDirectory "$env:PACKAGE_DIRECTORY" -ServerInfoJsonPath "$env:RUNNER_TEMP/salon-prod/servers.json" -SshPassword "$env:SALON_PROD_SSH_PASSWORD" -CoreRemoteAppDir /opt/jc-salon-core -GatewayRemoteAppDir /opt/jc-salon-gateway -CorePattern '^core01-' -GatewayPattern '^gateway01-' -CoreServiceName jc-salon-core -GatewayServiceName jc-salon-gateway -WebTargetName salon-web -WebRemoteDir "$env:WEB_REMOTE_DIR" -PublicBaseUrl https://salon.yunail.art
|
|
- name: 记录回退后的成功版本
|
|
shell: bash
|
|
run: node .gitea/scripts/record-rollback.mjs
|