| 1 | #!/bin/bash |
| 2 | # Update Dockerfile dependency graph when docker/Dockerfile changes. |
| 3 | # This script is designed to be used as a pre-commit hook. |
| 4 | |
| 5 | set -euo pipefail |
| 6 | |
| 7 | # Accept file paths as arguments |
| 8 | FILES=("$@") |
| 9 | |
| 10 | # Check if docker/Dockerfile is among the provided files |
| 11 | if printf '%s\n' "${FILES[@]}" | grep -q "^docker/Dockerfile$"; then |
| 12 | echo "docker/Dockerfile has changed, attempting to update dependency graph..." |
| 13 | |
| 14 | # Check if Docker is installed and running |
| 15 | if ! command -v docker &> /dev/null; then |
| 16 | echo "Warning: Docker command not found. Skipping Dockerfile graph update." |
| 17 | echo "Please install Docker to automatically update the graph: https://docs.docker.com/get-docker/" |
| 18 | exit 0 |
| 19 | fi |
| 20 | if ! docker info &> /dev/null; then |
| 21 | echo "Warning: Docker daemon is not running. Skipping Dockerfile graph update." |
| 22 | echo "Please start Docker to automatically update the graph." |
| 23 | exit 0 |
| 24 | fi |
| 25 | |
| 26 | # Define the target file path |
| 27 | TARGET_GRAPH_FILE="docs/assets/contributing/dockerfile-stages-dependency.png" |
| 28 | |
| 29 | # Ensure target directory exists |
| 30 | mkdir -p "$(dirname "$TARGET_GRAPH_FILE")" |
| 31 | |
| 32 | # Store old image hash in a variable if the file exists |
| 33 | OLD_HASH="" |
| 34 | if [ -f "$TARGET_GRAPH_FILE" ]; then |
| 35 | OLD_HASH=$(sha256sum "$TARGET_GRAPH_FILE") |
| 36 | fi |
| 37 | |
| 38 | # Generate Dockerfile graph |
| 39 | echo "Running dockerfilegraph tool..." |
| 40 | docker run \ |
| 41 | --rm \ |
| 42 | --user "$(id -u):$(id -g)" \ |
| 43 | --workdir /workspace \ |
| 44 | --volume "$(pwd -P)":/workspace \ |
| 45 | ghcr.io/patrickhoefler/dockerfilegraph:alpine \ |
| 46 | --output png \ |
| 47 | --dpi 200 \ |
| 48 | --max-label-length 50 \ |
| 49 | --filename docker/Dockerfile \ |
| 50 | --concentrate \ |
| 51 | --legend |
| 52 | |
| 53 | echo "Finding generated PNG file..." |
| 54 | # Check for Dockerfile.png in the root directory (most likely location) |
| 55 | if [ -f "./Dockerfile.png" ]; then |
| 56 | echo "Found generated file at: ./Dockerfile.png" |
| 57 | mv "./Dockerfile.png" "$TARGET_GRAPH_FILE" |
| 58 | else |
| 59 | # Try to find it elsewhere |
| 60 | DOCKERFILE_PNG=$(find . -name "Dockerfile.png" -type f | head -1) |
| 61 | |
| 62 | if [ -n "$DOCKERFILE_PNG" ]; then |
| 63 | echo "Found generated file at: $DOCKERFILE_PNG" |
| 64 | mv "$DOCKERFILE_PNG" "$TARGET_GRAPH_FILE" |
| 65 | else |
| 66 | echo "Error: Could not find the generated PNG file" |
| 67 | find . -name "*.png" -type f -mmin -5 |
| 68 | exit 1 |
| 69 | fi |
| 70 | fi |
| 71 | |
| 72 | # Check if the graph has changed |
| 73 | NEW_HASH=$(sha256sum "$TARGET_GRAPH_FILE") |
| 74 | if [ "$NEW_HASH" != "$OLD_HASH" ]; then |
| 75 | echo "Graph has changed. Please stage the updated file: $TARGET_GRAPH_FILE" |
| 76 | exit 1 |
| 77 | else |
| 78 | echo "No changes in graph detected." |
| 79 | fi |
| 80 | fi |
| 81 | |
| 82 | exit 0 |