From 8297d08247537ad9226663794f256a75d6ef6180 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Thu, 18 Jul 2024 15:28:22 -0400 Subject: [PATCH] Update (base update) [ghstack-poisoned] --- .github/workflows/runtime_build_and_test.yml | 93 ++++++++- .../download-experimental-build-ghaction.js | 183 ++++++++++++++++++ 2 files changed, 273 insertions(+), 3 deletions(-) create mode 100755 scripts/release/download-experimental-build-ghaction.js diff --git a/.github/workflows/runtime_build_and_test.yml b/.github/workflows/runtime_build_and_test.yml index 181bb2ffa800a..58b65f34ee712 100644 --- a/.github/workflows/runtime_build_and_test.yml +++ b/.github/workflows/runtime_build_and_test.yml @@ -180,9 +180,8 @@ jobs: - name: Archive build uses: actions/upload-artifact@v4 with: - name: build_${{ matrix.worker_id }}_${{ matrix.release_channel }} - path: | - build + name: _build_${{ matrix.worker_id }}_${{ matrix.release_channel }} + path: build test_build: name: yarn test-build @@ -242,6 +241,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -269,6 +269,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -311,6 +312,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -341,6 +343,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -370,6 +373,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -407,6 +411,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - name: Display structure of build @@ -462,6 +467,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - run: ./scripts/circleci/pack_and_store_devtools_artifacts.sh @@ -507,6 +513,7 @@ jobs: - name: Restore archived build uses: actions/download-artifact@v4 with: + pattern: _build_* path: build merge-multiple: true - run: | @@ -515,3 +522,83 @@ jobs: - run: ./scripts/circleci/run_devtools_e2e_tests.js env: RELEASE_CHANNEL: experimental + + # ----- SIZEBOT ----- + download_base_build_for_sizebot: + name: Download base build for sizebot + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18.20.1 + cache: yarn + cache-dependency-path: yarn.lock + - name: Restore cached node_modules + uses: actions/cache@v4 + id: node_modules + with: + path: scripts/release/node_modules + key: ${{ runner.arch }}-${{ runner.os }}-scripts-modules-${{ hashFiles('scripts/release/yarn.lock') }} + - run: yarn install --frozen-lockfile + - run: yarn install --frozen-lockfile + working-directory: scripts/release + - name: Download artifacts for base revision + run: | + git fetch origin main + GH_TOKEN=${{ github.token }} scripts/release/download-experimental-build-ghaction.js --commit=$(git rev-parse origin/main) + mv ./build ./base-build + # TODO: The `download-experimental-build` script copies the npm + # packages into the `node_modules` directory. This is a historical + # quirk of how the release script works. Let's pretend they + # don't exist. + - name: Delete extraneous files + run: rm -rf ./base-build/node_modules + - name: Display structure of base-build + run: ls -R base-build + - name: Archive base-build + uses: actions/upload-artifact@v4 + with: + name: base-build + path: base-build + + sizebot: + name: Run sizebot + needs: download_base_build_for_sizebot + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18.20.1 + cache: yarn + cache-dependency-path: yarn.lock + - name: Restore cached node_modules + uses: actions/cache@v4 + id: node_modules + with: + path: "**/node_modules" + key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }} + - run: yarn install --frozen-lockfile + - name: Restore archived build for PR + uses: actions/download-artifact@v4 + with: + pattern: _build_* + path: build + merge-multiple: true + - name: Display structure of build for PR + run: ls -R build + - name: Restore archived base-build from origin/main + uses: actions/download-artifact@v4 + with: + name: base-build + path: base-build + - name: Display structure of base-build from origin/main + run: ls -R base-build + - run: echo ${{ github.sha }} >> build/COMMIT_SHA + - run: node ./scripts/tasks/danger + - name: Archive sizebot results + uses: actions/upload-artifact@v4 + with: + name: sizebot-message + path: sizebot-message.md diff --git a/scripts/release/download-experimental-build-ghaction.js b/scripts/release/download-experimental-build-ghaction.js new file mode 100755 index 0000000000000..6ee939af87625 --- /dev/null +++ b/scripts/release/download-experimental-build-ghaction.js @@ -0,0 +1,183 @@ +#!/usr/bin/env node + +'use strict'; + +const {join, relative} = require('path'); +const {logPromise, handleError} = require('./utils'); +const yargs = require('yargs'); +const clear = require('clear'); +const theme = require('./theme'); +const {exec} = require('child-process-promise'); +const {existsSync} = require('fs'); + +const argv = yargs.wrap(yargs.terminalWidth()).options({ + releaseChannel: { + alias: 'r', + describe: 'Download the given release channel.', + requiresArg: true, + type: 'string', + choices: ['experimental', 'stable'], + default: 'experimental', + }, + commit: { + alias: 'c', + describe: 'Commit hash to download.', + requiresArg: true, + demandOption: true, + type: 'string', + }, +}).argv; + +function printSummary(commit) { + const commandPath = relative( + process.env.PWD, + join(__dirname, '../download-experimental-build-ghaction.js') + ); + + clear(); + + const message = theme` + {caution An experimental build has been downloaded!} + + You can download this build again by running: + {path ${commandPath}} --commit={commit ${commit}} + `; + + console.log(message.replace(/\n +/g, '\n').trim()); +} + +const OWNER = 'facebook'; +const REPO = 'react'; +const WORKFLOW_ID = 'runtime_build_and_test.yml'; +const GITHUB_HEADERS = ` + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${process.env.GH_TOKEN}" \ + -H "X-GitHub-Api-Version: 2022-11-28"`.trim(); + +function getWorkflowId() { + if (existsSync(join(__dirname, `../../.github/workflows/${WORKFLOW_ID}`))) { + return WORKFLOW_ID; + } else { + throw new Error( + `Incorrect workflow ID: .github/workflows/${WORKFLOW_ID} does not exist. Please check the name of the workflow being downloaded from.` + ); + } +} + +async function getWorkflowRunId(commit) { + const res = await exec( + `curl -L ${GITHUB_HEADERS} https://api.github.com/repos/${OWNER}/${REPO}/actions/workflows/${getWorkflowId()}/runs?head_sha=${commit}&branch=main&exclude_pull_requests=true` + ); + + console.log(res); + const json = JSON.parse(res.stdout); + let workflowRun; + if (json.total_count === 1) { + workflowRun = json.workflow_runs[0]; + } else { + workflowRun = json.workflow_runs.find( + run => run.head_sha === commit && run.head_branch === 'main' + ); + } + + if (workflowRun == null || workflowRun.id == null) { + console.log( + theme`{error The workflow run for the specified commit (${commit}) could not be found.}` + ); + process.exit(1); + } + + return workflowRun.id; +} + +async function getArtifact(workflowRunId, artifactName) { + const res = await exec( + `curl -L ${GITHUB_HEADERS} https://api.github.com/repos/${OWNER}/${REPO}/actions/runs/${workflowRunId}/artifacts?per_page=100&name=${artifactName}` + ); + + console.log(res); + const json = JSON.parse(res.stdout); + let artifact; + if (json.total_count === 1) { + artifact = json.artifacts[0]; + } else { + artifact = json.artifacts.find( + _artifact => _artifact.name === artifactName + ); + } + + if (artifact == null) { + console.log( + theme`{error The specified workflow run (${workflowRunId}) does not contain any build artifacts.}` + ); + process.exit(1); + } + + return artifact; +} + +async function downloadArtifactsFromGitHub(commit, releaseChannel) { + const workflowRunId = await getWorkflowRunId(commit); + const artifact = await getArtifact(workflowRunId, 'artifacts_combined'); + + // Download and extract artifact + const cwd = join(__dirname, '..', '..'); + await exec(`rm -rf ./build`, {cwd}); + await exec( + `curl -L ${GITHUB_HEADERS} ${artifact.archive_download_url} \ + > a.zip && unzip a.zip -d . && rm a.zip build2.tgz && tar -xvzf build.tgz && rm build.tgz`, + { + cwd, + } + ); + + // Copy to staging directory + // TODO: Consider staging the release in a different directory from the CI + // build artifacts: `./build/node_modules` -> `./staged-releases` + if (!existsSync(join(cwd, 'build'))) { + await exec(`mkdir ./build`, {cwd}); + } else { + await exec(`rm -rf ./build/node_modules`, {cwd}); + } + let sourceDir; + // TODO: Rename release channel to `next` + if (releaseChannel === 'stable') { + sourceDir = 'oss-stable'; + } else if (releaseChannel === 'experimental') { + sourceDir = 'oss-experimental'; + } else if (releaseChannel === 'rc') { + sourceDir = 'oss-stable-rc'; + } else if (releaseChannel === 'latest') { + sourceDir = 'oss-stable-semver'; + } else { + console.error('Internal error: Invalid release channel: ' + releaseChannel); + process.exit(releaseChannel); + } + await exec(`cp -r ./build/${sourceDir} ./build/node_modules`, {cwd}); +} + +async function downloadBuildArtifacts(commit, releaseChannel) { + const label = theme`commit {commit ${commit}})`; + return logPromise( + downloadArtifactsFromGitHub(commit, releaseChannel), + theme`Downloading artifacts from GitHub for ${label}` + ); +} + +const main = async () => { + try { + await downloadBuildArtifacts(argv.commit, argv.releaseChannel); + printSummary(argv.commit); + } catch (error) { + handleError(error); + } +}; + +if (process.env.GH_TOKEN == null) { + console.log( + theme`{error Expected GH_TOKEN to be provided as an env variable}` + ); + process.exit(1); +} + +main();