diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 16ebaaec..8c407ce8 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -11,14 +11,44 @@ on: - dev paths: - 'apps/www/**' + pull_request_target: + types: + # When a created pull request from forked repo, it will be comment 'Should deploy to add label' + - opened + # When a labeled '🚀request-deploy' pull request from forked repo, it will be deploy to Cloudflare Pages + - labeled + paths: + - 'apps/www/**' + # Allows you to run this workflow manually from the Actions tab + # eslint-disable-next-line yml/no-empty-mapping-value + workflow_dispatch: + +permissions: + # default contents: read & write (in forked repos, only read) + contents: write + # default deployments: read & write (in forked repos, only read) + deployments: write + # default pull-requests: read & write (in forked repos, only read) + pull-requests: write jobs: publish: runs-on: ubuntu-latest - permissions: - contents: read - deployments: write name: Publish to Cloudflare Pages + # push event in main branch + # workflow_dispatch event + # pull_request event from not forked repo + # pull_request_target event with label "🚀request-deploy" from forked repo + if: ${{ + github.event_name == 'push' || + github.event_name == 'workflow_dispatch' || + (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) || + (github.event_name == 'pull_request_target' && + github.event.action == 'labeled' && + github.event.pull_request.head.repo.fork == true && + contains(github.event.label.name, '🚀request-deploy')) + }} + steps: - name: Checkout uses: actions/checkout@v3 @@ -70,3 +100,58 @@ jobs: # Optional: Change the working directory workingDirectory: apps/www wranglerVersion: '3' + + - name: Create PR comment + if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} + uses: mshick/add-pr-comment@v2 + with: + message: | + ### ⚡ Successfully Cloudflare Pages deployed! + | Name | Link | + | :--- | :--- | + | Latest commit | ${{ github.event.pull_request.head.sha || github.sha }} | + | Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | + | Deploy Preview Url | [${{ steps.cloudflare-pages-deploy.outputs.url }}](${{ steps.cloudflare-pages-deploy.outputs.url }}) | + | Environment | ${{ steps.cloudflare-pages-deploy.outputs.environment }} | + + - name: Remove label + if: ${{ github.event_name == 'pull_request_target' && contains(github.event.label.name, '🚀request-deploy') }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + github.rest.issues.removeLabel({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + name: ['🚀request-deploy'] + }) + + # Comment on PR from the fork + comment: + name: Comment + runs-on: ubuntu-latest + + # pull_request_target opened event from forked repo + if: ${{ + github.event_name == 'pull_request_target' && + github.event.action == 'opened' && + github.event.pull_request.head.repo.fork == true + }} + + steps: + - name: Create PR comment + run: | + cat << EOF > comment.md + # ⚠️ \`🚀request-deploy\` label is required for deployment + + This repository is a forked repository. + Due to security reasons, deployment from forked repositories does not happen automatically. + + To request deployment, please add the \`🚀request-deploy\` label to this pull request. + (Only certain members have the ability to add labels.) + EOF + + gh pr comment ${{ github.event.number }} -R ${{ github.repository }} -F comment.md + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}