This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Generate STL and Thumbnails using OpenSCAD | |
on: | |
push: | |
paths: | |
- "*/**/*.txt" | |
- "*/**/*.scad" | |
- ".github/workflows/**/*" | |
jobs: | |
build_and_deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Install OpenSCAD, WebP tools, and Xvfb | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y openscad webp xvfb imagemagick | |
- name: Generate STL files and multiple PNG thumbnails | |
run: | | |
xvfb-run -a --server-args="-screen 0 1024x768x24" bash -c ' | |
find ./keycaps -name "*.scad" | grep -v "^./lib/" | while read scad; do | |
stl_path="${scad%.scad}.stl" | |
openscad -o $stl_path $scad | |
angles=( | |
"0,0,0,65,0,25,100" | |
"0,0,0,65,0,115,100" | |
"0,0,0,25,25,25,100" | |
"0,0,0,-155,-25,0,100" | |
) | |
thumbnail_counter=1 | |
for angle in "${angles[@]}"; do | |
png_path="${scad%.scad}_$thumbnail_counter.png" | |
openscad --camera=$angle --colorscheme="Tomorrow" --render --imgsize=512,512 -o $png_path $scad | |
thumbnail_counter=$((thumbnail_counter + 1)) | |
done | |
done | |
' | |
- name: Combine thumbnails into a single image | |
run: | | |
find ./keycaps -name "*.stl" | while read stl; do | |
base_name="${stl%.stl}" | |
output="${base_name}.png" | |
montage \ | |
"${base_name}_1.png" "${base_name}_2.png" \ | |
"${base_name}_3.png" "${base_name}_4.png" \ | |
-geometry +0+0 -tile 2x2 \ | |
$output | |
rm "${base_name}_1.png" "${base_name}_2.png" \ | |
"${base_name}_3.png" "${base_name}_4.png" | |
done | |
- name: Convert PNG thumbnails to WebP | |
run: | | |
for png in $(find ./keycaps -name "*.png"); do | |
cwebp $png -o ${png%.png}.webp | |
done | |
- name: Clean up directory (Only keep STL, WebP files and txt descriptions) | |
run: | | |
rm -f .gitignore | |
find . ! -path "./.git/*" ! -name ".git" -type f ! \( -name "*.stl" -o -name "*.webp" -o -name "*.txt" \) | |
find . ! -path "./.git/*" ! -name ".git" -type f ! \( -name "*.stl" -o -name "*.webp" -o -name "*.txt" \) -exec rm -rf {} \; | |
ls -R | |
- name: Generate index.html | |
run: | | |
repo_url="https://github.com/$GITHUB_REPOSITORY/blob/gh-pages" | |
echo "<!DOCTYPE html><html><head><link href='https://cdn.jsdelivr.net/npm/tailwindcss@2.2.16/dist/tailwind.min.css' rel='stylesheet'></head><body class='bg-gray-200'>" > index.html | |
echo '<div class="container mx-auto px-4">' >> index.html | |
echo "<h1 class='my-8 text-4xl font-extrabold leading-none tracking-tight text-gray-900'>$GITHUB_REPOSITORY</h1>" >> index.html | |
find ./keycaps -name "*.stl" | sort | while read stl; do | |
scad_name="${stl%.stl}.scad" | |
txt_name="${stl%.stl}.txt" | |
thumbnail_name="${stl%.stl}.webp" | |
stl_link="https://github.com/$GITHUB_REPOSITORY/blob/gh-pages/${stl#./}" | |
github_link="https://github.com/$GITHUB_REPOSITORY/blob/main/$scad_name" | |
title="${stl#./}" | |
echo "<div class='bg-white w-full flex rounded overflow-hidden shadow-lg mb-8'>" >> index.html | |
echo " <div class='h-48 w-48 flex-none bg-cover rounded-l text-center overflow-hidden' style='background-image: url(\"$thumbnail_name\")'></div>" >> index.html | |
echo " <div class='px-4 py-2 flex flex-col justify-between leading-normal'>" >> index.html | |
echo " <div>" >> index.html | |
echo " <div class='text-gray-900 font-bold text-xl mb-2'>$title</div>" >> index.html | |
if [ -f $txt_name ]; then | |
echo " <div class='text-gray-700 text-sm'>" >> index.html | |
awk 'BEGIN{ORS="<br/>"} NF{print $0}' $txt_name | sed '$ s/<br\/>$//' >> index.html | |
echo " </div>" >> index.html | |
fi | |
echo " </div>" >> index.html | |
echo " <div>" >> index.html | |
echo " <a href='$github_link' class='inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2'>View on GitHub</a>" >> index.html | |
echo " <a href='$stl_link' class='inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2'>Preview STL</a>" >> index.html | |
echo " </div>" >> index.html | |
echo " </div>" >> index.html | |
echo "</div>" >> index.html | |
done | |
echo "</div></body></html>" >> index.html | |
- name: Deploy to GitHub Pages | |
run: | | |
git config user.name "GitHub Actions Bot" | |
git config user.email "masuidrive@users.noreply.githufullb.com" | |
git checkout -b gh-pages | |
find ./keycaps -type f \( -name "*.stl" -o -name "*.webp" \) -exec git add {} \; | |
git add index.html | |
git commit -m "Update STL files and thumbnails of https://github.com/$GITHUB_REPOSITORY" | |
git push -f origin gh-pages |