Skip to content

Commit

Permalink
ci: cloudflare deploy (#3919)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdef1cafe committed Mar 1, 2023
1 parent 588b4cf commit de9733d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ jobs:
name: Call
uses: ./.github/workflows/pr.yml
needs: [install-and-cache]

call-cloudflare-workflow:
name: Call
uses: ./.github/workflows/cloudflare.yml
secrets: inherit # pass org/repo secrets to the called workflow
needs: [install-and-cache]
53 changes: 53 additions & 0 deletions .github/workflows/cloudflare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
on:
workflow_call:

jobs:
deploy:
env:
# head_ref is current branch name
# https://docs.github.com/en/actions/learn-github-actions/contexts#example-contents-of-the-github-context
GITHUB_BRANCH_NAME: ${{ github.head_ref }}
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 }}
if: contains(fromJSON('["develop", "release", "yeet", "main", "private"]'), ${{ github.head_ref }})
runs-on: size-bertha
name: Build
steps:
- name: Checkout
uses: actions/checkout@v3
- 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.
run: |
echo "COMMIT_SHORT_HASH=`echo ${{ github.event.pull_request.head.sha }} | cut -c1-7`" >> ${GITHUB_ENV}
# in the case of a direct push to a branch, e.g. yoloing to develop or main, GITHUB_SHA is the head commit
# of the branch and we can use that directly.
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
run: |
echo "COMMIT_SHORT_HASH=`echo ${GITHUB_SHA} | cut -c1-7`" >> ${GITHUB_ENV}
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Cache
uses: actions/cache@v3
with:
path: |
node_modules
~/.cache/Cypress
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
- name: Yarn Install
run: yarn install --frozen-lockfile
- name: Build
run: yarn env && yarn build
# 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.GITHUB_BRANCH_NAME }}" --commit-hash="${{ env.COMMIT_SHORT_HASH }}" --commit-message="${{ env.GITHUB_BRANCH_NAME }}-${{ env.COMMIT_SHORT_HASH }}"
2 changes: 1 addition & 1 deletion scripts/bootstrap-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getSerializedEnvVars = (environment: Environment) => {
*/
const getSpecifiedEnvironment = (): Environment => {
const args = process.argv.slice(2)
const branch = process.env.CF_PAGES_BRANCH
const branch = process.env.CF_PAGES_BRANCH || process.env.GITHUB_BRANCH_NAME

// we're in a CI environment - we called the script as `yarn env` and hope the branch is set
if (branch) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const createDraftPR = async (): Promise<void> => {
const { messages } = await getCommits('release')
// TODO(0xdef1cafe): parse version bump from commit messages
const nextVersion = await getNextReleaseVersion('minor')
const title = `chore: release ${nextVersion} [DO NOT MERGE]`
const title = `chore: release ${nextVersion}`
const command = `gh pr create --draft --base "main" --title "${title}" --body "${messages}"`
console.log(chalk.green('Creating draft PR...'))
await pify(exec)(command)
Expand Down
1 change: 1 addition & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const getHeadShortCommitHash = async (): Promise<string> =>
export const getSemverTags = async (): Promise<string[]> => {
// safety in case we pick up other tags from other packages
const WEB_VERSION_RANGES = '>1.0.0 <2.0.0'
await git().fetch(['origin', '--tags', '--force'])
const tags = await git().tags()
const allTags: string[] = tags.all
const validTags: string[] = allTags
Expand Down
4 changes: 2 additions & 2 deletions scripts/writeBuildMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable no-console */
import { writeFileSync } from 'fs'

import { getHeadShortCommitHash, getLatestSemverTag } from './utils'
import { getLatestSemverTag } from './utils';

const main = async () => {
const latestTag = await getLatestSemverTag()
const headShortCommitHash = await getHeadShortCommitHash()
const headShortCommitHash = process.env.COMMIT_SHORT_HASH // comes from cloudflare.yml
const payload = JSON.stringify({ latestTag, headShortCommitHash }, null, 2)
console.log(payload)
writeFileSync('./build/metadata.json', payload)
Expand Down

0 comments on commit de9733d

Please sign in to comment.