From 029dfd32a251c9fb1a0b503bf6144787709c03b5 Mon Sep 17 00:00:00 2001 From: Grace Park Date: Tue, 30 Nov 2021 11:02:31 -0800 Subject: [PATCH] Migrate Prod build deploy to actions-script (#23228) * revert to regular runner and migrate js to actions-scripts * add prod deploy migration * update .js * add run_id --- .github/actions-scripts/prod-deploy.js | 51 +++++++++++++ .../purge-fastly-edge-cache.js | 11 +++ .github/workflows/prod-build-deploy.yml | 75 +------------------ 3 files changed, 66 insertions(+), 71 deletions(-) create mode 100644 .github/actions-scripts/prod-deploy.js create mode 100644 .github/actions-scripts/purge-fastly-edge-cache.js diff --git a/.github/actions-scripts/prod-deploy.js b/.github/actions-scripts/prod-deploy.js new file mode 100644 index 0000000000..387e1397be --- /dev/null +++ b/.github/actions-scripts/prod-deploy.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +import getOctokit from '../../script/helpers/github.js' +import deployToProduction from '../../script/deployment/deploy-to-production.js' + +const { + GITHUB_TOKEN, + HEROKU_API_TOKEN, + HEROKU_PRODUCTION_APP_NAME, + SOURCE_BLOB_URL, + DELAY_FOR_PREBOOT, + RUN_ID, +} = process.env + +// Exit if GitHub Actions PAT is not found +if (!GITHUB_TOKEN) { + throw new Error('You must supply a GITHUB_TOKEN environment variable!') +} + +// Exit if Heroku API token is not found +if (!HEROKU_API_TOKEN) { + throw new Error('You must supply a HEROKU_API_TOKEN environment variable!') +} + +// Exit if Heroku App name is not found +if (!HEROKU_PRODUCTION_APP_NAME) { + throw new Error('You must supply a HEROKU_PRODUCTION_APP_NAME environment variable!') +} + +if (!RUN_ID) { + throw new Error('$RUN_ID not set') +} + +// This helper uses the `GITHUB_TOKEN` implicitly! +// We're using our usual version of Octokit vs. the provided `github` +// instance to avoid versioning discrepancies. +const octokit = getOctokit() + +try { + await deployToProduction({ + octokit, + includeDelayForPreboot: DELAY_FOR_PREBOOT !== 'false', + // These parameters will ONLY be set by Actions + sourceBlobUrl: SOURCE_BLOB_URL, + runId: RUN_ID, + }) +} catch (error) { + console.error(`Failed to deploy to production: ${error.message}`) + console.error(error) + throw error +} diff --git a/.github/actions-scripts/purge-fastly-edge-cache.js b/.github/actions-scripts/purge-fastly-edge-cache.js new file mode 100644 index 0000000000..9582be1031 --- /dev/null +++ b/.github/actions-scripts/purge-fastly-edge-cache.js @@ -0,0 +1,11 @@ +#!/usr/bin/env node + +import purgeEdgeCache from '../../script/deployment/purge-edge-cache.js' + +try { + await purgeEdgeCache() +} catch (error) { + console.error(`Failed to purge the edge cache: ${error.message}`) + console.error(error) + throw error +} diff --git a/.github/workflows/prod-build-deploy.yml b/.github/workflows/prod-build-deploy.yml index 137c6acfd0..23ee91c54d 100644 --- a/.github/workflows/prod-build-deploy.yml +++ b/.github/workflows/prod-build-deploy.yml @@ -21,7 +21,7 @@ concurrency: jobs: build-and-deploy: if: ${{ github.repository == 'github/docs-internal'}} - runs-on: ${{ fromJSON('["ubuntu-latest", "self-hosted"]')[github.repository == 'github/docs-internal'] }} + runs-on: ubuntu-latest timeout-minutes: 15 steps: - name: Check out repo @@ -129,12 +129,8 @@ jobs: -H 'Content-Type:' \ --data-binary @app.tar.gz - - name: Install one-off development-only dependencies - run: npm install --no-save --include=optional esm - - name: Deploy id: deploy - uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }} @@ -144,56 +140,8 @@ jobs: SOURCE_BLOB_URL: ${{ steps.build-source.outputs.download_url }} DELAY_FOR_PREBOOT: 'true' ALLOWED_POLLING_FAILURES_PER_PHASE: '15' - with: - script: | - const { - GITHUB_TOKEN, - HEROKU_API_TOKEN, - HEROKU_PRODUCTION_APP_NAME, - SOURCE_BLOB_URL, - DELAY_FOR_PREBOOT - } = process.env - - // Exit if GitHub Actions PAT is not found - if (!GITHUB_TOKEN) { - throw new Error('You must supply a GITHUB_TOKEN environment variable!') - } - - // Exit if Heroku API token is not found - if (!HEROKU_API_TOKEN) { - throw new Error('You must supply a HEROKU_API_TOKEN environment variable!') - } - - // Exit if Heroku App name is not found - if (!HEROKU_PRODUCTION_APP_NAME) { - throw new Error('You must supply a HEROKU_PRODUCTION_APP_NAME environment variable!') - } - - // Workaround to allow us to load ESM files with `require(...)` - const esm = require('esm') - require = esm({}) - - const { default: getOctokit } = require('./script/helpers/github') - const { default: deployToProduction } = require('./script/deployment/deploy-to-production') - - // This helper uses the `GITHUB_TOKEN` implicitly! - // We're using our usual version of Octokit vs. the provided `github` - // instance to avoid versioning discrepancies. - const octokit = getOctokit() - - try { - await deployToProduction({ - octokit, - includeDelayForPreboot: DELAY_FOR_PREBOOT !== 'false', - // These parameters will ONLY be set by Actions - sourceBlobUrl: SOURCE_BLOB_URL, - runId: context.runId - }) - } catch (error) { - console.error(`Failed to deploy to production: ${error.message}`) - console.error(error) - throw error - } + RUN_ID: ${{ github.run_id }} + run: .github/actions-scripts/prod-deploy.js - name: Mark the deployment as inactive if timed out uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d @@ -227,26 +175,11 @@ jobs: console.log('⏲️ Deployment status: error - The deployment timed out...') - name: Purge Fastly edge cache - uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d env: FASTLY_TOKEN: ${{ secrets.FASTLY_TOKEN }} FASTLY_SERVICE_ID: ${{ secrets.FASTLY_SERVICE_ID }} FASTLY_SURROGATE_KEY: 'all-the-things' - with: - script: | - // Workaround to allow us to load ESM files with `require(...)` - const esm = require('esm') - require = esm({}) - - const { default: purgeEdgeCache } = require('./script/deployment/purge-edge-cache') - - try { - await purgeEdgeCache() - } catch (error) { - console.error(`Failed to purge the edge cache: ${error.message}`) - console.error(error) - throw error - } + run: .github/actions-scripts/purge-fastly-edge-cache.js - name: Send Slack notification if workflow failed uses: someimportantcompany/github-actions-slack-message@f8d28715e7b8a4717047d23f48c39827cacad340