Skip to content

Commit 8661c02

Browse files
authored
[CI] Add auto update workflow for Dockerfile graph (#11879)
Signed-off-by: wineandchord <guoqizhou19@gmail.com>
1 parent ce8d6b7 commit 8661c02

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ repos:
122122
language: system
123123
always_run: true
124124
pass_filenames: false
125+
- id: update-dockerfile-graph
126+
name: Update Dockerfile dependency graph
127+
entry: tools/update-dockerfile-graph.sh
128+
language: script
129+
files: ^docker/Dockerfile$
130+
pass_filenames: false
125131
# Keep `suggestion` last
126132
- id: suggestion
127133
name: Suggestion

tools/update-dockerfile-graph.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
# Check if docker/Dockerfile is staged for commit
8+
if git diff --cached --name-only | grep -q "^docker/Dockerfile$"; then
9+
echo "docker/Dockerfile has changed, attempting to update dependency graph..."
10+
11+
# Check if Docker is installed and running
12+
if ! command -v docker &> /dev/null; then
13+
echo "Warning: Docker command not found. Skipping Dockerfile graph update."
14+
echo "Please install Docker to automatically update the graph: https://docs.docker.com/get-docker/"
15+
exit 0
16+
fi
17+
if ! docker info &> /dev/null; then
18+
echo "Warning: Docker daemon is not running. Skipping Dockerfile graph update."
19+
echo "Please start Docker to automatically update the graph."
20+
exit 0
21+
fi
22+
23+
# Define the target file path
24+
TARGET_GRAPH_FILE="docs/source/assets/contributing/dockerfile-stages-dependency.png"
25+
26+
# Ensure target directory exists
27+
mkdir -p "$(dirname "$TARGET_GRAPH_FILE")"
28+
29+
# Store old image hash in a variable if the file exists
30+
OLD_HASH=""
31+
if [ -f "$TARGET_GRAPH_FILE" ]; then
32+
OLD_HASH=$(sha256sum "$TARGET_GRAPH_FILE")
33+
fi
34+
35+
# Generate Dockerfile graph
36+
echo "Running dockerfilegraph tool..."
37+
docker run \
38+
--rm \
39+
--user "$(id -u):$(id -g)" \
40+
--workdir /workspace \
41+
--volume "$(pwd)":/workspace \
42+
ghcr.io/patrickhoefler/dockerfilegraph:alpine \
43+
--output png \
44+
--dpi 200 \
45+
--max-label-length 50 \
46+
--filename docker/Dockerfile \
47+
--legend
48+
49+
echo "Finding generated PNG file..."
50+
# Check for Dockerfile.png in the root directory (most likely location)
51+
if [ -f "./Dockerfile.png" ]; then
52+
echo "Found generated file at: ./Dockerfile.png"
53+
mv "./Dockerfile.png" "$TARGET_GRAPH_FILE"
54+
else
55+
# Try to find it elsewhere
56+
DOCKERFILE_PNG=$(find . -name "Dockerfile.png" -type f | head -1)
57+
58+
if [ -n "$DOCKERFILE_PNG" ]; then
59+
echo "Found generated file at: $DOCKERFILE_PNG"
60+
mv "$DOCKERFILE_PNG" "$TARGET_GRAPH_FILE"
61+
else
62+
echo "Error: Could not find the generated PNG file"
63+
find . -name "*.png" -type f -mmin -5
64+
exit 1
65+
fi
66+
fi
67+
68+
# Check if the graph has changed
69+
NEW_HASH=$(sha256sum "$TARGET_GRAPH_FILE")
70+
if [ "$NEW_HASH" != "$OLD_HASH" ]; then
71+
echo "Graph has changed. Please stage the updated file: $TARGET_GRAPH_FILE"
72+
exit 1
73+
else
74+
echo "No changes in graph detected."
75+
fi
76+
fi
77+
78+
exit 0

0 commit comments

Comments
 (0)