Skip to content

Commit 3e009b8

Browse files
[github-actions] Reduce workflow token permissions (#63) (#89)
* Reduce workflow token permissions * Update wiki submodule pointer for PR #89 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent bc15d35 commit 3e009b8

8 files changed

Lines changed: 65 additions & 13 deletions

File tree

.github/wiki

Submodule wiki updated from fd4afe1 to 8852048

.github/workflows/label-sync.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414

1515
permissions:
1616
contents: read
17+
issues: read
1718
pull-requests: write
1819

1920
jobs:

.github/workflows/reports.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ on:
2323
branches: [ "main" ]
2424

2525
permissions:
26-
contents: write
27-
pull-requests: write
26+
contents: read
2827

2928
concurrency:
3029
group: ${{ github.event_name == 'pull_request' && format('reports-preview-pr-{0}', github.event.pull_request.number) || 'reports-pages' }}
@@ -35,6 +34,8 @@ jobs:
3534
if: github.event_name != 'schedule' && !(github.event_name == 'workflow_dispatch' && inputs.cleanup-previews) && (github.event_name != 'pull_request' || github.event.action != 'closed')
3635
name: Generate Reports
3736
runs-on: ubuntu-latest
37+
permissions:
38+
contents: write
3839

3940
env:
4041
PAGES_ARTIFACT_NAME: ${{ github.event_name == 'pull_request' && format('github-pages-pr-{0}', github.event.pull_request.number) || 'github-pages' }}
@@ -120,8 +121,16 @@ jobs:
120121
keep_files: false
121122
force_orphan: false
122123

124+
comment_preview:
125+
if: github.event_name == 'pull_request' && github.event.action != 'closed'
126+
name: Comment Pull Request Preview URLs
127+
needs: reports
128+
runs-on: ubuntu-latest
129+
permissions:
130+
pull-requests: write
131+
132+
steps:
123133
- name: Comment preview URLs on pull request
124-
if: github.event_name == 'pull_request'
125134
uses: marocchino/sticky-pull-request-comment@v2
126135
with:
127136
header: pr-preview
@@ -135,6 +144,8 @@ jobs:
135144
if: github.event_name == 'pull_request' && github.event.action == 'closed'
136145
name: Cleanup Pull Request Preview
137146
runs-on: ubuntu-latest
147+
permissions:
148+
contents: write
138149

139150
steps:
140151
- name: Checkout gh-pages
@@ -162,6 +173,9 @@ jobs:
162173
if: github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.cleanup-previews)
163174
name: Cleanup Orphaned Pull Request Previews
164175
runs-on: ubuntu-latest
176+
permissions:
177+
contents: write
178+
pull-requests: read
165179

166180
steps:
167181
- name: Checkout gh-pages

.github/workflows/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ on:
2626
branches: [ "main" ]
2727

2828
permissions:
29-
contents: write
30-
pages: write
31-
id-token: write
29+
contents: read
3230

3331
concurrency:
3432
group: "pages"

.github/workflows/wiki.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ on:
1111
types: [closed]
1212

1313
permissions:
14-
contents: write
15-
pull-requests: read
14+
contents: read
1615

1716
concurrency:
1817
group: update-wiki-${{ github.event.pull_request.number || github.ref }}
@@ -23,6 +22,9 @@ jobs:
2322
name: Update Wiki Preview
2423
if: github.event_name == 'pull_request'
2524
runs-on: ubuntu-latest
25+
permissions:
26+
contents: write
27+
pull-requests: read
2628

2729
env:
2830
WIKI_PREVIEW_BRANCH: pr-${{ github.event.pull_request.number }}
@@ -117,6 +119,9 @@ jobs:
117119
name: Publish Wiki Master
118120
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main'
119121
runs-on: ubuntu-latest
122+
permissions:
123+
contents: write
124+
pull-requests: read
120125

121126
env:
122127
WIKI_PUBLISH_BRANCH: master
@@ -186,6 +191,8 @@ jobs:
186191
name: Delete Closed PR Wiki Preview
187192
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == false
188193
runs-on: ubuntu-latest
194+
permissions:
195+
contents: write
189196

190197
env:
191198
WIKI_PREVIEW_BRANCH: pr-${{ github.event.pull_request.number }}
@@ -215,6 +222,9 @@ jobs:
215222
name: Delete Orphaned Wiki Previews
216223
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
217224
runs-on: ubuntu-latest
225+
permissions:
226+
contents: write
227+
pull-requests: read
218228

219229
steps:
220230
- name: Checkout main branch

docs/advanced/branch-protection-and-bot-commits.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ write generated preview commits, update pull request comments, and publish Pages
9292
content. Keep those permissions scoped to the workflow jobs that actually need
9393
them.
9494

95+
Workflow Permission Scope
96+
-------------------------
97+
98+
The reusable workflows default to read-only repository access and grant write
99+
permissions at the job level when generated content must be pushed or pull
100+
requests must be updated.
101+
102+
``tests.yml`` only needs ``contents: read`` because it checks out code, installs
103+
dependencies, and runs PHPUnit.
104+
105+
``reports.yml`` keeps ``contents: write`` on jobs that publish or clean
106+
``gh-pages`` content. The pull request preview comment runs as a separate job
107+
with ``pull-requests: write`` because it posts or updates the sticky preview
108+
comment. Scheduled preview cleanup uses ``pull-requests: read`` so it can
109+
distinguish open pull requests from closed or merged ones before deleting
110+
``previews/pr-<number>`` directories.
111+
112+
``wiki.yml`` keeps ``contents: write`` on preview, publish, and cleanup jobs
113+
because the workflow pushes wiki branches, updates the parent repository
114+
submodule pointer, promotes preview content to wiki ``master``, and deletes
115+
stale preview branches. Jobs that inspect pull request state keep
116+
``pull-requests: read``.
117+
118+
The label synchronization workflow declares ``issues: read`` to copy labels from
119+
the linked issue and ``pull-requests: write`` to apply those labels to the pull
120+
request. The auto-assign workflow keeps ``issues: write`` and
121+
``pull-requests: write`` because assignment is a write operation on both event
122+
types.
123+
95124
Resolving ``.github/wiki`` Pointer Conflicts
96125
--------------------------------------------
97126

resources/github-actions/label-sync.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ on:
99
jobs:
1010
label-sync:
1111
permissions:
12+
contents: read
13+
issues: read
1214
pull-requests: write
13-
uses: php-fast-forward/dev-tools/.github/workflows/label-sync.yml@main
15+
uses: php-fast-forward/dev-tools/.github/workflows/label-sync.yml@main

resources/github-actions/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ on:
55
workflow_dispatch:
66

77
permissions:
8-
contents: write
9-
pages: write
10-
id-token: write
8+
contents: read
119

1210
jobs:
1311
tests:

0 commit comments

Comments
 (0)