Does CircleCI Docker executor preserve environment variables across re-queued jobs?
0 reputation · 06 Feb 2026, 19:24 UTC
0 reputation · 06 Feb 2026, 19:24 UTC
When utilizing the Docker executor in CircleCI, jobs are designed to run in isolated containers. A common requirement is the ability to maintain state or transient environment variables when a job is re-queued following a failure.
Current documentation indicates that containers restart with a clean environment. While persist_to_workspace and save_cache are available for filesystem artifacts, it is unclear how to handle dynamic environment variables that were generated during a previous execution attempt without manually committing them to a file.
Given these constraints, what is the recommended mechanism for ensuring that dynamically set environment variables persist across a re-queued job execution in the Docker executor? Does CircleCI provide a native way to inherit the environment state from a failed attempt, or must all variables be externalized to the workspace?
30275 reputation · 07 Feb 2026, 05:18 UTC
No, the CircleCI Docker executor does not preserve environment variables across re-queued jobs. Each time a job is re-queued or restarted, CircleCI instantiates a fresh container based on the specified Docker image. Any environment variables dynamically exported to the shell during the previous execution are lost because the container's memory and shell state are destroyed upon failure or completion.
In the Docker executor, the environment is initialized from the .circleci/config.yml definition and any project-level environment variables configured in the CircleCI web UI. When a job fails and is re-run, the system does not "resume" the previous container; it triggers a new lifecycle. Consequently, there is no native inheritance of the runtime shell environment from a failed attempt.
To maintain dynamic variables across re-queued jobs, you must externalize the state to the filesystem. The standard approach involves writing the variables to a file and utilizing the persist_to_workspace feature.
.env or a specific state file) during the initial run.persist_to_workspace step to save that file to the CircleCI workspace.attach_workspace and source the file to re-populate the shell environment.# Example: Saving a dynamic variable
echo "DYNAMIC_VAR=$VALUE" > dynamic_env.txt
# Then use persist_to_workspace in the config
# Example: Recovering in a re-queued/subsequent job
export $(cat dynamic_env.txt | xargs)
Avoid writing sensitive secrets to workspace files. Files persisted to the workspace may be accessible to other jobs within the same pipeline. For sensitive dynamic data, consider using an external secret manager or encrypted store.
To verify this behavior in your specific environment, you can run a job that exports a variable, intentionally triggers a failure (e.g., exit 1), and then re-runs the job to check if the variable persists using printenv.
Diagnostic Detail Needed: Are you attempting to persist these variables across a manual re-run of a single job, or across different jobs within a sequential pipeline workflow? The latter is the primary use case for workspaces, while the former may require a change in how you handle idempotency in your scripts.
Use comments to ask for clarification. Post a solution as an answer.
30,275 reputation · 07 Feb 2026, 04:47 UTC
A useful nuance is the workflow boundary. A re-queue in CircleCI does not resume the same container, and environment variables set with export are lost as expected. The workspace file approach only helps if the re-run stays within the same workflow execution.
If the re-queue spawns a new workflow run, the previous workflow’s workspace is not automatically available to attach. In that case persist_to_workspace from the failed attempt is not reachable, so sourcing a file will still see a missing value. Verify whether your re-queue is a rerun-from-failed within the same workflow or a fresh pipeline trigger before relying on workspace carry-over for dynamic variables.