fix: 统一前端Harbor镜像发布与拉取部署
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import crypto from 'node:crypto'
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { options, packageRelease, inspectPackage, deploymentTarget, shellQuote, run, fail } from './release.mjs'
|
||||
import { options, packageRelease, inspectPackage, deploymentTarget, shellQuote, frontendRoot, frontendImage, validTag, run, fail } from './release.mjs'
|
||||
|
||||
export const installScript = `set -euo pipefail
|
||||
BASE_DIR=$(cd -- "$1" && pwd -P)
|
||||
@@ -34,17 +35,26 @@ SW_DEPLOY_BASE="$BASE_DIR" bash "$RELEASE/deploy.sh" "$TAG" "$MODE"
|
||||
function main() {
|
||||
const args = options({
|
||||
archive: { type: 'string' }, tag: { type: 'string' }, 'source-ref': { type: 'string' }, 'skip-install': { type: 'boolean' },
|
||||
'image-tag': { type: 'string' },
|
||||
host: { type: 'string' }, user: { type: 'string' }, port: { type: 'string' }, identity: { type: 'string' }, directory: { type: 'string' }, 'build-only': { type: 'boolean' }
|
||||
})
|
||||
if (args.help) {
|
||||
console.log('pnpm deploy:test [--archive FILE.tar.gz] [--tag VERSION] [--build-only] [--host 192.168.2.226] [--user root] [--port 22] [--identity SSH_KEY] [--directory /www/wwwroot/sw2.fuhua91.com]\n不指定 --archive 时先独立构建前端;--build-only 仅在服务器构建并预检,不替换当前容器。')
|
||||
console.log('pnpm deploy:test [--archive FILE.tar.gz | --image-tag VERSION] [--tag VERSION] [--build-only] [--host 192.168.2.226] [--user root] [--port 22] [--identity SSH_KEY] [--directory /www/wwwroot/sw2.fuhua91.com]\n默认打包并在服务器构建、推送 Harbor 后部署;--image-tag 直接拉取已有 Harbor 镜像;--build-only 仅预检,不替换当前容器。')
|
||||
return
|
||||
}
|
||||
if (args.archive && (args.tag || args['source-ref'] || args['skip-install'])) throw new Error('--archive 复用现有包,不能再指定构建参数')
|
||||
if (args['image-tag'] && (args.archive || args.tag || args['source-ref'] || args['skip-install'])) throw new Error('--image-tag 直接使用 Harbor 镜像,不能同时指定发布包或构建参数')
|
||||
const target = deploymentTarget(args)
|
||||
const release = args.archive ? inspectPackage(args.archive) : inspectPackage(packageRelease(args).archive)
|
||||
const identity = args.identity ? ['-i', path.resolve(args.identity)] : []
|
||||
const ssh = ['-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10', '-p', target.port, ...identity, target.remote]
|
||||
if (args['image-tag']) {
|
||||
const tag = validTag(args['image-tag'])
|
||||
const command = `SW_DEPLOY_BASE=${shellQuote(target.base)} bash -s -- ${shellQuote(tag)} --pull-only${args['build-only'] ? ' --build-only' : ''}`
|
||||
run('ssh', [...ssh, command], { stdio: ['pipe', 'inherit', 'inherit'], input: fs.readFileSync(path.join(frontendRoot, 'deploy/deploy.sh'), 'utf8').replaceAll('\r\n', '\n') })
|
||||
console.log(`${args['build-only'] ? 'Harbor 镜像预检通过' : 'Harbor 镜像部署完成'}:${frontendImage(tag)}`)
|
||||
return
|
||||
}
|
||||
const release = args.archive ? inspectPackage(args.archive) : inspectPackage(packageRelease(args).archive)
|
||||
const uploads = `${target.base}/frontend-releases`
|
||||
const uploaded = `${uploads}/.upload-${release.info.version}-${crypto.randomBytes(6).toString('hex')}.tar.gz`
|
||||
run('ssh', [...ssh, `test -d ${shellQuote(target.base)} && test -w ${shellQuote(target.base)} && mkdir -p ${shellQuote(uploads)}`])
|
||||
|
||||
Reference in New Issue
Block a user