@@ -56,8 +56,30 @@ if [[ ! -v CLOUD_IMAGE_NAME ]]; then
5656 exit 1
5757fi
5858
59- # Check for dangling symbolic links (target does not exist).
60- DANGLING_LINKS=$( find -L . -type l)
59+ # In the following sections we will want to check that the files that we are
60+ # packaging in the Docker image don't have outside symbolic links.
61+ # However, we want to exclude files and dirs listed in `.dockerignore` from this check
62+
63+ # Build find exclusion arguments
64+ EXCLUDE_PATHS=()
65+ if [ -f .dockerignore ]; then
66+ while IFS= read -r pattern; do
67+ # Ignore empty lines and comments
68+ if [[ -n " $pattern " && ! " $pattern " =~ ^# ]]; then
69+ EXCLUDE_PATHS+= (-o -path " ./$pattern " )
70+ fi
71+ done < .dockerignore
72+ fi
73+
74+ PRUNE_ARGS= ()
75+ if [ ${# EXCLUDE_PATHS[@]} -gt 0 ]; then
76+ # Remove leading -o and create the prune expression for find
77+ # This tells find not to descend into these paths.
78+ PRUNE_ARGS=(\( " ${EXCLUDE_PATHS[@]: 1} " \) -prune -o)
79+ fi
80+
81+ # Check for dangling symbolic links (target does not exist), excluding .dockerignore paths.
82+ DANGLING_LINKS= $( find -L . " ${PRUNE_ARGS[@]} " -type l -print)
6183if [ -n " $DANGLING_LINKS " ]; then
6284 echo " ERROR: Found dangling symbolic links in the build context:"
6385 echo " $DANGLING_LINKS "
@@ -67,7 +89,7 @@ if [ -n "$DANGLING_LINKS" ]; then
6789fi
6890
6991# Check for absolute symbolic links, which Docker can't follow outside the build context.
70- ABSOLUTE_LINKS=$( find . -type l -lname ' /*' )
92+ ABSOLUTE_LINKS= $( find . " ${PRUNE_ARGS[@]} " -type l -lname ' /*' -print )
7193if [ -n " $ABSOLUTE_LINKS " ]; then
7294 echo " ERROR: Found symbolic links with absolute paths in the build context:"
7395 echo " $ABSOLUTE_LINKS "
0 commit comments