Sync files only if sources are updated #10
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 select.lua if updated | |
run: | | |
curl -s -w "%{http_code}" -o /tmp/select.lua https://raw.githubusercontent.com/mpv-player/mpv/refs/heads/master/player/lua/select.lua | tee /tmp/curl_code | |
if [ $(cat /tmp/curl_code) -ne 200 ]; then | |
echo "Failed to download select.lua (HTTP code $(cat /tmp/curl_code))" | |
elif ! cmp -s /tmp/select.lua scripts/select.lua; then | |
mv /tmp/select.lua scripts/select.lua | |
echo "scripts/select.lua" >> /tmp/updated_files.txt | |
else | |
echo "select.lua is up-to-date, no changes" | |
fi | |
- name: Sync console.lua if updated | |
run: | | |
curl -s -w "%{http_code}" -o /tmp/console.lua https://raw.githubusercontent.com/mpv-player/mpv/master/player/lua/console.lua | tee /tmp/curl_code | |
if [ $(cat /tmp/curl_code) -ne 200 ]; then | |
echo "Failed to download console.lua (HTTP code $(cat /tmp/curl_code))" | |
elif ! cmp -s /tmp/console.lua scripts/console.lua; then | |
mv /tmp/console.lua scripts/console.lua | |
echo "scripts/console.lua" >> /tmp/updated_files.txt | |
else | |
echo "console.lua is up-to-date, no changes" | |
fi | |
- name: Sync stats.lua if updated | |
run: | | |
curl -s -w "%{http_code}" -o /tmp/stats.lua https://raw.githubusercontent.com/mpv-player/mpv/master/player/lua/stats.lua | tee /tmp/curl_code | |
if [ $(cat /tmp/curl_code) -ne 200 ]; then | |
echo "Failed to download stats.lua (HTTP code $(cat /tmp/curl_code))" | |
elif ! cmp -s /tmp/stats.lua scripts/stats.lua; then | |
mv /tmp/stats.lua scripts/stats.lua | |
echo "scripts/stats.lua" >> /tmp/updated_files.txt | |
else | |
echo "stats.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 }} |