Skip to content

Commit eee2d13

Browse files
authored
feat: allow-exit-codes
2 parents 940dab4 + be349ce commit eee2d13

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
pull_request:
77
jobs:
88
test-build-args:
9-
name: Test
9+
name: Test Build Args
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v3
@@ -46,3 +46,19 @@ jobs:
4646
fi
4747
4848
echo "all good"
49+
test-failure:
50+
name: Test Allow Exit Codes
51+
runs-on: ubuntu-latest
52+
strategy:
53+
matrix:
54+
allow-exit-codes: ["0,42,1", "0,5,6,1,42", "0,*", "42", "*"]
55+
steps:
56+
- uses: actions/checkout@v3
57+
- name: build the container and run it
58+
id: run
59+
uses: ./
60+
with:
61+
repository: ${{ github.repository }}
62+
ref: ${{ github.sha }}
63+
dockerfile: tests/Dockerfile.test-failure
64+
allow-exit-codes: ${{ matrix.allow-exit-codes }}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
## [1.1.8] - 2023-08-31
10+
### Added
11+
- Add `allow-exit-codes` parameter used when running the docker image
12+
913
## [1.1.7] - 2023-08-04
1014
### Added
1115
- Add `build-args` parameter used when building the docker image

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ To use the action, add the following step to your GitHub Actions workflow:
2828
* `opts`: The Docker run options. Optional.
2929
* `args`: The Docker run args. Optional.
3030
* `build-args`: The Docker build args. Optional.
31+
* `allow-exit-codes`: A comma separated list of exit code allowed when running the container. Use * to allow all exit codes
3132
* `working-directory`: The Docker run working directory. Optional; defaults to `${{ github.workspace }}`.
3233
* `github-server-url`: The GitHub server URL. Optional; defaults to `${{ github.server_url }}`.
3334
* `docker-registry-url`: The Docker registry URL. Optional; defaults to `https://ghcr.io`.

action.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ inputs:
4141
description: 'The docker registry URL'
4242
required: false
4343
default: 'https://ghcr.io'
44+
allow-exit-codes:
45+
description: 'A comma separated list of exit code allowed when running the container. Use * to allow all exit codes'
46+
# see https://github.com/orgs/community/discussions/15452 for context.
47+
required: false
48+
default: '0'
4449
outputs:
4550
outputs:
4651
description: 'The outputs of the docker run step in JSON format'
@@ -93,6 +98,34 @@ runs:
9398
env:
9499
DOCKER_REGISTRY_URL: ${{ inputs.docker-registry-url }}
95100
IMAGE_TAG: ${{ steps.pull.outputs.image-tag }}
96-
run: docker run --workdir "${PWD/"$GITHUB_WORKSPACE"/"/github/workspace"}" --rm --env-file <(env | grep -E '^(ACTIONS|GITHUB|INPUT|RUNNER)_') -e HOME -e CI -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "$GITHUB_WORKSPACE":"/github/workspace" ${{ inputs.opts }} "${IMAGE_TAG}" ${{ inputs.args }}
101+
ALLOW_EXIT_CODES: ${{ inputs.allow-exit-codes }}
102+
run: |
103+
set +e
104+
set +o pipefail
105+
106+
ALLOW_ALL=$(echo "$ALLOW_EXIT_CODES" | grep -q "\*"; echo $?)
107+
EXIT_CODES=(${ALLOW_EXIT_CODES//,/ })
108+
109+
function docker_run() {
110+
docker run --workdir "${PWD/"$GITHUB_WORKSPACE"/"/github/workspace"}" --rm --env-file <(env | grep -E '^(ACTIONS|GITHUB|INPUT|RUNNER)_') -e HOME -e CI -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "$GITHUB_WORKSPACE":"/github/workspace" ${{ inputs.opts }} "${IMAGE_TAG}" ${{ inputs.args }}
111+
return $?
112+
}
113+
114+
docker_run
115+
EXIT_CODE=$?
116+
117+
if [[ $ALLOW_ALL -eq 0 ]]; then
118+
echo "Allowing exit code $EXIT_CODE"
119+
exit 0
120+
fi
121+
122+
for ALLOWED_EXIT_CODE in "${EXIT_CODES[@]}"; do
123+
if [[ "$EXIT_CODE" -eq "$ALLOWED_EXIT_CODE" ]]; then
124+
echo "Allowing exit code $EXIT_CODE"
125+
exit 0
126+
fi
127+
done
128+
129+
exit $EXIT_CODE
97130
working-directory: ${{ inputs.working-directory }}
98131
shell: bash

tests/Dockerfile.test-failure

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# a simple container that takes 3 args and generate a shell script that prints them
2+
FROM alpine:3.12.0
3+
4+
RUN echo "#!/bin/sh" > /script.sh
5+
RUN echo "exit 42" >> /script.sh
6+
7+
RUN chmod +x /script.sh
8+
9+
ENTRYPOINT ["/script.sh"]

0 commit comments

Comments
 (0)