Skip to content

Commit 57cf3e8

Browse files
committed
update and fixes
1 parent 5894cc3 commit 57cf3e8

1 file changed

Lines changed: 112 additions & 44 deletions

File tree

.github/workflows/automatic_image_genration.yml

Lines changed: 112 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,141 @@ jobs:
1313

1414
steps:
1515
- name: Checkout repository
16-
uses: actions/checkout@v4 # Updated to Node.js 20 compatible version
17-
with:
18-
fetch-depth: 0 # Ensures all commits are fetched
16+
uses: actions/checkout@v4
17+
# fetch-depth: 0 # Not strictly needed anymore, default checkout is usually fine
1918

2019
- name: Set up JDK 11
21-
uses: actions/setup-java@v3.11.0 # Updated to Node.js 20 compatible version
20+
uses: actions/setup-java@v3
2221
with:
2322
distribution: 'temurin'
2423
java-version: '11'
2524

2625
- name: Install dependencies
2726
run: |
2827
sudo apt-get update
29-
sudo apt-get install -y graphviz xdotool xvfb
30-
sudo snap install drawio --classic
28+
sudo apt-get install -y --no-install-recommends graphviz xvfb
29+
# Install drawio via snap
30+
sudo snap install drawio
31+
# Wait a moment for snap changes to propagate if needed
32+
sleep 5
3133
3234
- name: Download PlantUML jar
3335
run: |
34-
wget https://github.com/plantuml/plantuml/releases/latest/download/plantuml.jar -O /usr/local/bin/plantuml.jar
35-
36-
- name: Check for changed files
37-
id: changed_files
38-
run: |
39-
BASE_SHA=$(git rev-parse HEAD^1)
40-
CHANGED_FILES=$(git diff --name-only $BASE_SHA HEAD | grep -E 'src/plantuml/.*\.puml|src/drawio/.*\.drawio' || true)
41-
echo "changed_files=$CHANGED_FILES" >> $GITHUB_ENV
42-
echo "::set-output name=changed_files::$CHANGED_FILES"
43-
echo "CHANGED_FILES: $CHANGED_FILES"
44-
45-
- name: Debug changed files
46-
run: |
47-
echo "Debug: Changed files are ${{ steps.changed_files.outputs.changed_files }}"
36+
wget https://github.com/plantuml/plantuml/releases/latest/download/plantuml.jar -O /tmp/plantuml.jar
4837
49-
- name: Generate images from changed PlantUML files
50-
if: ${{ steps.changed_files.outputs.changed_files }} != ''
38+
- name: Generate images from PlantUML files
39+
id: generate_plantuml
5140
run: |
52-
for file in $(echo "${{ steps.changed_files.outputs.changed_files }}" | grep 'src/plantuml/.*\.puml' || true); do
53-
echo "Processing PlantUML file: $file"
41+
set -e
42+
GENERATED_COUNT=0
43+
shopt -s nullglob # Ensure loop doesn't run if find returns nothing
44+
while IFS= read -r -d $'\0' file; do
45+
echo "Checking PlantUML file: $file"
5446
output_dir=$(dirname "$file" | sed 's|^src/plantuml|images|')
55-
mkdir -p "$output_dir"
56-
java -jar /usr/local/bin/plantuml.jar -tpng "$file" -o "${PWD}/$output_dir"
57-
java -jar /usr/local/bin/plantuml.jar -tsvg "$file" -o "${PWD}/$output_dir"
58-
done
47+
image_base=$(basename "$file" .puml)
48+
png_path="$output_dir/$image_base.png"
49+
svg_path="$output_dir/$image_base.svg"
50+
51+
# Regenerate if PNG or SVG is missing, OR if the source file is newer than PNG or SVG
52+
if [ ! -f "$png_path" ] || [ ! -f "$svg_path" ] || { [ -f "$png_path" ] && [ "$file" -nt "$png_path" ]; } || { [ -f "$svg_path" ] && [ "$file" -nt "$svg_path" ]; }; then
53+
echo "-> Generating images for $file"
54+
mkdir -p "$output_dir"
55+
# Run generation commands
56+
java -jar /tmp/plantuml.jar -tpng "$file" -o "$output_dir"
57+
java -jar /tmp/plantuml.jar -tsvg "$file" -o "$output_dir"
58+
# Verify files were created (for debugging)
59+
if [ -f "$png_path" ] && [ -f "$svg_path" ]; then
60+
echo " Successfully generated: $png_path and $svg_path"
61+
GENERATED_COUNT=$((GENERATED_COUNT + 1))
62+
else
63+
echo " ERROR: Failed to generate images for $file" >&2
64+
# Consider failing the step: exit 1
65+
fi
66+
else
67+
echo "-> Images for $file are up-to-date."
68+
fi
69+
done < <(find src/plantuml -name "*.puml" -print0) # Use process substitution
70+
71+
echo "Generated/Updated $GENERATED_COUNT PlantUML image sets."
72+
# Set output flag if any images were generated
73+
if [ "$GENERATED_COUNT" -gt 0 ]; then
74+
echo "plantuml_generated=true" >> $GITHUB_OUTPUT
75+
else
76+
echo "plantuml_generated=false" >> $GITHUB_OUTPUT
77+
fi
78+
shell: bash # Explicitly use bash for process substitution
5979

60-
- name: Generate images from changed drawio files
61-
if: ${{ steps.changed_files.outputs.changed_files }} != ''
80+
- name: Generate images from drawio files
81+
id: generate_drawio
6282
run: |
63-
for file in $(echo "${{ steps.changed_files.outputs.changed_files }}" | grep 'src/drawio/.*\.drawio' || true); do
64-
echo "Processing drawio file: $file"
83+
set -e
84+
GENERATED_COUNT=0
85+
shopt -s nullglob # Ensure loop doesn't run if find returns nothing
86+
while IFS= read -r -d $'\0' file; do
87+
echo "Checking drawio file: $file"
6588
output_dir=$(dirname "$file" | sed 's|^src/drawio|images|')
66-
mkdir -p "$output_dir"
67-
xvfb-run -a drawio -x -f png -o "${PWD}/$output_dir/$(basename "$file" .drawio).png" "$file"
68-
xvfb-run -a drawio -x -f svg -o "${PWD}/$output_dir/$(basename "$file" .drawio).svg" "$file"
69-
done
89+
image_base=$(basename "$file" .drawio)
90+
png_path="$output_dir/$image_base.png"
91+
svg_path="$output_dir/$image_base.svg"
7092
71-
- name: Debug images folder
93+
# Regenerate if PNG or SVG is missing, OR if the source file is newer than PNG or SVG
94+
if [ ! -f "$png_path" ] || [ ! -f "$svg_path" ] || { [ -f "$png_path" ] && [ "$file" -nt "$png_path" ]; } || { [ -f "$svg_path" ] && [ "$file" -nt "$svg_path" ]; }; then
95+
echo "-> Generating images for $file"
96+
mkdir -p "$output_dir"
97+
# Add --no-sandbox and try disabling gpu explicitly for drawio CLI in xvfb
98+
xvfb-run --auto-servernum drawio --no-sandbox --disable-gpu --export --format png --output "$png_path" "$file"
99+
xvfb-run --auto-servernum drawio --no-sandbox --disable-gpu --export --format svg --output "$svg_path" "$file"
100+
# Verify files were created (for debugging)
101+
if [ -f "$png_path" ] && [ -f "$svg_path" ]; then
102+
echo " Successfully generated: $png_path and $svg_path"
103+
GENERATED_COUNT=$((GENERATED_COUNT + 1))
104+
else
105+
echo " ERROR: Failed to generate images for $file" >&2
106+
# Consider failing the step: exit 1
107+
fi
108+
else
109+
echo "-> Images for $file are up-to-date."
110+
fi
111+
done < <(find src/drawio -name "*.drawio" -print0) # Use process substitution
112+
113+
echo "Generated/Updated $GENERATED_COUNT drawio image sets."
114+
# Set output flag if any images were generated
115+
if [ "$GENERATED_COUNT" -gt 0 ]; then
116+
echo "drawio_generated=true" >> $GITHUB_OUTPUT
117+
else
118+
echo "drawio_generated=false" >> $GITHUB_OUTPUT
119+
fi
120+
shell: bash # Explicitly use bash for process substitution
121+
122+
- name: Show Git Status for Images
123+
# Run always to see the state before commit attempt
72124
run: |
73-
echo "Contents of the images folder:"
74-
ls -R images
125+
echo "--- Git Status for images directory ---"
126+
git status --porcelain images || echo "No changes or untracked files in images directory."
127+
echo "--- Listing images directory ---"
128+
ls -R images || echo "Images folder does not exist or is empty."
75129
76130
- name: Commit and push generated images
77-
if: ${{ steps.changed_files.outputs.changed_files }} != ''
131+
# Use the outputs from the generation steps. Only proceed if something was potentially generated.
132+
if: steps.generate_plantuml.outputs.plantuml_generated == 'true' || steps.generate_drawio.outputs.drawio_generated == 'true'
78133
run: |
79-
git config --global user.name 'github-actions'
80-
git config --global user.email 'github-actions@github.com'
81-
git add -A images
82-
git commit -m "Generate images from changed PlantUML and drawio files"
134+
echo "Checking for changes in 'images' directory to commit..."
135+
git config --global user.name 'github-actions[bot]'
136+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
137+
138+
# Stage ALL changes within the 'images' directory (new, modified, deleted)
139+
git add images
140+
141+
# Check if there's anything staged within the 'images' pathspec
142+
if git diff --staged --quiet -- images; then
143+
echo "No staged changes detected in 'images' directory after 'git add'. Nothing to commit."
144+
exit 0
145+
fi
146+
147+
echo "Staged changes found. Committing..."
148+
git commit -m "chore: Generate/update images from source files" -m "Generated by GitHub Action."
149+
150+
echo "Pushing changes..."
83151
git push
84152
env:
85153
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)