Skip to content

Commit d30ee43

Browse files
fix(release): preserve draft publish after tag cleanup errors (#2353)
Keep staging-tag removal best-effort after reconciliation is complete while preserving identity checks and recording cleanup outcomes. Closes #2336. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2aa2cc4 commit d30ee43

2 files changed

Lines changed: 113 additions & 3 deletions

File tree

‎.github/workflows/squad-npm-publish.yml‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,22 +427,77 @@ jobs:
427427
exit "$status"
428428
429429
- name: Remove SDK staging tag
430+
id: remove-sdk-staging-tag
430431
if: success() && steps.existing.outputs.stage_pair == 'true'
431432
env:
432433
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
433434
run: |
434435
set -euo pipefail
435436
node "$RELEASE_SCRIPTS_ROOT/assert-publish-identity.mjs" --source-sha "$SOURCE_SHA" --dispatch-sha "${{ github.sha }}" --workflow-sha "${{ github.workflow_sha }}" --tag "$TAG" --evidence-dir "$PACKS_DIR"
436-
npm dist-tag rm @bradygaster/squad-sdk "$STAGING_TAG"
437+
if npm dist-tag rm @bradygaster/squad-sdk "$STAGING_TAG"; then
438+
echo "outcome=success" >> "$GITHUB_OUTPUT"
439+
else
440+
status=$?
441+
echo "outcome=failure" >> "$GITHUB_OUTPUT"
442+
echo "exit_code=$status" >> "$GITHUB_OUTPUT"
443+
fi
437444
438445
- name: Remove CLI staging tag
446+
id: remove-cli-staging-tag
439447
if: success() && steps.existing.outputs.stage_pair == 'true'
440448
env:
441449
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
442450
run: |
443451
set -euo pipefail
444452
node "$RELEASE_SCRIPTS_ROOT/assert-publish-identity.mjs" --source-sha "$SOURCE_SHA" --dispatch-sha "${{ github.sha }}" --workflow-sha "${{ github.workflow_sha }}" --tag "$TAG" --evidence-dir "$PACKS_DIR"
445-
npm dist-tag rm @bradygaster/squad-cli "$STAGING_TAG"
453+
if npm dist-tag rm @bradygaster/squad-cli "$STAGING_TAG"; then
454+
echo "outcome=success" >> "$GITHUB_OUTPUT"
455+
else
456+
status=$?
457+
echo "outcome=failure" >> "$GITHUB_OUTPUT"
458+
echo "exit_code=$status" >> "$GITHUB_OUTPUT"
459+
fi
460+
461+
- name: Record staging tag cleanup results
462+
if: always() && steps.existing.outputs.stage_pair == 'true'
463+
env:
464+
SDK_CLEANUP_OUTCOME: ${{ steps.remove-sdk-staging-tag.outputs.outcome || 'not-recorded' }}
465+
SDK_CLEANUP_STEP_OUTCOME: ${{ steps.remove-sdk-staging-tag.outcome }}
466+
SDK_CLEANUP_EXIT_CODE: ${{ steps.remove-sdk-staging-tag.outputs.exit_code || '' }}
467+
CLI_CLEANUP_OUTCOME: ${{ steps.remove-cli-staging-tag.outputs.outcome || 'not-recorded' }}
468+
CLI_CLEANUP_STEP_OUTCOME: ${{ steps.remove-cli-staging-tag.outcome }}
469+
CLI_CLEANUP_EXIT_CODE: ${{ steps.remove-cli-staging-tag.outputs.exit_code || '' }}
470+
run: |
471+
set -euo pipefail
472+
node --input-type=module <<'NODE'
473+
import { writeFileSync } from 'node:fs';
474+
475+
const outcomes = {
476+
sdk: {
477+
outcome: process.env.SDK_CLEANUP_OUTCOME,
478+
stepOutcome: process.env.SDK_CLEANUP_STEP_OUTCOME,
479+
exitCode: process.env.SDK_CLEANUP_EXIT_CODE || null,
480+
},
481+
cli: {
482+
outcome: process.env.CLI_CLEANUP_OUTCOME,
483+
stepOutcome: process.env.CLI_CLEANUP_STEP_OUTCOME,
484+
exitCode: process.env.CLI_CLEANUP_EXIT_CODE || null,
485+
},
486+
};
487+
const evidence = {
488+
stagingTag: process.env.STAGING_TAG,
489+
outcomes,
490+
};
491+
writeFileSync(
492+
`${process.env.RECON_DIR}/staging-tag-cleanup.json`,
493+
`${JSON.stringify(evidence, null, 2)}\n`,
494+
);
495+
for (const [pkg, result] of Object.entries(outcomes)) {
496+
if (result.outcome === 'failure') {
497+
console.log(`::warning::Could not remove the ${pkg} staging tag ${process.env.STAGING_TAG}; see cleanup step logs.`);
498+
}
499+
}
500+
NODE
446501
447502
- name: Upload reconciliation evidence
448503
if: always()
@@ -458,7 +513,7 @@ jobs:
458513
publish-draft-release:
459514
name: Publish the existing draft after recovery reconciliation
460515
needs: [prepare, publish]
461-
if: inputs.verify_run_id != '' && inputs.publish_draft_release && needs.publish.outputs.outcome == 'COMPLETE'
516+
if: always() && needs.prepare.result == 'success' && inputs.verify_run_id != '' && inputs.publish_draft_release && needs.publish.outputs.outcome == 'COMPLETE'
462517
runs-on: ubuntu-latest
463518
timeout-minutes: 5
464519
permissions:

‎test/publish-policy.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,58 @@ describe('publish-policy: live workflow files', () => {
159159
});
160160
}
161161
});
162+
163+
describe('publish-policy: release recovery after staging cleanup failure', () => {
164+
const workflow = readFileSync(
165+
join(process.cwd(), '.github', 'workflows', 'squad-npm-publish.yml'),
166+
'utf-8',
167+
);
168+
const section = (startMarker: string, endMarker: string): string => {
169+
const start = workflow.indexOf(startMarker);
170+
if (start === -1) return '';
171+
const end = workflow.indexOf(endMarker, start + startMarker.length);
172+
return workflow.slice(start, end === -1 ? undefined : end);
173+
};
174+
const sdkCleanup = section(
175+
' - name: Remove SDK staging tag\n',
176+
'\n - name: Remove CLI staging tag',
177+
);
178+
const cliCleanup = section(
179+
' - name: Remove CLI staging tag\n',
180+
'\n - name: Record staging tag cleanup results',
181+
);
182+
const cleanupEvidence = section(
183+
' - name: Record staging tag cleanup results\n',
184+
'\n - name: Upload reconciliation evidence',
185+
);
186+
const draftJob = section(
187+
' publish-draft-release:\n',
188+
'\n promote-insider-tag:',
189+
);
190+
191+
it('records staging cleanup failures without failing the publish job', () => {
192+
expect(sdkCleanup).toMatch(/id: remove-sdk-staging-tag/);
193+
expect(sdkCleanup).toMatch(
194+
/if: success\(\) && steps\.existing\.outputs\.stage_pair == 'true'/,
195+
);
196+
expect(sdkCleanup).toMatch(
197+
/assert-publish-identity\.mjs[\s\S]*?if npm dist-tag rm @bradygaster\/squad-sdk[\s\S]*?outcome=failure[\s\S]*?exit_code=\$status/,
198+
);
199+
expect(cliCleanup).toMatch(/id: remove-cli-staging-tag/);
200+
expect(cliCleanup).toMatch(
201+
/if: success\(\) && steps\.existing\.outputs\.stage_pair == 'true'/,
202+
);
203+
expect(cliCleanup).toMatch(
204+
/assert-publish-identity\.mjs[\s\S]*?if npm dist-tag rm @bradygaster\/squad-cli[\s\S]*?outcome=failure[\s\S]*?exit_code=\$status/,
205+
);
206+
expect(cleanupEvidence).toMatch(
207+
/SDK_CLEANUP_OUTCOME: \$\{\{ steps\.remove-sdk-staging-tag\.outputs\.outcome[\s\S]*?CLI_CLEANUP_OUTCOME: \$\{\{ steps\.remove-cli-staging-tag\.outputs\.outcome[\s\S]*?staging-tag-cleanup\.json[\s\S]*?::warning::Could not remove/,
208+
);
209+
});
210+
211+
it('publishes the existing draft when preparation succeeded and reconciliation is complete', () => {
212+
expect(draftJob).toMatch(
213+
/if: always\(\) && needs\.prepare\.result == 'success' && inputs\.verify_run_id != '' && inputs\.publish_draft_release && needs\.publish\.outputs\.outcome == 'COMPLETE'/,
214+
);
215+
});
216+
});

0 commit comments

Comments
 (0)