Sync files only if sources are updated #11
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: Sync files only if sources are updated | |
on: | |
schedule: | |
- cron: '0 0 2 * *' | |
workflow_dispatch: | |
jobs: | |
sync_files: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# hold the list of updated files | |
- name: Initialize updated files list | |
run: echo "" > /tmp/updated_files.txt | |
# compare and update the file if changed, with error checking for curl | |
- name: Sync sponsorblock_minimal.lua if updated | |
run: | | |
curl -s -w "%{http_code}" -o /tmp/sponsorblock_minimal.lua https://codeberg.org/jouni/mpv_sponsorblock_minimal/raw/branch/master/sponsorblock_minimal.lua | tee /tmp/curl_code | |
if [ $(cat /tmp/curl_code) -ne 200 ]; then | |
echo "Failed to download sponsorblock_minimal.lua (HTTP code $(cat /tmp/curl_code))" | |
elif ! cmp -s /tmp/sponsorblock_minimal.lua scripts/sponsorblock_minimal.lua; then | |
mv /tmp/sponsorblock_minimal.lua scripts/sponsorblock_minimal.lua | |
echo "scripts/sponsorblock_minimal.lua" >> /tmp/updated_files.txt | |
else | |
echo "sponsorblock_minimal.lua is up-to-date, no changes" | |
fi | |
- name: Sync thumbfast.lua if updated | |
run: | | |
curl -s -w "%{http_code}" -o /tmp/thumbfast.lua https://raw.githubusercontent.com/po5/thumbfast/master/thumbfast.lua | tee /tmp/curl_code | |
if [ $(cat /tmp/curl_code) -ne 200 ]; then | |
echo "Failed to download thumbfast.lua (HTTP code $(cat /tmp/curl_code))" | |
elif ! cmp -s /tmp/thumbfast.lua scripts/thumbfast.lua; then | |
mv /tmp/thumbfast.lua scripts/thumbfast.lua | |
echo "scripts/thumbfast.lua" >> /tmp/updated_files.txt | |
else | |
echo "thumbfast.lua is up-to-date, no changes" | |
fi | |
- name: Debug updated files | |
run: | | |
echo "Updated files:" | |
cat /tmp/updated_files.txt || echo "No files were updated" | |
# Commit and push only if files from /tmp/updated_files.txt have been updated | |
- name: Commit and push changes | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "GitHub Actions" | |
# Remove empty lines from the updated_files.txt | |
sed -i '/^$/d' /tmp/updated_files.txt | |
# Check if /tmp/updated_files.txt exists and is non-empty | |
if [ -s /tmp/updated_files.txt ]; then | |
# Iterate over each file in /tmp/updated_files.txt and add only if it exists | |
while IFS= read -r file; do | |
if [ -f "$file" ]; then | |
git add "$file" | |
else | |
echo "File $file not found, skipping." | |
fi | |
done < /tmp/updated_files.txt | |
# Check if there are any staged changes for commit | |
if [ -n "$(git status --porcelain)" ]; then | |
git commit -m "Sync files from various sources if updated" | |
git push | |
else | |
echo "No changes to commit after staging" | |
fi | |
else | |
echo "No files were updated" | |
rm -f /tmp/updated_files.txt # Remove the file if it's empty or not needed | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |