Files
b4l-project-template/.gitea/workflows/auto-sort-files.yml
grabowski 4371c0f201
All checks were successful
Auto-Sort Files / auto-sort (push) Successful in 5s
Fix workflow file movement detection issue
- Replace pipe-based while loop with process substitution to preserve variable scope
- Use temporary file to track FILES_MOVED status across subshells
- Fix issue where files were moved but commit/push steps were skipped
- Use find with -print0 and while read -d '' for proper handling of filenames with spaces
- Ensure FILES_MOVED flag is properly set when files are actually moved
- Now correctly triggers commit and push steps when files are reorganized
2025-08-11 11:16:49 +07:00

133 lines
5.1 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Gitea Actions Auto-Sort Configuration for Datasheets and Images
# This workflow automatically moves files with specific prefixes to their correct directories
name: Auto-Sort Files
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
auto-sort:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.CI_BOT_TOKEN }}
- name: Auto-sort datasheets and images
run: |
#!/bin/bash
set -e
# Create a temporary file to track if files were moved
TEMP_FLAG=$(mktemp)
echo "false" > "$TEMP_FLAG"
# Function to move files with logging
move_file() {
local src="$1"
local dest="$2"
local dest_dir=$(dirname "$dest")
# Create destination directory if it doesn't exist
mkdir -p "$dest_dir"
# Move file if it's not already in the correct location
if [ "$src" != "$dest" ] && [ -f "$src" ]; then
echo "Moving: $src -> $dest"
mv "$src" "$dest"
echo "true" > "$TEMP_FLAG"
fi
}
# Find and move datasheet files (DS_ prefix)
echo "Searching for datasheet files with DS_ prefix..."
while IFS= read -r -d '' file; do
# Skip files already in docs/datasheets/
if [[ "$file" != ./docs/datasheets/* ]]; then
filename=$(basename "$file")
move_file "$file" "docs/datasheets/$filename"
fi
done < <(find . -name "DS_*.pdf" -o -name "DS_*.doc" -o -name "DS_*.docx" -o -name "DS_*.txt" -print0)
# Find and move image files (IMG_ prefix)
echo "Searching for image files with IMG_ prefix..."
while IFS= read -r -d '' file; do
# Skip files already in docs/images/
if [[ "$file" != ./docs/images/* ]]; then
filename=$(basename "$file")
move_file "$file" "docs/images/$filename"
fi
done < <(find . -name "IMG_*.png" -o -name "IMG_*.jpg" -o -name "IMG_*.jpeg" -o -name "IMG_*.svg" -o -name "IMG_*.gif" -o -name "IMG_*.pdf" -o -name "IMG_*.webp" -print0)
# Check if any files were moved
FILES_MOVED=$(cat "$TEMP_FLAG")
rm "$TEMP_FLAG"
if [ "$FILES_MOVED" = "true" ]; then
echo "Files were moved. Setting output for commit step."
echo "files_moved=true" >> $GITHUB_OUTPUT
else
echo "No files needed to be moved."
echo "files_moved=false" >> $GITHUB_OUTPUT
fi
id: sort_files
- name: Commit moved files
if: steps.sort_files.outputs.files_moved == 'true'
run: |
git config --local user.email "ci-bot@b4l.co.th"
git config --local user.name "B4L CI Auto-Sort Bot"
git add docs/datasheets/ docs/images/
git commit -m "Auto-sort: Move DS_ and IMG_ prefixed files to correct directories
- Moved datasheet files (DS_*) to docs/datasheets/
- Moved image files (IMG_*) to docs/images/
- Automated by Gitea Actions auto-sort workflow"
- name: Push changes
if: steps.sort_files.outputs.files_moved == 'true'
run: |
# Get the repository URL dynamically
REPO_URL=$(git config --get remote.origin.url)
# Extract the base URL and repository path
if [[ $REPO_URL == https://* ]]; then
# HTTPS URL format: https://git.b4l.co.th/B4L/repo-name.git
BASE_URL=$(echo $REPO_URL | sed 's|https://\([^/]*\)/.*|https://\1|')
REPO_PATH=$(echo $REPO_URL | sed 's|https://[^/]*/\(.*\)|\1|')
PUSH_URL="${BASE_URL}/ci-bot:${{ secrets.CI_BOT_TOKEN }}@${REPO_PATH}"
else
# SSH URL format: git@git.b4l.co.th:B4L/repo-name.git
DOMAIN=$(echo $REPO_URL | sed 's|git@\([^:]*\):.*|\1|')
REPO_PATH=$(echo $REPO_URL | sed 's|git@[^:]*:\(.*\)|\1|')
PUSH_URL="https://ci-bot:${{ secrets.CI_BOT_TOKEN }}@${DOMAIN}/${REPO_PATH}"
fi
echo "Pushing to: $PUSH_URL"
git push $PUSH_URL HEAD:${{ github.ref_name }}
- name: Create summary
run: |
echo "## Auto-Sort Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.sort_files.outputs.files_moved }}" = "true" ]; then
echo "✅ Files were automatically moved to correct directories" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Moved Files:" >> $GITHUB_STEP_SUMMARY
echo "- Datasheet files (DS_*) → docs/datasheets/" >> $GITHUB_STEP_SUMMARY
echo "- Image files (IMG_*) → docs/images/" >> $GITHUB_STEP_SUMMARY
else
echo " No files needed to be moved - all files are properly organized" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Supported Prefixes:" >> $GITHUB_STEP_SUMMARY
echo "- **DS_**: Datasheets (PDF, DOC, DOCX, TXT)" >> $GITHUB_STEP_SUMMARY
echo "- **IMG_**: Images (PNG, JPG, SVG, GIF, PDF, WEBP)" >> $GITHUB_STEP_SUMMARY