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

Add ability for repo member to trigger deploy previews from forks #55

Merged
merged 1 commit into from
Mar 3, 2025
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: 45 additions & 0 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,56 @@ on:
workflow_dispatch:
pull_request:
branches: [main]
issue_comment:
types: [created]

name: Deploy Preview

jobs:
is-external-pr:
# Be helpful with reviewer and remind them to trigger a deploy preview if the PR is from a fork.
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == true }}
runs-on: ubuntu-latest
steps:
- name: Error with message for manual deploy
run: |
echo "::error title=Manual action required for preview::PR from fork can't be deployed as preview to Netlify automatically. Use '/deploy-preview' command in comments to trigger the preview manually."
shell: bash

role-of-commenter:
if: github.event.issue.pull_request
runs-on: ubuntu-latest
steps:
- name: Check if commenter is a member, owner or collaborator
id: commenter-check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
commenter_role=$(gh api repos/$GITHUB_REPOSITORY/collaborators/${{ github.event.comment.user.login }}/permission --jq '.permission')
echo "commenter_role=$commenter_role" >> "$GITHUB_OUTPUT"
echo "author_association=${{ github.event.comment.author_association }}" >> "$GITHUB_OUTPUT"
shell: bash

build-deploy-preview:
# Deploy a preview only if
# - the PR is not from a fork,
# - requested by PR comment /deploy-preview, from a repo user or github action bot (user id 41898282)
# FIXME: We need to change the way we filter because somehow some MEMBER in API are seen as CONTRIBUTOR in CI
if: >
(
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.fork != true
) ||
(
github.event.issue.pull_request &&
(
github.event.comment.user.id == '41898282' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'COLLABORATOR'
) &&
startsWith(github.event.comment.body, '/deploy-preview')
)
runs-on: ubuntu-latest
steps:
- name: Check out repository
Expand Down