-
Notifications
You must be signed in to change notification settings - Fork 4
155 lines (137 loc) · 6.54 KB
/
automatic_image_genration.yml
File metadata and controls
155 lines (137 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: Automatic Image Generation
on:
push:
paths:
- 'src/plantuml/**'
- 'src/drawio/**'
- '.github/workflows/generate_images.yml'
workflow_dispatch:
jobs:
generate_images:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
# fetch-depth: 0 # Not strictly needed anymore, default checkout is usually fine
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends graphviz xvfb
# Install drawio via snap
sudo snap remove drawio
sudo snap install drawio
# Wait a moment for snap changes to propagate if needed
sleep 5
- name: Download PlantUML jar
run: |
wget https://github.com/plantuml/plantuml/releases/latest/download/plantuml.jar -O /tmp/plantuml.jar
- name: Generate images from PlantUML files
id: generate_plantuml
run: |
set -e
GENERATED_COUNT=0
shopt -s nullglob # Ensure loop doesn't run if find returns nothing
while IFS= read -r -d $'\0' file; do
echo "Checking PlantUML file: $file"
output_dir=$(dirname "$file" | sed 's|^src/plantuml|images|')
image_base=$(basename "$file" .puml)
png_path="$output_dir/$image_base.png"
svg_path="$output_dir/$image_base.svg"
# Regenerate if PNG or SVG is missing, OR if the source file is newer than PNG or SVG
if [ ! -f "$png_path" ] || [ ! -f "$svg_path" ] || { [ -f "$png_path" ] && [ "$file" -nt "$png_path" ]; } || { [ -f "$svg_path" ] && [ "$file" -nt "$svg_path" ]; }; then
echo "-> Generating images for $file"
mkdir -p "$output_dir"
# Run generation commands
java -jar /tmp/plantuml.jar -tpng "$file" -o "$output_dir"
java -jar /tmp/plantuml.jar -tsvg "$file" -o "$output_dir"
# Verify files were created (for debugging)
if [ -f "$png_path" ] && [ -f "$svg_path" ]; then
echo " Successfully generated: $png_path and $svg_path"
GENERATED_COUNT=$((GENERATED_COUNT + 1))
else
echo " ERROR: Failed to generate images for $file" >&2
# Consider failing the step: exit 1
fi
else
echo "-> Images for $file are up-to-date."
fi
done < <(find src/plantuml -name "*.puml" -print0) # Use process substitution
echo "Generated/Updated $GENERATED_COUNT PlantUML image sets."
# Set output flag if any images were generated
if [ "$GENERATED_COUNT" -gt 0 ]; then
echo "plantuml_generated=true" >> $GITHUB_OUTPUT
else
echo "plantuml_generated=false" >> $GITHUB_OUTPUT
fi
shell: bash # Explicitly use bash for process substitution
- name: Generate images from drawio files
id: generate_drawio
run: |
set -e
GENERATED_COUNT=0
shopt -s nullglob # Ensure loop doesn't run if find returns nothing
while IFS= read -r -d $'\0' file; do
echo "Checking drawio file: $file"
output_dir=$(dirname "$file" | sed 's|^src/drawio|images|')
image_base=$(basename "$file" .drawio)
png_path="$output_dir/$image_base.png"
svg_path="$output_dir/$image_base.svg"
# Regenerate if PNG or SVG is missing, OR if the source file is newer than PNG or SVG
if [ ! -f "$png_path" ] || [ ! -f "$svg_path" ] || { [ -f "$png_path" ] && [ "$file" -nt "$png_path" ]; } || { [ -f "$svg_path" ] && [ "$file" -nt "$svg_path" ]; }; then
echo "-> Generating images for $file"
mkdir -p "$output_dir"
# Add --no-sandbox and try disabling gpu explicitly for drawio CLI in xvfb
xvfb-run --auto-servernum drawio --no-sandbox --disable-gpu --export --format png --output "$png_path" "$file"
xvfb-run --auto-servernum drawio --no-sandbox --disable-gpu --export --format svg --output "$svg_path" "$file"
# Verify files were created (for debugging)
if [ -f "$png_path" ] && [ -f "$svg_path" ]; then
echo " Successfully generated: $png_path and $svg_path"
GENERATED_COUNT=$((GENERATED_COUNT + 1))
else
echo " ERROR: Failed to generate images for $file" >&2
# Consider failing the step: exit 1
fi
else
echo "-> Images for $file are up-to-date."
fi
done < <(find src/drawio -name "*.drawio" -print0) # Use process substitution
echo "Generated/Updated $GENERATED_COUNT drawio image sets."
# Set output flag if any images were generated
if [ "$GENERATED_COUNT" -gt 0 ]; then
echo "drawio_generated=true" >> $GITHUB_OUTPUT
else
echo "drawio_generated=false" >> $GITHUB_OUTPUT
fi
shell: bash # Explicitly use bash for process substitution
- name: Show Git Status for Images
# Run always to see the state before commit attempt
run: |
echo "--- Git Status for images directory ---"
git status --porcelain images || echo "No changes or untracked files in images directory."
echo "--- Listing images directory ---"
ls -R images || echo "Images folder does not exist or is empty."
- name: Commit and push generated images
# Use the outputs from the generation steps. Only proceed if something was potentially generated.
if: steps.generate_plantuml.outputs.plantuml_generated == 'true' || steps.generate_drawio.outputs.drawio_generated == 'true'
run: |
echo "Checking for changes in 'images' directory to commit..."
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Stage ALL changes within the 'images' directory (new, modified, deleted)
git add images
# Check if there's anything staged within the 'images' pathspec
if git diff --staged --quiet -- images; then
echo "No staged changes detected in 'images' directory after 'git add'. Nothing to commit."
exit 0
fi
echo "Staged changes found. Committing..."
git commit -m "chore: Generate/update images from source files" -m "Generated by GitHub Action."
echo "Pushing changes..."
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}