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: Enable tests to run on forks and action-created PR #855

Merged
merged 3 commits into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,39 @@ on:
push:
branches: [main]

pull_request:
pull_request_target:
branches: [main]
types: [opened, synchronize, labeled]

permissions:
pull-requests: write # used to remove label
# other permissions are defaulted to "none"

jobs:
setup:
runs-on: ubuntu-latest
# We run tests only if it's:
# 1) push to main, or
# 2) pull request not from a fork (ie. internal PR), or
# 3) pull request from a fork (ie. external PR) that was added "run-tests" label
if: |
github.event_name == 'push' ||
(github.event.pull_request.head.repo.full_name == github.repository) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(github.event.action == 'labeled' && github.event.label.name == 'run-tests')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

steps:
# This would be a good place to run and cache `yarn`, `yarn build`, etc
- name: Remove run-tests label, if applicable
if: always() && github.event.label.name == 'run-tests'
uses: actions/github-script@0.3.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { issue: { number: issue_number }, repo: { owner, repo } } = context;
const label = 'run-tests';
github.issues.removeLabel({ owner, repo, issue_number, name: label });

unit:
needs: setup
runs-on: ubuntu-latest
env:
NODE_ENV: test
Expand All @@ -28,6 +56,10 @@ jobs:
steps:
- name: Checkout Amplify UI
uses: actions/checkout@v2
with:
ref: ${{ github.event_name == 'push' && github.ref || github.event.pull_request.head.ref }}
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false

- name: Setup Node.js LTS
uses: actions/setup-node@v2
Expand Down Expand Up @@ -73,6 +105,13 @@ jobs:
steps:
- name: Checkout Amplify UI
uses: actions/checkout@v2
with:
# For `pull_request_target`, we want ref to point to `pull_request.head.ref` because `github.ref`
# always points to the target branch.
# See: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
ref: ${{ github.event_name == 'push' && github.ref || github.event.pull_request.head.ref }}
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false

- name: Next.js Cache
uses: actions/cache@v2
Expand Down Expand Up @@ -126,6 +165,10 @@ jobs:
steps:
- name: Checkout Amplify UI
uses: actions/checkout@v2
with:
ref: ${{ github.event_name == 'push' && github.ref || github.event.pull_request.head.ref }}
wlee221 marked this conversation as resolved.
Show resolved Hide resolved
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false

- name: Setup Node.js LTS
uses: actions/setup-node@v2
Expand Down