Deploy to GitHub Pages #1880
This file contains hidden or 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: Deploy to GitHub Pages | |
on: | |
push: | |
branches: [main] | |
schedule: | |
- cron: "0 */8 * * *" | |
workflow_dispatch: | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
concurrency: | |
group: "pages" | |
cancel-in-progress: true | |
env: | |
BUILD_PATH: "." | |
NOTION_API_SECRET: ${{ secrets.NOTION_API_SECRET }} | |
DELETE_CACHES_GH_TOKEN: ${{ secrets.DELETE_CACHES_GH_TOKEN }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: github-pages | |
timeout-minutes: 45 | |
steps: | |
- name: Checkout your repository using git | |
uses: actions/checkout@v4 | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 21 | |
# Idea from https://www.voorhoede.nl/en/blog/super-fast-npm-install-on-github-actions/ | |
- name: Restore Node Modules Cache | |
id: npm-cache-restore | |
uses: actions/cache/restore@v4 | |
with: | |
path: ./node_modules | |
key: modules-${{ hashFiles(format('{0}/package-lock.json', env.BUILD_PATH)) }} | |
- name: Clear Previous Node Modules Cache (if needed) | |
if: steps.npm-cache-restore.outputs.cache-hit != 'true' && env.DELETE_CACHES_GH_TOKEN != '' | |
run: | | |
gh cache list --key modules- --json key --jq '.[].key' | xargs -I {} gh cache delete {} | |
env: | |
GH_TOKEN: ${{ env.DELETE_CACHES_GH_TOKEN }} | |
- name: Install dependencies | |
if: steps.npm-cache-restore.outputs.cache-hit != 'true' | |
run: npm ci --ignore-scripts | |
working-directory: ${{ env.BUILD_PATH }} | |
- name: Setup Pages | |
id: pages | |
uses: actions/configure-pages@v5 | |
- name: Restore Cached old_package.json | |
id: restore-old-package-json | |
uses: actions/cache/restore@v4 | |
with: | |
path: ./old_package.json | |
key: old-package-json-cache-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
restore-keys: old-package-json-cache-${{ github.repository_id }}- | |
- name: Restore Cached `tmp` | |
id: cache-tmp-restore | |
uses: actions/cache/restore@v4 | |
with: | |
path: ./tmp | |
key: tmp-webtrotion-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
restore-keys: tmp-webtrotion-${{ github.repository_id }}- | |
- name: Restore Cached `public` | |
id: cache-public-restore | |
uses: actions/cache/restore@v4 | |
with: | |
path: ./public | |
key: public-webtrotion-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
restore-keys: public-webtrotion-${{ github.repository_id }}- | |
- name: Restore Cached constants-config | |
id: restore-constants-config | |
uses: actions/cache/restore@v4 | |
with: | |
path: ./constants-config.json | |
key: constants-config-${{ hashFiles(format('{0}/constants-config.json', env.BUILD_PATH)) }} | |
- name: Process constants-config if cache not hit | |
if: steps.restore-constants-config.outputs.cache-hit != 'true' | |
run: | | |
echo "Cached old_constants-config.json is different than current constants-config; trimming tmp (except .gitkeep) and deleting public/notion..." | |
# Insert any additional processing or cleanup commands as needed. | |
find tmp -mindepth 1 -not -name '.gitkeep' -exec rm -rf {} + | |
rm -rf public/notion | |
- name: Trim public and tmp files based on cache changes | |
env: | |
CACHE_HIT: ${{ steps.npm-cache-restore.outputs.cache-hit }} | |
run: | | |
if [ -f ./old_package.json ]; then | |
# If lastBuildTimestamp has changed, delete everything in tmp except .gitkeep and remove public/notion | |
OLD_LAST_BUILD_STORE=$(jq -r '.cacheVersion.lastBuildTimestampStore' ./old_package.json) | |
NEW_LAST_BUILD_STORE=$(jq -r '.cacheVersion.lastBuildTimestampStore' ./package.json) | |
echo "Old lastBuildTimestampStore: $OLD_LAST_BUILD_STORE" | |
echo "New lastBuildTimestampStore: $NEW_LAST_BUILD_STORE" | |
if [ "$OLD_LAST_BUILD_STORE" != "$NEW_LAST_BUILD_STORE" ]; then | |
echo "lastBuildTimestampStore changed, trimming tmp (except .gitkeep) and deleting public/notion..." | |
find tmp -mindepth 1 -not -name '.gitkeep' -exec rm -rf {} + | |
rm -rf public/notion | |
fi | |
# Check tmpJSONCache (original logic) | |
OLD_TMPJSON=$(jq -r '.cacheVersion.tmpJSONCache' ./old_package.json) | |
NEW_TMPJSON=$(jq -r '.cacheVersion.tmpJSONCache' ./package.json) | |
echo "Old tmpJSONCache: $OLD_TMPJSON" | |
echo "New tmpJSONCache: $NEW_TMPJSON" | |
if [ "$OLD_TMPJSON" != "$NEW_TMPJSON" ]; then | |
echo "tmpJSONCache changed, trimming blocks-html-cache, blocks-json-cache, rss-cache..." | |
rm -rf tmp/blocks-html-cache | |
rm -rf tmp/blocks-json-cache | |
rm -rf tmp/rss-cache | |
fi | |
# If tmpHTMLCache has changed, delete tmp/blocks-html-cache and tmp/rss-cache | |
OLD_TMPHTML=$(jq -r '.cacheVersion.tmpHTMLCache' ./old_package.json) | |
NEW_TMPHTML=$(jq -r '.cacheVersion.tmpHTMLCache' ./package.json) | |
echo "Old tmpHTMLCache: $OLD_TMPHTML" | |
echo "New tmpHTMLCache: $NEW_TMPHTML" | |
if [ "$OLD_TMPHTML" != "$NEW_TMPHTML" ]; then | |
echo "tmpHTMLCache changed, deleting tmp/blocks-html-cache and tmp/rss-cache..." | |
rm -rf tmp/blocks-html-cache | |
rm -rf tmp/rss-cache | |
fi | |
# If tmpOGImagesCache has changed, delete tmp/og-images | |
OLD_TMPOG=$(jq -r '.cacheVersion.tmpOGImagesCache' ./old_package.json) | |
NEW_TMPOG=$(jq -r '.cacheVersion.tmpOGImagesCache' ./package.json) | |
echo "Old tmpOGImagesCache: $OLD_TMPOG" | |
echo "New tmpOGImagesCache: $NEW_TMPOG" | |
if [ "$OLD_TMPOG" != "$NEW_TMPOG" ]; then | |
echo "tmpOGImagesCache changed, deleting tmp/og-images..." | |
rm -rf tmp/og-images | |
fi | |
# If publicNotion has changed, delete public/notion | |
OLD_PUBLIC_NOTION=$(jq -r '.cacheVersion.publicNotion' ./old_package.json) | |
NEW_PUBLIC_NOTION=$(jq -r '.cacheVersion.publicNotion' ./package.json) | |
echo "Old publicNotion: $OLD_PUBLIC_NOTION" | |
echo "New publicNotion: $NEW_PUBLIC_NOTION" | |
if [ "$OLD_PUBLIC_NOTION" != "$NEW_PUBLIC_NOTION" ]; then | |
echo "publicNotion changed, deleting public/notion..." | |
rm -rf public/notion | |
fi | |
else | |
echo "old_package.json not found." | |
if [ "$CACHE_HIT" != "true" ]; then | |
echo "No cache hit for package-lock.json detected; trimming tmp and deleting public/notion..." | |
find tmp -mindepth 1 -not -name '.gitkeep' -exec rm -rf {} + | |
rm -rf public/notion | |
else | |
echo "Cache hit for package-lock.json detected; skipping trimming." | |
fi | |
fi | |
- name: Set and log environment secrets and variables and then Build with Astro and postbuild for pagefind | |
env: | |
NOTION_API_SECRET: ${{ env.NOTION_API_SECRET }} | |
GITHUB_PAGES: true | |
SITE: ${{ steps.pages.outputs.origin }} | |
BASE: ${{ steps.pages.outputs.base_path }} | |
run: | | |
echo "NOTION_API_SECRET = $NOTION_API_SECRET" | |
echo "GITHUB_PAGES = $GITHUB_PAGES" | |
echo "SITE = $SITE" | |
echo "BASE = $BASE" | |
npx --no-install astro build | |
npx --no-install pagefind --site dist | |
working-directory: ${{ env.BUILD_PATH }} | |
- name: Clear Previous `tmp` and `public` Cache (if needed) | |
if: env.DELETE_CACHES_GH_TOKEN != '' | |
run: | | |
gh cache list --json key --jq '.[].key' | grep -E '.*(tmp|public).*(web|blog)trotion' | xargs -I {} gh cache delete {} | |
env: | |
GH_TOKEN: ${{ env.DELETE_CACHES_GH_TOKEN }} | |
- name: Clear Previous `old-package-json-cache` (if needed) | |
if: env.DELETE_CACHES_GH_TOKEN != '' | |
run: | | |
gh cache list --key old-package-json-cache- --json key --jq '.[].key' | xargs -I {} gh cache delete {} | |
env: | |
GH_TOKEN: ${{ env.DELETE_CACHES_GH_TOKEN }} | |
- name: Clear Previous constants config Cache (if needed) | |
if: steps.restore-constants-config.outputs.cache-hit != 'true' && env.DELETE_CACHES_GH_TOKEN != '' | |
run: | | |
gh cache list --key constants-config- --json key --jq '.[].key' | xargs -I {} gh cache delete {} | |
env: | |
GH_TOKEN: ${{ env.DELETE_CACHES_GH_TOKEN }} | |
- name: Save node cache manually | |
if: steps.npm-cache-restore.outputs.cache-hit != 'true' | |
id: npm-cache-save | |
uses: actions/cache/save@v4 | |
with: | |
path: ./node_modules | |
key: modules-${{ hashFiles(format('{0}/package-lock.json', env.BUILD_PATH)) }} | |
- name: Save New `tmp` Cache | |
id: cache-tmp-save | |
uses: actions/cache/save@v4 | |
with: | |
path: ./tmp | |
key: tmp-webtrotion-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
- name: Save New `public` Cache | |
id: cache-public-save | |
uses: actions/cache/save@v4 | |
with: | |
path: ./public | |
key: public-webtrotion-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
- name: Copy package.json to old_package.json | |
run: cp ./package.json ./old_package.json | |
- name: Save old_package.json Cache | |
uses: actions/cache/save@v4 | |
with: | |
path: ./old_package.json | |
key: old-package-json-cache-${{ github.repository_id }}-${{ github.run_id }}-${{ github.run_attempt }} | |
- name: Save Cached constants-config | |
if: steps.restore-constants-config.outputs.cache-hit != 'true' | |
uses: actions/cache/save@v4 | |
with: | |
path: ./constants-config.json | |
key: constants-config-${{ hashFiles(format('{0}/constants-config.json', env.BUILD_PATH)) }} | |
- name: Upload artifact | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: ${{ env.BUILD_PATH }}/dist | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
name: Deploy | |
environment: | |
name: github-pages | |
url: ${{ steps.deployment.outputs.page_url }} | |
steps: | |
- name: Deploy to GitHub Pages | |
id: deployment | |
uses: actions/deploy-pages@v4 | |
workflow-keepalive: | |
if: github.event_name == 'schedule' | |
runs-on: ubuntu-latest | |
permissions: | |
actions: write | |
steps: | |
- uses: liskin/gh-workflow-keepalive@v1 |