Skip to content

Commit 81792d0

Browse files
committed
Add skip if no Dockerfiles are in directories
1 parent 5ca5a12 commit 81792d0

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

.github/workflows/ci.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
results: ${{ steps.hadolint5.outputs.results }}
104104
run: echo "$results"
105105

106-
- name: Run integration test 7 - set recursive
106+
- name: Run integration test 7 - set recursive with existing files
107107
# This step will never fail, but will print out rule violations
108108
# for all the Dockerfiles in repository.
109109
uses: ./
@@ -120,14 +120,25 @@ jobs:
120120
# format: sarif
121121
# output-file: report.sarif
122122

123-
- name: Run integration test 9 - run with no Dockerfiles
123+
124+
- name: Run integration test 9 - set recursive with non-matching files
125+
# This step will never fail, but will print out rule violations
126+
# for all the Dockerfiles in repository.
127+
uses: ./
128+
with:
129+
dockerfile: "*Dockerfile_non_existent"
130+
failure-threshold: error
131+
recursive: true
132+
133+
- name: Run integration test 10 - run with no Dockerfiles
124134
# This should not fail if no Dockerfiles are found in the path
125135
# especially if git change deletes Dockerfile
126-
uses: testdata/test_empty_dir
136+
uses: ./
127137
with:
128-
dockerfile: ""
138+
dockerfile: "*Dockerfile"
129139
failure-threshold: error
130140
recursive: true
141+
working-directory: testdata/test_empty_dir/
131142

132143
release:
133144
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ inputs:
7373
required: false
7474
description: 'A comma separated list of trusted registry urls'
7575
default:
76+
working-directory:
77+
required: false
78+
description: 'Path where you want to start scanning for Dockerfiles'
79+
default:
7680

7781
runs:
7882
using: 'docker'
@@ -91,6 +95,7 @@ runs:
9195
HADOLINT_OVERRIDE_STYLE: ${{ inputs.override-style }}
9296
HADOLINT_IGNORE: ${{ inputs.ignore }}
9397
HADOLINT_TRUSTED_REGISTRIES: ${{ inputs.trusted-registries }}
98+
HADOLINT_WORKING_DIRECTORY: ${{ inputs.working-directory }}
9499

95100
HADOLINT_CONFIG: ${{ inputs.config }}
96101
HADOLINT_RECURSIVE: ${{ inputs.recursive }}

hadolint.sh

+54-20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
# checkout (outside the Docker container running hadolint). We copy
44
# problem-matcher.json to the home folder.
55

6+
# unset certain env vars to empty values
7+
RESULTS=''
8+
# shellcheck disable=SC2034
9+
HADOLINT_RESULTS=''
10+
11+
# disable cheks for undefined env vars, in here mostly githu env vars
12+
# shellcheck disable=SC2154
13+
14+
if [[ -n "${HADOLINT_WORKING_DIRECTORY}" ]]; then
15+
cd "${HADOLINT_WORKING_DIRECTORY}" \
16+
|| { echo "Error: failed to change path to ${HADOLINT_WORKING_DIRECTORY}, check if exists, if is a directory directory permissions etc"; exit 1; }
17+
fi
18+
619
PROBLEM_MATCHER_FILE="/problem-matcher.json"
7-
if [ -f "$PROBLEM_MATCHER_FILE" ]; then
8-
cp "$PROBLEM_MATCHER_FILE" "$HOME/"
20+
if [[ -f "${PROBLEM_MATCHER_FILE}" ]]; then
21+
cp "${PROBLEM_MATCHER_FILE}" "${HOME}/"
922
fi
1023
# After the run has finished we remove the problem-matcher.json from
1124
# the repository so we don't leave the checkout dirty. We also remove
@@ -16,52 +29,73 @@ cleanup() {
1629
}
1730
trap cleanup EXIT
1831

19-
echo "::add-matcher::$HOME/problem-matcher.json"
32+
echo "::add-matcher::${HOME}/problem-matcher.json"
2033

21-
if [ -n "$HADOLINT_CONFIG" ]; then
34+
if [[ -n "${HADOLINT_CONFIG}" ]]; then
2235
HADOLINT_CONFIG="-c ${HADOLINT_CONFIG}"
2336
fi
2437

25-
if [ -z "$HADOLINT_TRUSTED_REGISTRIES" ]; then
38+
if [[ -z "${HADOLINT_TRUSTED_REGISTRIES}" ]]; then
2639
unset HADOLINT_TRUSTED_REGISTRIES
2740
fi
2841

29-
COMMAND="hadolint $HADOLINT_CONFIG"
42+
COMMAND="hadolint ${HADOLINT_CONFIG}"
3043

31-
if [ "$HADOLINT_RECURSIVE" = "true" ]; then
44+
if [[ "${HADOLINT_RECURSIVE}" = "true" ]]; then
3245
shopt -s globstar
33-
3446
filename="${!#}"
3547
flags="${*:1:$#-1}"
3648

37-
RESULTS=$(eval "$COMMAND $flags" -- **/"$filename")
49+
files_found=false
50+
# try to find files to scan but do not end with eror if no files found
51+
# notice that $filename can contain glob char so we add exception here
52+
# shellcheck disable=SC2231
53+
for file in **/${filename}
54+
do
55+
if [[ -e "${file}" ]]
56+
then
57+
files_found=true
58+
break
59+
fi
60+
done
61+
62+
if [[ "${files_found}" = "true" ]]; then
63+
# notice that $filename can contain glob char so we add exception here
64+
# shellcheck disable=SC2086,SC2231,SC2248
65+
RESULTS=$(eval "${COMMAND} ${flags}" -- **/${filename})
66+
else
67+
RESULTS=''
68+
echo "No Dockerfiles detected, skipping processing";
69+
fi
70+
3871
else
3972
flags=$*
40-
RESULTS=$(eval "$COMMAND" "$flags")
73+
RESULTS=$(eval "${COMMAND}" "${flags}")
4174
fi
4275
FAILED=$?
4376

44-
if [ -n "$HADOLINT_OUTPUT" ]; then
45-
if [ -f "$HADOLINT_OUTPUT" ]; then
46-
HADOLINT_OUTPUT="$TMP_FOLDER/$HADOLINT_OUTPUT"
77+
if [[ -n "${HADOLINT_OUTPUT}" ]]; then
78+
if [[ -f "${HADOLINT_OUTPUT}" ]]; then
79+
HADOLINT_OUTPUT="${TMP_FOLDER}/${HADOLINT_OUTPUT}"
4780
fi
48-
echo "$RESULTS" >"$HADOLINT_OUTPUT"
81+
echo "${RESULTS}" >"${HADOLINT_OUTPUT}"
4982
fi
5083

5184
RESULTS="${RESULTS//$'\\n'/''}"
5285

5386
{
5487
echo "results<<EOF"
55-
echo "$RESULTS"
88+
echo "${RESULTS}"
5689
echo "EOF"
57-
} >>"$GITHUB_OUTPUT"
90+
} >>"${GITHUB_OUTPUT}"
5891

5992
{
6093
echo "HADOLINT_RESULTS<<EOF"
61-
echo "$RESULTS"
94+
echo "${RESULTS}"
6295
echo "EOF"
63-
} >>"$GITHUB_ENV"
96+
} >>"${GITHUB_ENV}"
6497

65-
[ -z "$HADOLINT_OUTPUT" ] || echo "Hadolint output saved to: $HADOLINT_OUTPUT"
98+
[[ -z "${HADOLINT_OUTPUT}" ]] || echo "Hadolint output saved to: ${HADOLINT_OUTPUT}"
6699

67-
exit $FAILED
100+
# shellcheck disable=SC2248
101+
exit ${FAILED}

testdata/test_empty_dir/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This directory is intentionally empty.
2+
3+
It is used by the test suite to verify that hadolint action is not executed
4+
if processed directory does not contain any Dockerfile.

0 commit comments

Comments
 (0)