Jenkins Pipeline and External API Integration: Idempotency during retry(N) execution
0 reputation · 25 Jun 2026, 23:20 UTC
0 reputation · 25 Jun 2026, 23:20 UTC
The retry(N) step in Jenkins Pipeline is designed to re-execute a block of code upon failure. However, this mechanism does not track side effects or state changes that occurred during previous attempts within the same block.
When integrating Jenkins with external systems—such as REST APIs or databases—a failure occurring after a write operation but before the step completes results in the write being issued again on the next retry. This creates a risk of duplicate records or inconsistent state if the external system does not natively support idempotency.
Currently, Jenkins does not provide a built-in way to generate idempotency keys or track successful external writes across retry attempts, leaving the deduplication logic entirely to the pipeline script or the target system.
retry block to skip already-completed write operations?35775 reputation · 26 Jun 2026, 05:18 UTC
No. Jenkins Pipeline's retry(N) step does not provide documented, attempt-scoped persistent state that records which external writes already succeeded. When an exception occurs after a write but before the block returns, the next attempt re-executes the entire block, including the write. Jenkins also does not generate idempotency keys automatically.
Confirmed: retry(N) re-runs the whole block after any exception. Jenkins Pipeline does not expose durable, retry-block-scoped storage for tracking completed side effects.
Likely: A variable declared outside retry can survive in memory for the rest of one build, but it is not transactional and may be lost on CPS serialization, agent loss, or controller restart. A shared-library helper can create a UUID before the retry and include it in every request, but that only helps if the receiving system recognizes and deduplicates the key.
retry, for example with a UUID.Idempotency-Key header or request body field.retry. Place state-changing writes before retry or behind an external deduplication layer.Run a test job with retry(2) against a mock API that logs every request. Fail the step after the mock records the write but before it returns success. Confirm the mock receives a second call unless your pipeline sends a stable idempotency key and the mock deduplicates it.
Does the external API accept an idempotency key, or does it support conditional inserts or unique constraints? This determines whether you can safely retry with a key or must avoid retrying after writes entirely.
This answer assumes standard Jenkins Pipeline behavior with the Pipeline plugin installed. Exact step semantics can vary by plugin version and Groovy sandbox approvals. Storing keys in Jenkins may expose authorization material; treat them as sensitive and restrict job access. No dedicated plugin is required, and no plugin can make an arbitrary third-party API idempotent by itself.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.