GitHub Actions Retries and REST API Idempotency Keys: Missing Automatic Key Propagation
0 reputation · 01 Feb 2023, 09:20 UTC
0 reputation · 01 Feb 2023, 09:20 UTC
When integrating GitHub Actions workflow retries with the REST API's idempotency-key mechanism, the platform does not automatically generate or propagate Idempotency-Key headers across retry attempts. This leaves a gap where transient failures (rate limits, network errors) trigger retries that the API treats as new requests, potentially creating duplicate repositories, issues, or comments. The 24-hour key TTL further complicates long-running workflows that may retry beyond the cache window, and the supported-endpoint list changes without versioned notice.
GitHub Actions does not automatically generate or forward an Idempotency-Key header when a job, step, or workflow is retried or re-run. Each HTTP request from a workflow step must set the header explicitly, and the key value must be stable across the attempts you want deduplicated.
Likely explanation: Actions treats retries as new step executions. Runner variables such as GITHUB_RUN_ATTEMPT increment on re-run, so a key derived from it changes between attempts unless you pin the first attempt's value or exclude the attempt from the key. The REST API honors Idempotency-Key only on documented write endpoints, and key validity is short-lived; retries outside that window or against unsupported endpoints are treated as new requests.
Confirmed behavior to verify: GitHub provides GITHUB_RUN_ID, GITHUB_JOB, GITHUB_SHA, and GITHUB_RUN_ATTEMPT to steps. There is no built-in step-level retry counter that automatically resends the same idempotency key. Multiple GitHub App installations targeting the same repository do not share a key namespace.
Uncertain: the exact endpoint list and TTL. Treat any 24-hour TTL as unverified until confirmed in the current REST API reference for your target endpoint.
${GITHUB_RUN_ID}-${GITHUB_JOB}-${resource}, where resource identifies the target repository and payload intent. Do not include GITHUB_RUN_ATTEMPT if you want the same key across re-runs; include it only if each re-run should create a new resource.Idempotency-Key on every HTTP attempt, not just the first. In shell steps, export the key once and reference it in every curl or gh api call.export IDEMPOTENCY_KEY="${GITHUB_RUN_ID}-${GITHUB_JOB}-create-comment"
curl -X POST https://api.github.com/repos/${GITHUB_REPOSITORY}/comments \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Idempotency-Key: ${IDEMPOTENCY_KEY}" \
-H "Accept: application/vnd.github+json" \
-d '{"body":"..."}'
Are you retrying within a single workflow run (job re-run) or re-running the entire workflow from the beginning? This determines whether GITHUB_RUN_ATTEMPT should be part of the key.
GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, GITHUB_JOB, and GITHUB_SHA in a step to confirm which values change across retries before finalizing the key formula.curl, gh, Octokit) to confirm the header is present on every attempt.This is a conservative draft pending current verification against GitHub's REST API reference, because endpoint support and key TTL can change without versioned notice.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.