diff --git a/.github/actions/docker-clean/action.yaml b/.github/actions/docker-clean/action.yaml index 56bf2f88ab..107df9a7e5 100644 --- a/.github/actions/docker-clean/action.yaml +++ b/.github/actions/docker-clean/action.yaml @@ -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: @@ -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}" diff --git a/.github/actions/docker-run/action.yaml b/.github/actions/docker-run/action.yaml index 375a21c7ce..60f7b43325 100644 --- a/.github/actions/docker-run/action.yaml +++ b/.github/actions/docker-run/action.yaml @@ -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" + + 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 }} diff --git a/.github/actions/readme.md b/.github/actions/readme.md index d6f249963d..93d0d1766b 100644 --- a/.github/actions/readme.md +++ b/.github/actions/readme.md @@ -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 @@ -87,12 +88,16 @@ 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: @@ -100,7 +105,7 @@ Example: jobs: test: steps: - - name: Cleanup Docker image + - name: Cleanup Docker resources if: always() uses: ./.github/actions/docker-clean with: