surge-preview icon indicating copy to clipboard operation
surge-preview copied to clipboard

Resolve GitHub Action Permissions -- Error: Resource not accessible by integration

Open zsnmwy opened this issue 2 years ago • 1 comments

Warning: For workflows that are triggered by the pull_request_target event, the GITHUB_TOKEN is granted read/write repository permission unless the permissions key is specified and the workflow can access secrets, even when it is triggered from a fork. Although the workflow runs in the context of the base of the pull request, you should make sure that you do not check out, build, or run untrusted code from the pull request with this event. Additionally, any caches share the same scope as the base branch. To help prevent cache poisoning, you should not save the cache if there is a possibility that the cache contents were altered. For more information, see "Keeping your GitHub Actions and workflows secure: Preventing pwn requests" on the GitHub Security Lab website. pull_request_target

  1. pull_request change to pull_request_target
  2. Change checkout ref and repo
- uses: actions/checkout@v2
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}

Example:

https://github.com/gocrane/crane/blob/main/.github/workflows/preview.yml

name: 🔂 Crane PR Docs Preview

on:
  pull_request_target:
    # when using teardown: 'true', add default event types + closed event type
    types: [opened, synchronize, reopened, closed]

jobs:
  preview:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
      contents: write
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}

      - uses: actions/setup-python@v2
        with:
          python-version: "3.9"

      - run: pip install mkdocs-material mkdocs-static-i18n mike

      - run: git log --oneline --decorate --max-count=10 && ls -la

      - uses: afc163/surge-preview@v1
        with:
          surge_token: ${{ secrets.SURGE_TOKEN }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          dist: site
          teardown: 'true'
          build: |
           mkdocs build

It works fine.

https://github.com/gocrane/crane/pull/248

https://github.com/gocrane/crane/actions/runs/2102551351/workflow

zsnmwy avatar Apr 06 '22 15:04 zsnmwy