Skip to content

Commit be9903c

Browse files
committed
ci: Remove docker containers at end, systematically
1 parent 85d54c7 commit be9903c

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Docker cleanup action
2-
description: Composite action for removing Docker images
2+
description: Composite action for removing Docker containers and images
33
author: "Devito"
44

55
inputs:
@@ -10,12 +10,28 @@ inputs:
1010
tag:
1111
description: "Tag of the built image to use"
1212
required: true
13+
name:
14+
description: "Name substring used by the docker-run action"
15+
default: ""
1316

1417
runs:
1518
using: "composite"
1619
steps:
1720
- id: dockerclean
18-
name: "Cleanup docker image"
21+
name: "Cleanup Docker resources"
1922
shell: bash
23+
env:
24+
NAME: ${{ inputs.name }}
25+
TAG: ${{ inputs.tag }}
26+
UNIQUE: ${{ inputs.uid }}
2027
run: |
21-
docker image rm -f "${{ inputs.tag }}_${{ inputs.uid }}"
28+
CONTAINER_NAME="ci-${NAME:-${TAG}}-${UNIQUE}"
29+
CID_FILE="${RUNNER_TEMP}/${CONTAINER_NAME}.cid"
30+
31+
if [[ -s "${CID_FILE}" ]]; then
32+
CONTAINER_ID=$(< "${CID_FILE}")
33+
docker container rm -f "${CONTAINER_ID}" 2>/dev/null || true
34+
fi
35+
rm -f "${CID_FILE}"
36+
37+
docker image rm -f "${TAG}_${UNIQUE}"

‎.github/actions/docker-run/action.yaml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ runs:
5050
shell: bash
5151
env:
5252
NAME: ${{ inputs.name }}
53+
TAG: ${{ inputs.tag }}
54+
UNIQUE: ${{ inputs.uid }}
5355
run: |
56+
CONTAINER_NAME="ci-${NAME:-${TAG}}-${UNIQUE}"
57+
CID_FILE="${RUNNER_TEMP}/${CONTAINER_NAME}.cid"
58+
59+
rm -f "${CID_FILE}"
60+
5461
docker run \
5562
--init -t --rm \
5663
${{ inputs.args }} \
57-
--name "ci-${NAME:-${{ inputs.tag }}}-${{ inputs.uid }}" \
64+
--name "${CONTAINER_NAME}" \
65+
--cidfile "${CID_FILE}" \
5866
--env-file=docker/coverage.env \
5967
${{ steps.processenv.outputs.docker_environment_args }} \
60-
"${{ inputs.tag }}_${{ inputs.uid }}" \
68+
"${TAG}_${UNIQUE}" \
6169
${{ inputs.command }}

‎.github/actions/readme.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Inputs:
5656
- The tag must match built image, easily obtained from build action
5757
- If you provide a custom name `foo` the container name will be `ci-foo-UUUUUUUUUU` where UUUUUUUUUU is the UID
5858
- The default args `--init -t --rm` are _always_ added
59+
- The latest container ID is stored under `RUNNER_TEMP` for the docker-clean action
5960
- Environment variables must be passed a single environment variable per line, best achieved with the (`|`) syntax in yaml
6061
- Only a single command is executed, not a list of commands. Using `;` or `&&` will result in subsequent commands being executed outside of the docker environment
6162

@@ -87,20 +88,24 @@ Inputs:
8788

8889
- `uid`: Unique identifier output from docker-build action
8990
- `tag`: Tag of the built image to use
91+
- `name`: Name substring passed to docker-run (optional)
9092

9193
### Notes
9294

9395
- UID must be unique, easily obtained from build action
9496
- Tag must match built image, easily obtained from build action
95-
- Use `if: always()` to always clean up the image, even if the workflow fails
97+
- If set, name must match the optional name passed to docker-run
98+
- The container is force-removed using the ID recorded by docker-run
99+
- Use `if: always()` to clean up the container and image even if the workflow
100+
fails
96101

97102
Example:
98103

99104
```yaml
100105
jobs:
101106
test:
102107
steps:
103-
- name: Cleanup Docker image
108+
- name: Cleanup Docker resources
104109
if: always()
105110
uses: ./.github/actions/docker-clean
106111
with:

0 commit comments

Comments
 (0)