feat: complete production blue-green deployment

This commit is contained in:
2026-09-20 21:44:27 +08:00
parent e3415a8778
commit b50d59a058
6 changed files with 195 additions and 34 deletions
+30 -6
View File
@@ -21,12 +21,22 @@ case "$command" in
exit 0
;;
compose)
container_name=''
previous=''
for argument in "$@"; do
if [ "$previous" = --name ]; then container_name="$argument"; fi
previous="$argument"
if [ "$argument" = run ]; then
: > "$MOCK_CANDIDATE_EXISTS"
exit 0
action=run
fi
done
if [ "\${action:-}" = run ]; then
case "$container_name" in
*-worker) : > "$MOCK_WORKER_EXISTS" ;;
*) : > "$MOCK_CANDIDATE_EXISTS" ;;
esac
exit 0
fi
exit 0
;;
inspect)
@@ -43,6 +53,13 @@ case "$command" in
*Config.Image*) printf '%s\\n' 'registry.local/group/file:newtag' ;;
esac
;;
firstleg-production-file-green-worker)
[ -f "$MOCK_WORKER_EXISTS" ] || exit 1
case "$*" in
*State.Health*) printf '%s\\n' healthy ;;
*Config.Image*) printf '%s\\n' 'registry.local/group/file:newtag' ;;
esac
;;
*) exit 1 ;;
esac
;;
@@ -57,6 +74,7 @@ case "$command" in
case "$target" in
firstleg-production-file) : > "$MOCK_LEGACY_REMOVED" ;;
firstleg-production-file-green) rm -f "$MOCK_CANDIDATE_EXISTS" ;;
firstleg-production-file-green-worker) rm -f "$MOCK_WORKER_EXISTS" ;;
esac
;;
logs) echo 'candidate logs' ;;
@@ -79,13 +97,13 @@ async function runBlueGreen(health) {
await writeFile(sleep, '#!/usr/bin/env sh\nexit 0\n', 'utf8')
await chmod(docker, 0o755)
await chmod(sleep, 0o755)
await writeFile(envFile, 'PLACEHOLDER=true\n', 'utf8')
await writeFile(envFile, 'FILE_EXPORT_CONSUMER_ENABLED=true\n', 'utf8')
await writeFile(compose, 'services: {}\n', 'utf8')
await writeFile(log, '', 'utf8')
const paths = {
candidate: join(temp, 'candidate'), stopped: join(temp, 'legacy-stopped'),
removed: join(temp, 'legacy-removed')
removed: join(temp, 'legacy-removed'), worker: join(temp, 'worker')
}
const result = spawnSync(shell, [
script.replaceAll('\\', '/'), 'production', 'registry.local/group', 'newtag', 'file',
@@ -100,6 +118,7 @@ async function runBlueGreen(health) {
DEPLOY_HEALTH_POLL_SECONDS: '1',
MOCK_LOG: log,
MOCK_CANDIDATE_EXISTS: paths.candidate,
MOCK_WORKER_EXISTS: paths.worker,
MOCK_LEGACY_STOPPED: paths.stopped,
MOCK_LEGACY_REMOVED: paths.removed,
MOCK_CANDIDATE_HEALTH: health
@@ -111,7 +130,8 @@ async function runBlueGreen(health) {
const state = existsSync(stateFile) ? await readFile(stateFile, 'utf8') : ''
const evidence = {
...result, output, dockerLog, state,
stopped: existsSync(paths.stopped), removed: existsSync(paths.removed)
stopped: existsSync(paths.stopped), removed: existsSync(paths.removed),
worker: existsSync(paths.worker)
}
await rm(temp, { recursive: true, force: true })
return evidence
@@ -123,10 +143,13 @@ test('healthy candidate becomes active only after health and old instance drains
assert.equal(result.status, 0, result.output)
assert.equal(result.stopped, true)
assert.equal(result.removed, true)
assert.match(result.state, /^green\|firstleg-production-file-green\|19206\|registry\.local\/group\/file:newtag/)
assert.equal(result.worker, true)
assert.match(result.state, /^green\|firstleg-production-file-green\|19206\|registry\.local\/group\/file:newtag\|firstleg-production-file-green-worker/)
const run = result.dockerLog.indexOf('compose --project-name firstleg-production')
const stop = result.dockerLog.indexOf('stop --time 45 firstleg-production-file')
assert.ok(run >= 0 && run < stop)
assert.match(result.dockerLog,
/network connect --alias file --alias file-service firstleg-production-routing firstleg-production-file-green/)
})
test('unhealthy candidate is removed while old instance and state remain untouched', async (context) => {
@@ -135,6 +158,7 @@ test('unhealthy candidate is removed while old instance and state remain untouch
assert.equal(result.status, 1, result.output)
assert.equal(result.stopped, false)
assert.equal(result.removed, false)
assert.equal(result.worker, false)
assert.equal(result.state, '')
assert.doesNotMatch(result.dockerLog, /stop --time .*firstleg-production-file/)
})