fix(deploy): publish unified services to temporary gray environment

This commit is contained in:
2026-10-07 11:02:47 +08:00
parent 6346e7bd95
commit ec0c06ff88
9 changed files with 211 additions and 72 deletions
+64 -6
View File
@@ -69,7 +69,7 @@ case "$command" in
esac
`
async function runDeployment({ imageTag, failingTag, failingStartTag, restartingTag, allowRollback = true, environment = 'test', service = 'audit-service', missingHealth = false }) {
async function runDeployment({ imageTag, failingTag, failingStartTag, restartingTag, allowRollback = true, environment = 'test', service = 'audit-service', missingHealth = false, topicPrefix = `${environment}-`, remote = false }) {
const temporaryDirectory = await mkdtemp(join(tmpdir(), 'firstleg-rollback-'))
const mockBin = join(temporaryDirectory, 'bin')
const dockerPath = join(mockBin, 'docker')
@@ -82,13 +82,32 @@ async function runDeployment({ imageTag, failingTag, failingStartTag, restarting
await mkdir(mockBin)
await writeFile(dockerPath, mockDocker, 'utf8')
await chmod(dockerPath, 0o755)
await writeFile(envFile, ['NACOS_SERVER_ADDR=nacos:8848','NACOS_NAMESPACE=test-id','NACOS_USERNAME=fixture','NACOS_PASSWORD=fixture','REDIS_HOST=redis','REDIS_PORT=6379','REDIS_DATABASE=0','REDIS_TOKEN_KEY_PREFIX=baiyue:auth','INTERNAL_SERVICE_TOKEN=fixture','JWT_RSA_JWK_SET=fixture','JWT_CURRENT_KID=fixture','ROCKETMQ_NAME_SERVER=namesrv:9876','MQ_TOPIC_PREFIX=test-','AUDIT_DB_URL=jdbc:mysql://mysql/audit','AUDIT_DB_USERNAME=fixture','AUDIT_DB_PASSWORD=fixture',''].join('\n'), 'utf8')
if (remote) {
await writeFile(join(mockBin, 'ssh'), `#!/usr/bin/env sh
set -eu
for argument in "$@"; do command="$argument"; done
printf '%s\\n' "$command" >> "$MOCK_SSH_LOG"
exec sh -c "$command"
`)
await writeFile(join(mockBin, 'scp'), `#!/usr/bin/env sh
set -eu
while [ "$#" -gt 2 ]; do shift; done
cp "$1" "\${2#*:}"
`)
await chmod(join(mockBin, 'ssh'), 0o755)
await chmod(join(mockBin, 'scp'), 0o755)
await writeFile(join(temporaryDirectory, 'key'), 'fixture')
await writeFile(join(temporaryDirectory, 'known-hosts'), 'fixture')
}
await writeFile(envFile, ['NACOS_SERVER_ADDR=nacos:8848','NACOS_NAMESPACE=test-id','NACOS_USERNAME=fixture','NACOS_PASSWORD=fixture','REDIS_HOST=redis','REDIS_PORT=6379','REDIS_DATABASE=0','REDIS_TOKEN_KEY_PREFIX=baiyue:auth','INTERNAL_SERVICE_TOKEN=fixture','JWT_RSA_JWK_SET=fixture','JWT_CURRENT_KID=fixture','ROCKETMQ_NAME_SERVER=namesrv:9876',`MQ_TOPIC_PREFIX=${topicPrefix}`,'AUDIT_DB_URL=jdbc:mysql://mysql/audit','AUDIT_DB_USERNAME=fixture','AUDIT_DB_PASSWORD=fixture',''].join('\n'), 'utf8')
await writeFile(logFile, '', 'utf8')
await writeFile(currentImageFile, 'registry.local/group/firstleg/audit-service:oldgood\n', 'utf8')
await writeFile(healthStateFile, 'healthy\n', 'utf8')
await writeFile(runtimeStateFile, 'running\n', 'utf8')
const result = spawnSync(shell, [
'-c', 'PATH="$MOCK_BIN:$PATH"; export PATH; exec sh "$@"', 'fixture',
deployScript.replaceAll('\\', '/'),
environment,
'registry.local/group/firstleg',
@@ -101,7 +120,12 @@ async function runDeployment({ imageTag, failingTag, failingStartTag, restarting
env: {
...process.env,
PATH: `${mockBin}${delimiter}${process.env.PATH ?? ''}`,
TARGET_IS_LOCAL: 'true',
MOCK_BIN: mockBin.replaceAll('\\', '/').replace(/^([A-Za-z]):/, (_, drive) => `/${drive.toLowerCase()}`),
TARGET_IS_LOCAL: remote ? 'false' : 'true',
TARGET_SSH_TARGET: 'fixture@fixture',
TARGET_SSH_KEY: join(temporaryDirectory, 'key').replaceAll('\\', '/'),
TARGET_SSH_KNOWN_HOSTS: join(temporaryDirectory, 'known-hosts').replaceAll('\\', '/'),
MOCK_SSH_LOG: join(temporaryDirectory, 'ssh.log').replaceAll('\\', '/'),
ALLOW_SAME_CONTRACT_ROLLBACK: String(allowRollback),
REGISTRY_USER: 'ci-user',
REGISTRY_PASSWORD: 'ci-password',
@@ -120,8 +144,9 @@ async function runDeployment({ imageTag, failingTag, failingStartTag, restarting
const dockerLog = await readFile(logFile, 'utf8')
const currentImage = (await readFile(currentImageFile, 'utf8')).trim()
const sshLog = remote ? await readFile(join(temporaryDirectory, 'ssh.log'), 'utf8').catch(() => '') : ''
await rm(temporaryDirectory, { recursive: true, force: true })
return { ...result, dockerLog, currentImage }
return { ...result, dockerLog, currentImage, sshLog }
}
test('successful deployment keeps the new healthy image', async (context) => {
@@ -182,8 +207,8 @@ test('architecture switch failure does not roll back one old service', async ()
assert.doesNotMatch(result.dockerLog,/pull .*:oldgood/)
assert.match(result.stderr,/Automatic rollback disabled/)
})
test('non TEST environments and retired services fail before Docker', async () => {
for(const options of [{environment:'production'},{environment:'development'},{service:'event-bridge-service'},{service:'gateway-overseas'}]){
test('unsupported environments and retired services fail before Docker', async () => {
for(const options of [{environment:'development'},{service:'event-bridge-service'},{service:'gateway-overseas'}]){
const result=await runDeployment({imageTag:'newgood',...options})
assert.equal(result.status,2)
assert.equal(result.dockerLog,'')
@@ -200,3 +225,36 @@ test('SQL initializer rejects absent authorization before any Docker call',()=>{
assert.equal(result.status,2)
assert.match(result.stderr,/Explicit TEST SQL authorization required/)
})
test('gray deploy preserves health checks and uses an isolated Compose project', async () => {
const result = await runDeployment({ imageTag: 'graygood', environment: 'production', allowRollback: false })
assert.equal(result.status, 0, result.stderr)
assert.match(result.dockerLog, /--project-name firstleg-production/)
assert.doesNotMatch(result.dockerLog, /--project-name firstleg-test/)
})
test('gray deployment rejects TEST message scope before any Docker operation', async () => {
const result = await runDeployment({ imageTag: 'graygood', environment: 'production', topicPrefix: 'test-' })
assert.equal(result.status, 2)
assert.equal(result.dockerLog, '')
assert.match(result.stderr, /MQ_TOPIC_PREFIX must be production-/)
})
test('SSH gray delivery executes the same health gate and cleans its unique temporary bundle', async () => {
const result = await runDeployment({ imageTag: 'remotegray', environment: 'production', allowRollback: false, remote: true })
assert.equal(result.status, 0, result.stderr)
assert.equal(result.currentImage, 'registry.local/group/firstleg/audit-service:remotegray')
assert.match(result.dockerLog, /--project-name firstleg-production/)
assert.match(result.sshLog, /rm -rf -- '\/tmp\/firstleg-production-release\.[A-Za-z0-9]+'/)
assert.doesNotMatch(result.sshLog, /ci-password/)
})
test('SSH gray health failure remains failed and still removes the uploaded credential bundle', async () => {
const result = await runDeployment({ imageTag: 'remotebad', failingTag: 'remotebad', environment: 'production', allowRollback: false, remote: true })
assert.equal(result.status, 1, result.stderr)
assert.match(result.stderr, /Automatic rollback disabled/)
assert.match(result.sshLog, /rm -rf -- '\/tmp\/firstleg-production-release\.[A-Za-z0-9]+'/)
assert.doesNotMatch(result.sshLog, /ci-password/)
})