Skip to content

Add reuse-container mode#303

Open
rsennewald wants to merge 1 commit intobuildkite-plugins:masterfrom
rsennewald:reuse-containers
Open

Add reuse-container mode#303
rsennewald wants to merge 1 commit intobuildkite-plugins:masterfrom
rsennewald:reuse-containers

Conversation

@rsennewald
Copy link
Copy Markdown

Add a persistent container feature. When reuse-container: true
is set, the plugin keeps a container alive across job steps
instead of creating and destroying one each time.
Key behaviors:

  • Derives a stable container name from the image name and agent
    spawn index (or uses an explicit reuse-container-name override).
  • On each job, checks for an existing running container: if the
    image digest matches, attaches via docker exec; if the digest
    differs, prints a warning banner, removes the stale container,
    and creates a fresh one.
  • New containers are started detached (docker run -d ... sleep infinity) with --rm suppressed, then the job command runs via
    docker exec.
  • Environment variables are stripped from the creation docker run
    and injected per-exec only, preventing secrets from leaking
    between jobs that share the container.
  • The pre-exit hook skips container cleanup when reuse-container
    is enabled so the container persists for subsequent jobs.
    Files changed:
  • plugin.yml: new reuse-container and reuse-container-name opts
  • lib/shared.bash: get_reuse_container_name,
    get_container_image_id, get_image_id helpers
  • commands/run.sh: reuse-container branch with exec-arg extraction,
    digest validation, and detached container creation
  • hooks/pre-exit: skip cleanup when reuse-container is set
  • tests/reuse-container.bats: 11 bats tests covering creation,
    reuse, digest mismatch, stopped container, custom name, spawn
    index, env passthrough, env stripping, pre-exit, --rm absence,
    and exit code propagation

Add a persistent container feature. When `reuse-container: true`
is set, the plugin keeps a container alive across job steps
instead of creating and destroying one each time.
Key behaviors:
- Derives a stable container name from the image name and agent
  spawn index (or uses an explicit `reuse-container-name` override).
- On each job, checks for an existing running container: if the
  image digest matches, attaches via `docker exec`; if the digest
  differs, prints a warning banner, removes the stale container,
  and creates a fresh one.
- New containers are started detached (`docker run -d ... sleep
  infinity`) with `--rm` suppressed, then the job command runs via
  `docker exec`.
- Environment variables are stripped from the creation `docker run`
  and injected per-exec only, preventing secrets from leaking
  between jobs that share the container.
- The `pre-exit` hook skips container cleanup when reuse-container
  is enabled so the container persists for subsequent jobs.
Files changed:
- plugin.yml: new `reuse-container` and `reuse-container-name` opts
- lib/shared.bash: `get_reuse_container_name`,
  `get_container_image_id`, `get_image_id` helpers
- commands/run.sh: reuse-container branch with exec-arg extraction,
  digest validation, and detached container creation
- hooks/pre-exit: skip cleanup when reuse-container is set
- tests/reuse-container.bats: 11 bats tests covering creation,
  reuse, digest mismatch, stopped container, custom name, spawn
  index, env passthrough, env stripping, pre-exit, --rm absence,
  and exit code propagation
@rsennewald rsennewald requested a review from a team as a code owner March 23, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants