Skip to content

Commit 5674696

Browse files
Switch docs deploy to app-only gh-pages pushes
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 1643c9e commit 5674696

6 files changed

Lines changed: 14 additions & 90 deletions

File tree

.github/PR-PREVIEW.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ This repository publishes docs previews for pull requests under:
88

99
1. On PR open/reopen/synchronize:
1010
- builds docs with preview-specific `site_url`,
11-
- updates `previews/pr-<number>/` content via a PR into `gh-pages`,
12-
- enables auto-merge for that `gh-pages` PR,
11+
- updates `previews/pr-<number>/` content by pushing directly to `gh-pages` as the Scribbler app,
1312
- comments on the source PR with the preview URL,
1413
- reports the preview URL through a named environment (`pr-preview-<number>`).
1514
2. On PR close (merge or abandon):
16-
- removes `previews/pr-<number>/` via a PR into `gh-pages`,
17-
- enables auto-merge for that cleanup PR,
15+
- removes `previews/pr-<number>/` by pushing directly to `gh-pages` as the Scribbler app,
1816
- deletes all preview deployments and the preview environment.
1917

2018
## Required repository configuration
2119

2220
1. Ensure `gh-pages` branch exists.
2321
2. Configure GitHub Pages to publish from `gh-pages`.
24-
3. Protect `gh-pages` so direct commits are blocked and updates happen through PRs only.
25-
4. Allow auto-merge for PRs in this repository.
22+
3. Protect `gh-pages` and restrict push access so **only Scribbler bot app** can push.
23+
4. In the `gh-pages` branch protection/ruleset, add **Scribbler bot app** as the only actor allowed to bypass required pull requests and any required status checks for that branch.
2624

2725
## Scribbler GitHub App permissions
2826

@@ -31,8 +29,7 @@ The app needs the following repository permissions:
3129
| Permission | Access | Why |
3230
| --- | --- | --- |
3331
| Metadata | Read | Required baseline for API access |
34-
| Contents | Read & write | Push preview/cleanup branches and update content |
35-
| Pull requests | Read & write | Create/update/merge PRs into `gh-pages` |
32+
| Contents | Read & write | Push docs and preview content directly to `gh-pages` |
3633
| Issues | Read & write | Post and update preview comments on PR threads |
3734
| Deployments | Read & write | Deactivate and delete preview deployments |
3835
| Administration | Read & write | Delete per-PR environments during cleanup |

.github/scripts/docs/Cleanup-PreviewDocs.ps1

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ param(
99
[string]$PreviewUrl,
1010
[Parameter(Mandatory = $true)]
1111
[string]$EnvironmentName,
12-
[Parameter(Mandatory = $true)]
13-
[string]$HeadBranch,
1412
[string]$PagesDirectory = '_pages',
1513
[string]$BaseBranch = 'gh-pages'
1614
)
@@ -47,16 +45,7 @@ if ($clonedPages) {
4745
$status = (& git -C $PagesDirectory status --porcelain)
4846
if (-not [string]::IsNullOrWhiteSpace($status)) {
4947
Invoke-Git -Arguments @('-C', $PagesDirectory, 'commit', '-m', "Remove docs preview for PR #$PullRequestNumber")
50-
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', '--force-with-lease', 'origin', "HEAD:refs/heads/$HeadBranch")
51-
52-
$env:GH_TOKEN = $Token
53-
$prNumber = Upsert-PullRequest `
54-
-Repository $Repository `
55-
-HeadBranch $HeadBranch `
56-
-BaseBranch $BaseBranch `
57-
-Title "docs(preview): cleanup PR #$PullRequestNumber" `
58-
-Body "Automated cleanup for preview URL: $PreviewUrl"
59-
Enable-PullRequestAutoMerge -Repository $Repository -PullRequestNumber $prNumber
48+
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', 'origin', "HEAD:refs/heads/$BaseBranch")
6049
}
6150
}
6251

.github/scripts/docs/Publish-LiveDocs.ps1

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ param(
66
[Parameter(Mandatory = $true)]
77
[string]$BuildDirectory,
88
[Parameter(Mandatory = $true)]
9-
[string]$HeadBranch,
10-
[Parameter(Mandatory = $true)]
119
[string]$CommitSha,
1210
[string]$PagesDirectory = '_pages',
1311
[string]$BaseBranch = 'gh-pages'
@@ -57,16 +55,6 @@ if ([string]::IsNullOrWhiteSpace($status)) {
5755
}
5856

5957
Invoke-Git -Arguments @('-C', $PagesDirectory, 'commit', '-m', "Deploy docs from $CommitSha")
60-
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', '--force-with-lease', 'origin', "HEAD:refs/heads/$HeadBranch")
61-
62-
$env:GH_TOKEN = $Token
63-
$prNumber = Upsert-PullRequest `
64-
-Repository $Repository `
65-
-HeadBranch $HeadBranch `
66-
-BaseBranch $BaseBranch `
67-
-Title 'docs(publish): update live docs' `
68-
-Body "Automated docs publish from `$CommitSha`nLive URL: https://psmodule.io/docs/"
69-
Enable-PullRequestAutoMerge -Repository $Repository -PullRequestNumber $prNumber
58+
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', 'origin', "HEAD:refs/heads/$BaseBranch")
7059

7160
Set-WorkflowOutput -Name 'has_changes' -Value 'true'
72-
Set-WorkflowOutput -Name 'pr_number' -Value $prNumber

.github/scripts/docs/Publish-PreviewDocs.ps1

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ param(
99
[string]$PreviewUrl,
1010
[Parameter(Mandatory = $true)]
1111
[string]$BuildDirectory,
12-
[Parameter(Mandatory = $true)]
13-
[string]$HeadBranch,
1412
[string]$PagesDirectory = '_pages',
1513
[string]$BaseBranch = 'gh-pages'
1614
)
@@ -53,16 +51,7 @@ Invoke-Git -Arguments @('-C', $PagesDirectory, 'add', '-A')
5351
$status = (& git -C $PagesDirectory status --porcelain)
5452
if (-not [string]::IsNullOrWhiteSpace($status)) {
5553
Invoke-Git -Arguments @('-C', $PagesDirectory, 'commit', '-m', "Update docs preview for PR #$PullRequestNumber")
56-
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', '--force-with-lease', 'origin', "HEAD:refs/heads/$HeadBranch")
57-
58-
$env:GH_TOKEN = $Token
59-
$prNumber = Upsert-PullRequest `
60-
-Repository $Repository `
61-
-HeadBranch $HeadBranch `
62-
-BaseBranch $BaseBranch `
63-
-Title "docs(preview): PR #$PullRequestNumber" `
64-
-Body "Automated preview content update for #$PullRequestNumber`nPreview URL: $PreviewUrl"
65-
Enable-PullRequestAutoMerge -Repository $Repository -PullRequestNumber $prNumber
54+
Invoke-Git -Arguments @('-C', $PagesDirectory, 'push', 'origin', "HEAD:refs/heads/$BaseBranch")
6655
}
6756

6857
$commentBody = "<!-- docs-pr-preview -->`n✅ Preview is ready: $PreviewUrl"

.github/scripts/docs/Shared.ps1

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,6 @@ function Invoke-Gh {
4848
return $output
4949
}
5050

51-
function Upsert-PullRequest {
52-
param(
53-
[Parameter(Mandatory = $true)]
54-
[string]$Repository,
55-
[Parameter(Mandatory = $true)]
56-
[string]$HeadBranch,
57-
[Parameter(Mandatory = $true)]
58-
[string]$BaseBranch,
59-
[Parameter(Mandatory = $true)]
60-
[string]$Title,
61-
[Parameter(Mandatory = $true)]
62-
[string]$Body
63-
)
64-
65-
$existing = Invoke-Gh -Arguments @('pr', 'list', '--repo', $Repository, '--base', $BaseBranch, '--head', $HeadBranch, '--state', 'open', '--json', 'number', '--jq', '.[0].number')
66-
if ([string]::IsNullOrWhiteSpace($existing)) {
67-
return (Invoke-Gh -Arguments @('pr', 'create', '--repo', $Repository, '--base', $BaseBranch, '--head', $HeadBranch, '--title', $Title, '--body', $Body, '--json', 'number', '--jq', '.number'))
68-
}
69-
70-
Invoke-Gh -Arguments @('pr', 'edit', $existing, '--repo', $Repository, '--title', $Title, '--body', $Body) | Out-Null
71-
return $existing
72-
}
73-
74-
function Enable-PullRequestAutoMerge {
75-
param(
76-
[Parameter(Mandatory = $true)]
77-
[string]$Repository,
78-
[Parameter(Mandatory = $true)]
79-
[string]$PullRequestNumber
80-
)
81-
82-
Invoke-Gh -Arguments @('pr', 'merge', $PullRequestNumber, '--repo', $Repository, '--squash', '--auto', '--delete-branch') | Out-Null
83-
}
84-
8551
function Upsert-IssueComment {
8652
param(
8753
[Parameter(Mandatory = $true)]

.github/workflows/Docs.yml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ jobs:
7272
name: github-pages
7373
url: https://psmodule.io/docs/
7474
permissions:
75-
contents: read
76-
pull-requests: write
75+
contents: write
7776
steps:
7877
- name: Checkout repository
7978
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
@@ -108,7 +107,7 @@ jobs:
108107
- name: Publish live docs
109108
id: publish-live
110109
shell: pwsh
111-
run: ./.github/scripts/docs/Publish-LiveDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -BuildDirectory "$PWD/src/site" -HeadBranch "scribbler/publish/main" -CommitSha "${{ github.sha }}"
110+
run: ./.github/scripts/docs/Publish-LiveDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -BuildDirectory "$PWD/src/site" -CommitSha "${{ github.sha }}"
112111

113112
preview:
114113
name: Preview
@@ -119,12 +118,10 @@ jobs:
119118
name: pr-preview-${{ github.event.number }}
120119
url: ${{ steps.preview-url.outputs.url }}
121120
permissions:
122-
contents: read
121+
contents: write
123122
deployments: write
124123
issues: write
125-
pull-requests: write
126124
env:
127-
PREVIEW_PAGES_BRANCH: scribbler/preview/pr-${{ github.event.number }}
128125
PREVIEW_URL: https://psmodule.io/docs/previews/pr-${{ github.event.number }}/
129126
steps:
130127
- name: Checkout repository
@@ -163,19 +160,17 @@ jobs:
163160
- name: Publish preview docs
164161
id: preview-url
165162
shell: pwsh
166-
run: ./.github/scripts/docs/Publish-PreviewDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -PullRequestNumber ${{ github.event.number }} -PreviewUrl "${{ env.PREVIEW_URL }}" -BuildDirectory "$PWD/src/site" -HeadBranch "${{ env.PREVIEW_PAGES_BRANCH }}"
163+
run: ./.github/scripts/docs/Publish-PreviewDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -PullRequestNumber ${{ github.event.number }} -PreviewUrl "${{ env.PREVIEW_URL }}" -BuildDirectory "$PWD/src/site"
167164

168165
preview-cleanup:
169166
name: Preview cleanup
170167
if: github.event_name == 'pull_request' && github.event.action == 'closed'
171168
runs-on: ubuntu-24.04
172169
permissions:
173-
contents: read
170+
contents: write
174171
deployments: write
175172
issues: write
176-
pull-requests: write
177173
env:
178-
PREVIEW_CLEANUP_BRANCH: scribbler/preview-cleanup/pr-${{ github.event.number }}
179174
PREVIEW_URL: https://psmodule.io/docs/previews/pr-${{ github.event.number }}/
180175
PREVIEW_ENVIRONMENT: pr-preview-${{ github.event.number }}
181176
steps:
@@ -193,4 +188,4 @@ jobs:
193188

194189
- name: Cleanup preview resources
195190
shell: pwsh
196-
run: ./.github/scripts/docs/Cleanup-PreviewDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -PullRequestNumber ${{ github.event.number }} -PreviewUrl "${{ env.PREVIEW_URL }}" -EnvironmentName "${{ env.PREVIEW_ENVIRONMENT }}" -HeadBranch "${{ env.PREVIEW_CLEANUP_BRANCH }}"
191+
run: ./.github/scripts/docs/Cleanup-PreviewDocs.ps1 -Repository "${{ github.repository }}" -Token "${{ steps.app-token.outputs.token }}" -PullRequestNumber ${{ github.event.number }} -PreviewUrl "${{ env.PREVIEW_URL }}" -EnvironmentName "${{ env.PREVIEW_ENVIRONMENT }}"

0 commit comments

Comments
 (0)