-
Notifications
You must be signed in to change notification settings - Fork 197
86 lines (85 loc) · 4.66 KB
/
Copy pathcloudflare.yml
File metadata and controls
86 lines (85 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
on:
workflow_call:
jobs:
deploy:
env:
# https://docs.github.com/en/actions/learn-github-actions/contexts#example-contents-of-the-github-context
PROJECT_NAME: web # cloudflare bug? our project name/alias is web-29e but wrangler wants just "web"
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
# https://github.com/actions/runner/issues/409#issuecomment-752775072
# Github Actions DSL expression fuckery to make a ternary
# syntax is if condition && true || false
# i.e. if it's a pull request, use the head ref, otherwise use the ref name
# so ultimately we only run this workflow against the whitelisted branches
if: contains(fromJSON('["develop", "release", "yeet", "main", "private", "beard", "juice"]'), github.event_name == 'pull_request' && github.head_ref || github.ref_name)
runs-on: ubuntu-latest
name: Build
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Fetch all history and tags for version extraction via git describe
fetch-depth: 0
- name: Setup Environment (PR)
if: ${{ github.event_name == 'pull_request' }}
# github actions run in the context of a special magical merge commit between the head of the PR
# and the base branch. this means that the commit hash is not the same as the head of the PR.
# we have to conditionally grab the head of the PR and truncate it to 7 characters.
# in a `pull_request`` event trigger, base_ref is target branch name, head_ref is current
run: |
echo "COMMIT_SHORT_HASH=`echo ${{ github.event.pull_request.head.sha }} | cut -c1-7`" >> ${GITHUB_ENV}
echo "CURRENT_BRANCH_NAME=${{ github.head_ref }}" >> ${GITHUB_ENV}
# in a `pull` event trigger, e.g. yoloing to develop or main, GITHUB_SHA is the head commit
# of the branch and we can use that directly.
# ref_name is the name of the branch, e.g. develop, main, etc.
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
run: |
echo "COMMIT_SHORT_HASH=`echo ${GITHUB_SHA} | cut -c1-7`" >> ${GITHUB_ENV}
echo "CURRENT_BRANCH_NAME=${{ github.ref_name }}" >> ${GITHUB_ENV}
- name: Extract Version
run: |
# Get version from the closest git tag (e.g., "v1.977.0" -> "1.977.0")
# Tags are created by release-please when PRs are merged to main
# Fallback to commit hash if no tags exist
TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [[ "$TAG" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
echo "version=${BASH_REMATCH[1]}" >> ${GITHUB_ENV}
else
echo "version=${{ env.COMMIT_SHORT_HASH }}" >> ${GITHUB_ENV}
fi
- name: Determine Build Mode
run: |
if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "release" || "${{ github.head_ref }}" == "main" || "${{ github.head_ref }}" == "release" ]]; then
echo "BUILD_MODE=production" >> ${GITHUB_ENV}
elif [[ "${{ github.ref_name }}" == "private" || "${{ github.head_ref }}" == "private" ]]; then
echo "BUILD_MODE=private" >> ${GITHUB_ENV}
else
echo "BUILD_MODE=development" >> ${GITHUB_ENV}
fi
- name: Debug GitHub Context
env: { CONTENT : "${{ toJson(github) }}" }
run: "echo $CONTENT"
- name: Setup pnpm
run: corepack enable && corepack prepare pnpm@10.30.3 --activate
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
- name: Cache node_modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: ${{ runner.os }}-node-modules-
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Build Web
run: MODE=${{ env.BUILD_MODE }} DEPLOY=true VITE_VERSION=${{ env.version }} pnpm run build:web
# a poor mans extension of cloudflare/pages-action@1 https://github.com/cloudflare/pages-action/blob/main/src/index.ts
# passing more params directly to wrangler to control commit hash and message
# https://developers.cloudflare.com/workers/wrangler/commands/#publish-1
- name: Deploy
run: npx -y wrangler@2 pages publish build --project-name="${{ env.PROJECT_NAME }}" --branch="${{ env.CURRENT_BRANCH_NAME }}" --commit-hash="${{ env.COMMIT_SHORT_HASH }}" --commit-message="${{ env.CURRENT_BRANCH_NAME }}-${{ env.COMMIT_SHORT_HASH }}"