-
Notifications
You must be signed in to change notification settings - Fork 31
ci: Gate PR Preview Deploys from Outside Contributors #2504
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
Open
rheisler-deque
wants to merge
10
commits into
develop
Choose a base branch
from
ci/2486-gate-docs-pr-preview-deploys
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2d0d52e
ci: add gated pull_request_target PR preview workflow (#2486)
rheisler-deque 6efa194
ci: add PR preview deploy job (#2486)
rheisler-deque 305683a
ci: add PR preview cleanup workflow (#2486)
rheisler-deque fe741b4
docs: Update CONTRIBUTING.md to reflect new process for Amplify previews
rheisler-deque 487ec60
ci: grant issues:write to PR preview deploy job
rheisler-deque 2c4dd95
ci: harden PR preview workflows from self-review
rheisler-deque 78133eb
docs: warn against secrets on the preview environments
rheisler-deque 4dca9f6
Update .github/workflows/pr-preview.yml
rheisler-deque 44d57bc
chore: update all dependency versions to latest per PR feedback
rheisler-deque 1ff7274
Merge remote-tracking branch 'origin/ci/2486-gate-docs-pr-preview-dep…
rheisler-deque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| name: PR Preview Cleanup | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [closed] | ||
| branches: | ||
| - develop | ||
|
|
||
| permissions: | ||
| id-token: write | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3 | ||
| with: | ||
| role-to-assume: ${{ vars.AMPLIFY_PREVIEW_ROLE_ARN }} | ||
| aws-region: us-east-1 | ||
| - name: Delete Amplify preview branch | ||
| env: | ||
| APP_ID: ${{ vars.AMPLIFY_APP_ID }} | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| run: | | ||
| set -euo pipefail | ||
| BRANCH="pr-${PR_NUMBER}" | ||
| if aws amplify get-branch --app-id "$APP_ID" --branch-name "$BRANCH" >/dev/null 2>&1; then | ||
| aws amplify delete-branch --app-id "$APP_ID" --branch-name "$BRANCH" >/dev/null | ||
| echo "Deleted $BRANCH" | ||
| else | ||
| echo "No preview branch $BRANCH to delete" | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| name: PR Preview | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, synchronize, reopened] | ||
| branches: | ||
| - develop | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| resolve: | ||
| runs-on: ubuntu-latest | ||
| permissions: {} | ||
| outputs: | ||
| pr_number: ${{ steps.route.outputs.pr_number }} | ||
| environment: ${{ steps.route.outputs.environment }} | ||
| steps: | ||
| - id: route | ||
| env: | ||
| ASSOCIATION: ${{ github.event.pull_request.author_association }} | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| run: | | ||
| case "$ASSOCIATION" in | ||
| OWNER|MEMBER|COLLABORATOR) environment=pr-preview-auto ;; | ||
| *) environment=pr-preview-gated ;; | ||
| esac | ||
| echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | ||
| echo "environment=$environment" >> "$GITHUB_OUTPUT" | ||
| echo "PR #$PR_NUMBER association $ASSOCIATION routes to $environment" | ||
|
|
||
| build: | ||
| needs: resolve | ||
| runs-on: ubuntu-latest | ||
| # Never attach secrets to pr-preview-auto or pr-preview-gated; this job runs untrusted fork code. | ||
| environment: | ||
|
rheisler-deque marked this conversation as resolved.
|
||
| name: ${{ needs.resolve.outputs.environment }} | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| persist-credentials: false | ||
| - uses: ./.github/actions/dependencies | ||
| - name: Build docs site | ||
| run: pnpm build | ||
| - name: Upload preview site | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: pr-preview-site | ||
| path: docs/dist | ||
| retention-days: 7 | ||
|
|
||
| deploy: | ||
| needs: [resolve, build] | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| id-token: write | ||
| pull-requests: write | ||
| issues: write | ||
| steps: | ||
| - name: Download preview site | ||
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | ||
| with: | ||
| name: pr-preview-site | ||
| path: docs/dist | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3 | ||
| with: | ||
| role-to-assume: ${{ vars.AMPLIFY_PREVIEW_ROLE_ARN }} | ||
| aws-region: us-east-1 | ||
| - name: Deploy to Amplify | ||
| env: | ||
| APP_ID: ${{ vars.AMPLIFY_APP_ID }} | ||
| PR_NUMBER: ${{ needs.resolve.outputs.pr_number }} | ||
| run: | | ||
| set -euo pipefail | ||
| BRANCH="pr-${PR_NUMBER}" | ||
| aws amplify get-branch --app-id "$APP_ID" --branch-name "$BRANCH" >/dev/null 2>&1 || aws amplify create-branch --app-id "$APP_ID" --branch-name "$BRANCH" >/dev/null | ||
| ( cd docs/dist && zip -r -q "$GITHUB_WORKSPACE/site.zip" . ) | ||
| DEPLOYMENT="$(aws amplify create-deployment --app-id "$APP_ID" --branch-name "$BRANCH")" | ||
|
frankensteinke marked this conversation as resolved.
|
||
| JOB_ID="$(echo "$DEPLOYMENT" | jq -r '.jobId')" | ||
| ZIP_URL="$(echo "$DEPLOYMENT" | jq -r '.zipUploadUrl')" | ||
| curl -fsS -H "Content-Type: application/zip" --upload-file site.zip "$ZIP_URL" | ||
| aws amplify start-deployment --app-id "$APP_ID" --branch-name "$BRANCH" --job-id "$JOB_ID" >/dev/null | ||
| - name: Comment preview URL | ||
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| env: | ||
| PR_NUMBER: ${{ needs.resolve.outputs.pr_number }} | ||
| APP_ID: ${{ vars.AMPLIFY_APP_ID }} | ||
| with: | ||
| script: | | ||
| const prNumber = Number(process.env.PR_NUMBER); | ||
| const appId = process.env.APP_ID; | ||
| const marker = '<!-- pr-preview -->'; | ||
| const url = `https://pr-${prNumber}.${appId}.amplifyapp.com`; | ||
| const body = `${marker}\nDocs preview: ${url}`; | ||
| const { owner, repo } = context.repo; | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber | ||
| }); | ||
| const existing = comments.find((c) => c.body?.includes(marker)); | ||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: existing.id, | ||
| body | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber, | ||
| body | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.