diff --git a/.tangled/workflows/stars.yml b/.tangled/workflows/stars.yml index b92f615..d30d470 100644 --- a/.tangled/workflows/stars.yml +++ b/.tangled/workflows/stars.yml @@ -26,16 +26,10 @@ dependencies: - openssh - coreutils - bash - - zstd clone: depth: 1 -cache: - - key: aoc-stars-pages - paths: - - aoc-cache - environment: RUSTUP_HOME: /tangled/workspace/.rustup CARGO_HOME: /tangled/workspace/.cargo @@ -45,67 +39,75 @@ steps: - name: Fetch calendars command: | set -eu - # AoC asks for automated requests no more often than every 15 minutes. The pages - # and the time of the last fetch live in the cache, so pushes inside that window - # reuse them and the table is rebuilt from the same pages. - dir=aoc-cache - pages=$dir/pages + # AoC asks for automated requests no more often than every 15 minutes. The time of + # the last fetch is the file fetched_at on the stars-cache branch. Inside that + # window this run does nothing: the README stays as it is, so nothing is zeroed. + url="https://$TANGLED_REPO_KNOT/$TANGLED_REPO_REPO_DID" + mkdir -p /tangled/workspace/stamp + if git -C /tangled/workspace/stamp init -q 2>/dev/null && + git -C /tangled/workspace/stamp fetch -q --depth=1 "$url" refs/heads/stars-cache 2>/dev/null; then + last=$(git -C /tangled/workspace/stamp show FETCH_HEAD:fetched_at 2>/dev/null || echo 0) + age=$(( $(date +%s) - last )) + if [ "$age" -ge 0 ] && [ "$age" -lt 900 ]; then + echo "AoC was fetched ${age}s ago (limit 900s). Leaving the README as it is." + touch /tangled/workspace/skip_stars + exit 0 + fi + fi + pages=/tangled/workspace/stars + rm -rf "$pages" + mkdir -p "$pages" last=$(date +%Y) if [ "$(date +%m)" != 12 ]; then last=$((last - 1)); fi - complete=1 - year=2015 - while [ "$year" -le "$last" ]; do - [ -s "$pages/$year.html" ] || complete=0 - year=$((year + 1)) - done - age=999999 - if [ -f "$dir/fetched_at" ]; then age=$(( $(date +%s) - $(cat "$dir/fetched_at") )); fi - if [ "$complete" = 1 ] && [ "$age" -ge 0 ] && [ "$age" -lt 900 ]; then - echo "Using cached AoC pages, fetched ${age}s ago (limit 900s)." - exit 0 - fi - rm -rf "$dir/new" - mkdir -p "$dir/new" year=2015 while [ "$year" -le "$last" ]; do curl --fail --silent --show-error --max-time 30 --retry 2 \ --user-agent "ch0.dev aoc stars workflow (git@ch0.dev)" \ --header "Cookie: session=$AOC_SESSION" \ - --output "$dir/new/$year.html" \ + --output "$pages/$year.html" \ "https://adventofcode.com/$year" year=$((year + 1)) sleep 2 done - # every page fetched: replace the cached set in one go - rm -rf "$pages" - mv "$dir/new" "$pages" - date +%s > "$dir/fetched_at" + date +%s > /tangled/workspace/fetched_at echo "Fetched AoC pages for 2015-$last." - name: Build table command: | set -eu + if [ -e /tangled/workspace/skip_stars ]; then echo "Skipped."; exit 0; fi rustup default stable - cargo run --release --quiet -- stars aoc-cache/pages + cargo run --release --quiet -- stars /tangled/workspace/stars git --no-pager diff --stat - name: Commit table command: | set -eu - if git diff --quiet -- README.md; then - echo "README is already up to date." - exit 0 - fi + if [ -e /tangled/workspace/skip_stars ]; then echo "Skipped."; exit 0; fi # ssh refuses to run without a passwd entry for the current uid if ! getent passwd "$(id -u)" >/dev/null; then echo "$(id -un 2>/dev/null || echo runner):x:$(id -u):$(id -g)::/tangled/workspace:/bin/sh" >> /etc/passwd fi key=/tangled/workspace/push_key (umask 077; printf '%s\n' "$STARS_PUSH_KEY" > "$key") + export GIT_SSH_COMMAND="ssh -i $key -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/tangled/workspace/known_hosts" + remote="git@$TANGLED_REPO_KNOT:$TANGLED_REPO_REPO_DID" git config user.name "aoc stars" git config user.email "git@ch0.dev" - git add README.md - git commit --quiet --message "Update stars table" - GIT_SSH_COMMAND="ssh -i $key -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/tangled/workspace/known_hosts" \ - git push --push-option=skip-ci "git@$TANGLED_REPO_KNOT:$TANGLED_REPO_REPO_DID" HEAD:refs/heads/main + if git diff --quiet -- README.md; then + echo "README is already up to date." + else + git add README.md + git commit --quiet --message "Update stars table" + git push --push-option=skip-ci "$remote" HEAD:refs/heads/main + fi + # record when AoC was fetched, as a new commit on the stars-cache branch + stamp=/tangled/workspace/stamp + git -C "$stamp" config user.name "aoc stars" + git -C "$stamp" config user.email "git@ch0.dev" + git -C "$stamp" fetch -q --depth=1 "$remote" refs/heads/stars-cache 2>/dev/null && git -C "$stamp" checkout -q FETCH_HEAD || git -C "$stamp" checkout -q --orphan stars-cache + cp /tangled/workspace/fetched_at "$stamp/fetched_at" + git -C "$stamp" add fetched_at + git -C "$stamp" commit --quiet --message "AoC fetched at $(cat /tangled/workspace/fetched_at)" + git -C "$stamp" push --push-option=skip-ci "$remote" HEAD:refs/heads/stars-cache rm -f "$key"