Skip to content

Instantly share code, notes, and snippets.

@jondkelley
Created July 9, 2019 15:03
Show Gist options
  • Save jondkelley/39e4f8bc683e63aa301fdc9a3ad0b7ac to your computer and use it in GitHub Desktop.
Save jondkelley/39e4f8bc683e63aa301fdc9a3ad0b7ac to your computer and use it in GitHub Desktop.
Lint Dockerfiles for CI/CD
#!/usr/bin/env bash
# usage: ./lint-dockerfile.sh <path-to-dockerfile>
# purpose: lints dockerfile for common errors
# requires: docker to be installed
DOCKERFILE_PATH=$1
echo "Linting dockerfile : DOCKERFILE_PATH"
docker run -it --rm --privileged -v `pwd`:/root/ projectatomic/dockerfile-lint dockerfile_lint -f ${DOCKERFILE_PATH} | grep ERRORS > dockerfile-lint.out
# isolate lint warnings
awk '/WARNINGS/,/INFO/' dockerfile-lint.out | sed '$d' | tail +3 > dockerfile-lint-warnings.out
testok = "true"
if [ $? -eq 0 ]; then
echo "Linting dockerfile : WARNINGS found"
# Print warnings
cat dockerfile-lint-warnings.out
fi
# isolate lint info
awk '/ERRORS/,/WARNINGS/' dockerfile-lint.out | sed '$d' | tail +3 > dockerfile-lint-info.out
grep '--------INFO---------' dockerfile-lint.out
if [ $? -eq 0 ]; then
echo "Linting dockerfile : INFORMATION output generated"
# Print up to 25 lines of info notices
grep -A25 "INFO" dockerfile-lint.out | tail +3
fi
# isolate lint errors
awk '/ERRORS/,/WARNINGS/' dockerfile-lint.out | sed '$d' | tail +3 > dockerfile-lint-errors.out
if [ $? -eq 0 ]; then
testok = "false"
echo "Linting dockerfile : ERRORS found"
# Print errors
cat dockerfile-lint-errors.out
fi
if [ "$testok" != "true" ]; then
echo "Linting dockerfile : Tests failed, check job output"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment