Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(GHA): Improve handling of PRs from forks #3228

Merged
merged 6 commits into from
Apr 13, 2022
Merged

Conversation

jpveooys
Copy link
Collaborator

@jpveooys jpveooys commented Apr 8, 2022

Related issue

Resolves #2737

Overview

This:

  • makes Chromatic work with PRs from forks
  • gets Chromatic reporting its status in GitHub properly again
  • adds a GitHub code security scan for all PRs
  • gracefully skips SonarCloud for PRs from forks (due to incompatibility)
  • removes the use of secrets in cache keys
  • updates all actions to their latest versions

Reason

To make sure PRs can be made from forks without friction.

Work carried out

  • Update all actions to latest versions
  • Reformat all workflows
  • Remove use of secret in cache keys
  • Skip SonarCloud scan on PRs from forks
  • Add GitHub code scanning
  • Handle Chromatic builds in PRs from forks

Developer notes

See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for some detail on the relevant considerations here.

This has been tested on a fork:

Chromatic has been refactored to follow a similar pattern as in the blog post linked above (for all builds). This means Storybook is built in the main PR build and uploaded as an artefact, and then this is downloaded in an on: workflow_run workflow where master is checked out instead of the PR branch and secrets are available.

This also brings back the Chromatic status on PRs:

image

image

These won't appear until the PR is merged (as .github/workflows/post_build_and_test.yml needs to be on master for it to be used).

Branch protection rules will need to be updated after this has been merged to require UI Tests instead of Test_visual_regression.

There's no nice way to make SonarCloud work with forks, because you have to have the secret available and the code checked out at the same time (this could be done, but it's not generally recommended). They have support for forks on their product board: https://portal.productboard.com/sonarsource/1-sonarcloud/c/50-sonarcloud-analyzes-external-pull-request

I added GitHub code scanning to make sure PRs from forks do go through a code security scan.

@jpveooys jpveooys changed the title ci(GHA): Improve handling of forks ci(GHA): Improve handling of PRs from forks Apr 8, 2022
@jpveooys jpveooys force-pushed the ci/handle-forks branch 4 times, most recently from d24e28b to ac7e8b8 Compare April 11, 2022 09:37
@jpveooys jpveooys marked this pull request as ready for review April 11, 2022 09:56
Copy link
Collaborator

@thyhjwb6 thyhjwb6 left a comment

Choose a reason for hiding this comment

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

Nice 👍

@jpveooys jpveooys self-assigned this Apr 11, 2022
@jpveooys jpveooys added Type: Devops DevOps/automation related Package: react-component-library Package/code type labels Apr 11, 2022
Copy link
Collaborator

@m7kvqbe1 m7kvqbe1 left a comment

Choose a reason for hiding this comment

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

Nice changes! Just a couple of small comments.

with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ secrets.CACHE_VERSION }}-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }}
Copy link
Collaborator

@m7kvqbe1 m7kvqbe1 Apr 12, 2022

Choose a reason for hiding this comment

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

What was the reason for that CACHE_VERSION secret?

Was it there as a bit of a hack to manually invalidate the cache?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes that's probably the main reason. The other possibility is changing it after e.g. a Node.js upgrade.

We could keep it in but it won't work for forks or Dependabot. We could also just append e.g. v1 to the cache key and change it in the workflow if a problem came up.

It seems like some APIs to manage the cache may be coming later in the year as well: actions/cache#2 (comment)

uses: actions/github-script@v6
with:
script: |
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth putting this in an external seperate script and referencing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, yes, it was a bit annoying editing that here. It looks like it's possible: https://github.com/actions/github-script#run-a-separate-file

Will give it a go 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got this working – have pushed a fixup.

@m7kvqbe1 m7kvqbe1 self-requested a review April 12, 2022 16:16
These aren't needed and cause inconsistent behaviour for PRs from forks and Dependabot PRs.
Running a SonarCloud scan fails on PRs from forks as it requires secrets. Therefore this skips it for forks (in addition to Dependabot updates).

Running a SonarCloud scan requires secrets to be available and the code being scanned to be checked out, which is not generally recommended for untrusted code. Therefore there is no nice way to make it work.

See also https://portal.productboard.com/sonarsource/1-sonarcloud/c/50-sonarcloud-analyzes-external-pull-request
This adds an additional security using GitHub's CodeQL code scanning feaure. This is useful as SonarCloud doesn't work for forks while code scanning does.
This changes how Chromatic is invoked in order to make it compatible with forks.

It is now triggered using the workflow_run event using a pre-built Storybook, so that it can be run in a trusted environment without exposing secrets to untrusted code.

This also fixes the reporting of Chromatic status for normal PRs.

Finally, this change also fixes running of Chromatic for Dependabot PRs as Chromatic was being run against master in this case.
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@jpveooys jpveooys merged commit 4728697 into master Apr 13, 2022
@jpveooys jpveooys deleted the ci/handle-forks branch April 13, 2022 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: react-component-library Package/code type Type: Devops DevOps/automation related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor CI workflow to allow PR's from forks
3 participants