Update CDN assets #9858
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: Update CDN assets | |
on: | |
# https://unix.stackexchange.com/questions/519538/how-to-set-cron-job-for-every-5-hours | |
schedule: | |
- cron: '11 */1 * * *' | |
workflow_dispatch: | |
jobs: | |
publish: | |
name: Update assets | |
runs-on: ubuntu-latest | |
steps: | |
- name: Repo check | |
run: | | |
if [[ "$GITHUB_REPOSITORY_OWNER" != "dimisa-RUAdList" ]]; then | |
echo "This GitHub Action is meant to deliver filter lists for RUAdList." | |
echo "You shouldn't need to run this GitHub Action in your fork." | |
echo "If you do, please customize the cron expression above, and" | |
echo "the URLs below." | |
exit 1 | |
fi | |
exit 0 | |
- name: Clone RUAdListCDN | |
uses: actions/checkout@v3 | |
- name: Build directory | |
run: | | |
BUILDDIR=$(mktemp -d) | |
echo "BUILDDIR=$BUILDDIR" >> $GITHUB_ENV | |
echo "Build directory is $BUILDDIR" | |
- name: Copy shell script from uAssets | |
run: | | |
TMPDIR="$(mktemp -d)" | |
git clone --depth=1 --single-branch --branch=master https://github.com/uBlockOrigin/uAssets.git "$TMPDIR" | |
cp -v "$TMPDIR"/tools/make-easylist.mjs ${{ env.BUILDDIR }}/ | |
cp -v "$TMPDIR"/tools/need-patch.mjs ${{ env.BUILDDIR }} | |
cp -v "$TMPDIR"/tools/make-diffpatch.sh ${{ env.BUILDDIR }} | |
cp -v "$TMPDIR"/tools/update-diffpatches.sh ${{ env.BUILDDIR }} | |
- name: Time check | |
run: | | |
UPDATE_PERIOD_IN_HOURS=5 | |
echo "CAN_DO=$(node ${{ env.BUILDDIR }}/need-patch.mjs delay="$UPDATE_PERIOD_IN_HOURS")" >> $GITHUB_ENV | |
- name: Generate version string | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
TAGNAME=$(date -u "+%Y.%-m.%-d.")$(date -u "+%H*60+%M" | bc) | |
echo "TAGNAME=$TAGNAME" >> $GITHUB_ENV | |
echo "Version string is $TAGNAME" | |
- name: Fetch lists from easylist/ruadlist | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
TMPDIR="$(mktemp -d)" | |
git clone --depth=1 --single-branch --branch=master https://github.com/easylist/ruadlist "$TMPDIR" | |
cp -v "$TMPDIR"/*.txt "${{ env.BUILDDIR }}" | |
cp -Rv "$TMPDIR"/advblock "${{ env.BUILDDIR }}" | |
- name: Generate ruadlist.ubo.min.txt from RuAdList-uBO.txt | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
pushd ${{ env.BUILDDIR }} | |
: > ruadlist.template | |
echo "[uBlock Origin 1.0]" >> ruadlist.template | |
echo "! Title: RU AdList for uBO only" >> ruadlist.template | |
echo "! Expires: 4 days" >> ruadlist.template | |
echo "! Last modified: %timestamp%" >> ruadlist.template | |
echo "! Diff-Path: %diffpath%#ruadlist" >> ruadlist.template | |
echo "! Diff-Expires: 5 hours" >> ruadlist.template | |
echo "! License: https://github.com/dimisa-RUAdList/RUAdListCDN/blob/main/LICENSE" >> ruadlist.template | |
echo >> ruadlist.template | |
echo "%include ruadlist:RuAdList-uBO.txt%" >> ruadlist.template | |
node ./make-easylist.mjs in=ruadlist.template out=ruadlist.ubo.min.txt minify=1 | |
popd | |
cp ${{ env.BUILDDIR }}/ruadlist.ubo.min.txt lists/ | |
- name: Patch last-updated field | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
DATE=$(date -Ru) | |
FILTER_LISTS=( "lists/ruadlist.ubo.min.txt" ) | |
for FILE in "${FILTER_LISTS[@]}"; do | |
STAT=$(git diff --numstat $FILE | sed -r '/^1\s+1\s+/d') | |
if [[ -n $STAT ]]; then | |
sed -ir "0,/^! Last modified: /s/^\(! Last modified: \)%timestamp%/\\1$DATE/" $FILE | |
else | |
git checkout -q $FILE | |
fi | |
done | |
- name: Create new patch | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
PATCHES_DIR="patches" | |
${{ env.BUILDDIR }}/make-diffpatch.sh "${{ env.TAGNAME }}" "$PATCHES_DIR" | |
- name: Update existing patches | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
PATCHES_DIR="patches" | |
FILTER_FILES=$(git ls-files --exclude-standard -- lists/*.min.txt) | |
${{ env.BUILDDIR }}/update-diffpatches.sh "$GITHUB_REPOSITORY" "$PATCHES_DIR" "$FILTER_FILES" | |
- name: Commit changes, if any | |
if: ${{ env.CAN_DO == 'yes' }} | |
run: | | |
if [[ -z $(git diff --name-only --cached) ]]; then exit 0; fi | |
git config user.name "GitHub Actions: main" | |
git config user.email "<>" | |
git commit -m "Update modified CDN assets to ${{ env.TAGNAME }}" | |
git push origin main | |
git tag ${{ env.TAGNAME }} | |
git push origin ${{ env.TAGNAME }} |