-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
131 lines (122 loc) · 4.59 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: 'Docker Container Action'
description: 'A reimplementation of docker container action'
branding:
icon: 'award'
color: 'green'
inputs:
repository:
description: 'The GitHub repository name'
required: true
ref:
description: 'The GitHub repository ref'
required: true
image:
description: 'The docker image name (defaults to repository)'
required: false
tag:
description: 'The docker image tag (defaults to ref)'
required: false
dockerfile:
description: 'The dockerfile path'
required: false
default: 'Dockerfile'
opts:
description: 'The docker run opts'
required: false
args:
description: 'The docker run args'
required: false
build-args:
description: 'The docker build args. Newline-delimited list.'
required: false
working-directory:
description: 'The docker run working directory'
required: false
default: ${{ github.workspace }}
github-server-url:
description: 'The GitHub server URL'
required: false
default: ${{ github.server_url }}
docker-registry-url:
description: 'The docker registry URL'
required: false
default: 'https://ghcr.io'
allow-exit-codes:
description: 'A comma separated list of exit code allowed when running the container. Use * to allow all exit codes'
# see https://github.com/orgs/community/discussions/15452 for context.
required: false
default: '0'
outputs:
outputs:
description: 'The outputs of the docker run step in JSON format'
value: ${{ toJSON(steps.run.outputs) }}
runs:
using: 'composite'
steps:
- id: pull
env:
IMAGE: ${{ inputs.image || inputs.repository }}
TAG: ${{ inputs.tag || inputs.ref }}
DOCKER_REGISTRY_URL: ${{ inputs.docker-registry-url }}
run: |
IMAGE_TAG="${DOCKER_REGISTRY_URL#http*://}/$IMAGE:$TAG"
echo "image-tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "image=$(docker pull ${IMAGE_TAG} -q || true)" >> $GITHUB_OUTPUT
shell: bash
- id: transform-build-args
# this steps reproduces the build-args used in https://github.com/docker/build-push-action#customizing
env:
BUILD_ARGS: ${{ inputs.build-args }}
run: |
build_args=""
while IFS= read -r line; do
line_with_equals=${line/:/=}
# skip empty lines (which happens with `<<<`)
if [[ -z "$line" ]]; then
continue
fi
build_args="$build_args --build-arg ${line_with_equals}"
done <<< "${BUILD_ARGS}"
echo "build-args=$build_args" >> $GITHUB_OUTPUT
shell: bash
- if: steps.pull.outputs.image == ''
env:
REPOSITORY: ${{ inputs.repository }}
REF: ${{ inputs.ref }}
DOCKERFILE: ${{ inputs.dockerfile }}
GITHUB_SERVER_URL: ${{ inputs.github-server-url }}
DOCKER_REGISTRY_URL: ${{ inputs.docker-registry-url }}
DOCKER_BUILDKIT: '0' # Using remote git context with buildkit does not support referencing merge commits via SHA
BUILD_ARGS: ${{ steps.transform-build-args.outputs.build-args }}
IMAGE_TAG: ${{ steps.pull.outputs.image-tag }}
run: docker build -t ${IMAGE_TAG} -f $DOCKERFILE ${BUILD_ARGS} $GITHUB_SERVER_URL/$REPOSITORY.git#$REF
shell: bash
- id: run
env:
DOCKER_REGISTRY_URL: ${{ inputs.docker-registry-url }}
IMAGE_TAG: ${{ steps.pull.outputs.image-tag }}
ALLOW_EXIT_CODES: ${{ inputs.allow-exit-codes }}
run: |
set +e
set +o pipefail
ALLOW_ALL=$(echo "$ALLOW_EXIT_CODES" | grep -q "\*"; echo $?)
EXIT_CODES=(${ALLOW_EXIT_CODES//,/ })
function docker_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 }}
return $?
}
docker_run
EXIT_CODE=$?
if [[ $ALLOW_ALL -eq 0 ]]; then
echo "Allowing exit code $EXIT_CODE"
exit 0
fi
for ALLOWED_EXIT_CODE in "${EXIT_CODES[@]}"; do
if [[ "$EXIT_CODE" -eq "$ALLOWED_EXIT_CODE" ]]; then
echo "Allowing exit code $EXIT_CODE"
exit 0
fi
done
exit $EXIT_CODE
working-directory: ${{ inputs.working-directory }}
shell: bash