Issue
Hadolint is an awesome tool for linting Dockerfiles. I am trying to integrated to my CI but I am dealing with for run over multiple Dockerfiles. Does someone know how the syntax look like? Here is how my dirs appears to:
dir1/Dockerfile
dir2/Dockerfile
dir3/foo/Dockerfile
in gitlab-ci
stage: hadolint
image: hadolint/hadolint:latest-debian
script:
- mkdir -p reports
- |
hadolint dir1/Dockerfile > reports/dir1.json \
hadolint dir2/Dockerfile > reports/dir2.json \
hadolint dir3/foo/Dockerfile > reports/dir3.json
But the sample above is now working.
Solution
If you want to keep all reports separated (one per top-level directory), you may want to rely on some shell snippet?
I mean something like:
- |
find . -name Dockerfile -exec \
sh -c 'src=${1#./} && { set -x && hadolint "$1"; } | tee -a "reports/${src%%/*}.txt"' sh "{}" \;
Explanation:
find . -name Dockerfile
loops over all Dockerfiles in the current directory;-exec sh -c '…'
runs a subshell for each Dockerfile, setting:$0 = "sh"
(dummy value)$1 = "{}"
(the full, relative path of the Dockerfile),"{}"
and\;
being directly related to thefind … -exec
pattern;
src=${1#./}
trims the path, replacing./dir1/Dockerfile
withdir1/Dockerfile
${src%%/*}
extracts the top-level directory name (dir1/Dockerfile
→dir1
)- and
| tee -a …
copies the output, appendinghadolint
's output to the top-level directory report file, for each parsed Dockerfile (while> …
should be avoided here for obvious reasons, if you have several Dockerfiles in a single top-level directory). - I have replaced the
.json
extension with.txt
ashadolint
does not seem to output JSON data.
Answered By - ErikMD