Skip to content

Build and Deploy to Cloudflare Pages #1259

Build and Deploy to Cloudflare Pages

Build and Deploy to Cloudflare Pages #1259

Workflow file for this run

name: Build and Deploy to Cloudflare Pages
on:
schedule:
- cron: "0 15 * * *"
push:
branches:
- main
workflow_dispatch:
pull_request:
types:
- opened
- synchronize
jobs:
build:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout repository
id: checkout_repo
uses: actions/checkout@v4
- name: Cache assets
uses: actions/cache@v3
with:
path: |
assets-cache
node_modules/.astro
key: assets-${{ github.run_id }} # Save each run's cache
restore-keys: | # Restore most recent cache
assets-
- name: Extract variables
id: extract_vars
run: | # branch_trunc is the branch truncated to 28 chars, which is the CF deploy preview subdomain length
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "datetime=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
branch_trunc=${branch:0:28}
echo "branch=${branch}" >> $GITHUB_OUTPUT
echo "branch_trunc=${branch_trunc}" >> $GITHUB_OUTPUT
- name: Build site
id: build_site
env:
PUBLIC_COMMIT_HASH: ${{ steps.extract_vars.outputs.sha_short }}
JAM_COMMENTS_DOMAIN: tylermercer.net
JAM_COMMENTS_API_KEY: ${{ secrets.JAM_COMMENTS_API_KEY }}
JAM_COMMENTS_ENVIRONMENT: ${{ steps.extract_vars.outputs.branch == 'main' && 'production' || 'preview' }}
run: |
npm ci
npm run build
echo "result=built" >> $GITHUB_OUTPUT
- name: Deploy to Cloudflare Pages
id: deploy
if: steps.build_site.outputs.result == 'built'
run: npx wrangler pages deploy --branch=${{ steps.extract_vars.outputs.branch }}
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Add preview URL to pull request
if: github.event_name == 'pull_request' && steps.build_site.outputs.result == 'built'
uses: thollander/actions-comment-pull-request@v2
with:
comment_tag: deploy_preview
mode: recreate
message: |
## Deployed to Cloudflare Pages! :rocket:
| Name | Result |
| ----------------------- | - |
| **Preview URL**: | https://${{ steps.extract_vars.outputs.branch_trunc }}.${{ secrets.CLOUDFLARE_PROJECT_NAME }}.pages.dev |
| **Last commit:** | `${{ steps.extract_vars.outputs.sha_short }}` |
| **Deployed at**: | `${{ steps.extract_vars.outputs.datetime }}` |
# send-email:
# needs: build
# runs-on: ubuntu-latest
# steps:
# - name: Checkout repository
# id: checkout_repo
# uses: actions/checkout@v3
# - name: Create .env with Github Secrets, Send email if needed
# if: github.event_name == 'schedule'
# id: send_email
# run: |
# touch .env
# echo SENDGRID_API_KEY=$SENDGRID_API_KEY >> .env
# echo SENDGRID_SEGMENT_UNCATEGORIZED=$SENDGRID_SEGMENT_UNCATEGORIZED >> .env
# echo SENDGRID_SEGMENT_SOFTWARE=$SENDGRID_SEGMENT_SOFTWARE >> .env
# echo SENDGRID_SEGMENT_FAITH=$SENDGRID_SEGMENT_FAITH >> .env
# echo SENDGRID_UG_UNCATEGORIZED=$SENDGRID_UG_UNCATEGORIZED >> .env
# echo SENDGRID_UG_SOFTWARE=$SENDGRID_UG_SOFTWARE >> .env
# echo SENDGRID_UG_FAITH=$SENDGRID_UG_FAITH >> .env
# echo SENDGRID_SENDER_ID=$SENDGRID_SENDER_ID >> .env
# npm install
# npm run send-email
# env:
# SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }}
# SENDGRID_SEGMENT_UNCATEGORIZED: ${{ secrets.SENDGRID_SEGMENT_UNCATEGORIZED }}
# SENDGRID_SEGMENT_SOFTWARE: ${{ secrets.SENDGRID_SEGMENT_SOFTWARE }}
# SENDGRID_SEGMENT_FAITH: ${{ secrets.SENDGRID_SEGMENT_FAITH }}
# SENDGRID_UG_UNCATEGORIZED: ${{ secrets.SENDGRID_UG_UNCATEGORIZED }}
# SENDGRID_UG_SOFTWARE: ${{ secrets.SENDGRID_UG_SOFTWARE }}
# SENDGRID_UG_FAITH: ${{ secrets.SENDGRID_UG_FAITH }}
# SENDGRID_SENDER_ID: ${{ secrets.SENDGRID_SENDER_ID }}