From 4aa2668b03f047344da663f56f35af750b813960 Mon Sep 17 00:00:00 2001 From: 0xdef1cafe <88504456+0xdef1cafe@users.noreply.github.com> Date: Tue, 28 Feb 2023 09:20:15 -0800 Subject: [PATCH] ci: cloudflare deploy (#3919) --- .github/workflows/bootstrap.yml | 6 ++++ .github/workflows/cloudflare.yml | 51 ++++++++++++++++++++++++++++++++ scripts/bootstrap-env.ts | 2 +- scripts/release.ts | 2 +- scripts/utils.ts | 1 + scripts/writeBuildMetadata.ts | 4 +-- 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/cloudflare.yml diff --git a/.github/workflows/bootstrap.yml b/.github/workflows/bootstrap.yml index e0d948a73ac..a9ae26a74da 100644 --- a/.github/workflows/bootstrap.yml +++ b/.github/workflows/bootstrap.yml @@ -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] diff --git a/.github/workflows/cloudflare.yml b/.github/workflows/cloudflare.yml new file mode 100644 index 00000000000..1db414ba417 --- /dev/null +++ b/.github/workflows/cloudflare.yml @@ -0,0 +1,51 @@ +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 + permissions: + contents: read + deployments: write + name: Build + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Environment (PR) + if: ${{ github.event_name == 'pull_request' }} + run: | + echo "COMMIT_SHORT_HASH=`echo ${{ github.event.pull_request.head.sha }} | cut -c1-7`" >> ${GITHUB_ENV} + - 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 }}" diff --git a/scripts/bootstrap-env.ts b/scripts/bootstrap-env.ts index 232fa02a262..cb8ba403656 100644 --- a/scripts/bootstrap-env.ts +++ b/scripts/bootstrap-env.ts @@ -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) { diff --git a/scripts/release.ts b/scripts/release.ts index 021efb240d1..b4c4973d273 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -60,7 +60,7 @@ const createDraftPR = async (): Promise => { 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) diff --git a/scripts/utils.ts b/scripts/utils.ts index ae8e1ddf119..4479a4f878f 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -19,6 +19,7 @@ export const getHeadShortCommitHash = async (): Promise => export const getSemverTags = async (): Promise => { // 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 diff --git a/scripts/writeBuildMetadata.ts b/scripts/writeBuildMetadata.ts index 2e695965c04..1249317dc59 100644 --- a/scripts/writeBuildMetadata.ts +++ b/scripts/writeBuildMetadata.ts @@ -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)