feat: add github action to deploy to cloudflare pages #32
Workflow file for this run
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: Deploy to Cloudflare Pages | |
on: | |
pull_request: | |
push: | |
branches: | |
- "main" | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: write | |
steps: | |
- name: Extract branch name | |
shell: bash | |
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
id: extract_branch | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ steps.extract_branch.outputs.branch }} | |
- name: Get latest commit hash of the PR branch | |
run: | | |
# Get the latest commit hash of the source branch of the PR | |
PR_BRANCH="${{ github.head_ref }}" | |
echo "Pull request branch: $PR_BRANCH" | |
# Fetch the commit hash for the PR branch | |
git fetch origin $PR_BRANCH:$PR_BRANCH | |
LATEST_COMMIT=$(git log -n 1 --oneline $PR_BRANCH | cut -d ' ' -f 1) | |
echo "Latest commit hash: $LATEST_COMMIT" | |
echo "LATEST_COMMIT=${LATEST_COMMIT}" >> $GITHUB_ENV | |
- name: debug | |
run: | | |
echo ${{ github.head_ref }} | |
echo ${{github.event.pull_request.base.sha}} | |
echo ${{github.ref}} | |
- name: Get Head Commit Message | |
shell: bash | |
id: get_head_commit_message | |
run: echo "commit=$(git show -s --format=%s)" >> "$GITHUB_OUTPUT" | |
# stolen from: https://github.com/cloudflare/pages-action/issues/16#issuecomment-1398478711 | |
- name: Comment deploy start | |
if: github.event_name != 'push' | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message-id: cloudflare-deploy | |
message: | | |
### <span aria-hidden="true">π§</span> Deploy Preview building... | |
| Name | Link | | |
|---------------------------------|------------------------| | |
|<span aria-hidden="true">π¨</span> Latest commit | ${{ github.sha }} | | |
|<span aria-hidden="true">π</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
--- | |
- name: Install rust toolchain | |
uses: dtolnay/rust-toolchain@stable | |
with: | |
toolchain: 1.81.0 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "20.2" | |
- name: Install wasm-pack | |
run: | | |
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh | |
- name: Install npm dependencies | |
working-directory: crates/org-wasm | |
run: npm install | |
- name: Generate wasm binaries | |
working-directory: crates/org-wasm | |
run: wasm-pack build --release --target bundler | |
- name: Deploy npm | |
working-directory: crates/org-wasm | |
run: npm run deploy | |
- name: Publish to Cloudflare | |
id: deploy | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
command: pages deploy crates/org-wasm/dist --project-name=org-rust --branch ${{ steps.extract_branch.outputs.branch }} --commit-hash ${{ github.head_ref }} --commit-message "${{ steps.get_head_commit_message.outputs.commit }}" | |
- name: Comment deploy url | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message-id: cloudflare-deploy | |
message: | | |
### <span aria-hidden="true">β </span> Deploy Preview ready! | |
| Name | Link | | |
|---------------------------------|------------------------| | |
|<span aria-hidden="true">π¨</span> Latest commit | ${{ github.sha }} | | |
|<span aria-hidden="true">π</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
|<span aria-hidden="true">π</span> Deploy Preview Url | [${{ steps.deploy.outputs.pages-deployment-alias-url }}](${{ steps.deploy.outputs.pages-deployment-alias-url }}) | | |
--- |