Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .github/actions/docker-clean/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Docker cleanup action
description: Composite action for removing Docker images
description: Composite action for removing Docker containers and images
author: "Devito"

inputs:
Expand All @@ -10,12 +10,28 @@ inputs:
tag:
description: "Tag of the built image to use"
required: true
name:
description: "Name substring used by the docker-run action"
default: ""

runs:
using: "composite"
steps:
- id: dockerclean
name: "Cleanup docker image"
name: "Cleanup Docker resources"
shell: bash
env:
NAME: ${{ inputs.name }}
TAG: ${{ inputs.tag }}
UNIQUE: ${{ inputs.uid }}
run: |
docker image rm -f "${{ inputs.tag }}_${{ inputs.uid }}"
CONTAINER_NAME="ci-${NAME:-${TAG}}-${UNIQUE}"
CID_FILE="${RUNNER_TEMP}/${CONTAINER_NAME}.cid"

if [[ -s "${CID_FILE}" ]]; then
CONTAINER_ID=$(< "${CID_FILE}")
docker container rm -f "${CONTAINER_ID}" 2>/dev/null || true
fi
rm -f "${CID_FILE}"

docker image rm -f "${TAG}_${UNIQUE}"
12 changes: 10 additions & 2 deletions .github/actions/docker-run/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ runs:
shell: bash
env:
NAME: ${{ inputs.name }}
TAG: ${{ inputs.tag }}
UNIQUE: ${{ inputs.uid }}
run: |
CONTAINER_NAME="ci-${NAME:-${TAG}}-${UNIQUE}"
CID_FILE="${RUNNER_TEMP}/${CONTAINER_NAME}.cid"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this should be an output and handled by actions, not written to some arbitrary file. Look at how I built all the custom action, or tell codex to...


rm -f "${CID_FILE}"

docker run \
--init -t --rm \
${{ inputs.args }} \
--name "ci-${NAME:-${{ inputs.tag }}}-${{ inputs.uid }}" \
--name "${CONTAINER_NAME}" \
--cidfile "${CID_FILE}" \
--env-file=docker/coverage.env \
${{ steps.processenv.outputs.docker_environment_args }} \
"${{ inputs.tag }}_${{ inputs.uid }}" \
"${TAG}_${UNIQUE}" \
${{ inputs.command }}
9 changes: 7 additions & 2 deletions .github/actions/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Inputs:
- The tag must match built image, easily obtained from build action
- If you provide a custom name `foo` the container name will be `ci-foo-UUUUUUUUUU` where UUUUUUUUUU is the UID
- The default args `--init -t --rm` are _always_ added
- The latest container ID is stored under `RUNNER_TEMP` for the docker-clean action
- Environment variables must be passed a single environment variable per line, best achieved with the (`|`) syntax in yaml
- 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

Expand Down Expand Up @@ -87,20 +88,24 @@ Inputs:

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

### Notes

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

Example:

```yaml
jobs:
test:
steps:
- name: Cleanup Docker image
- name: Cleanup Docker resources
if: always()
uses: ./.github/actions/docker-clean
with:
Expand Down
Loading