【问题标题】:Running GitHub Actions on the code in a Pull Request from a fork对来自 fork 的 Pull Request 中的代码运行 GitHub Actions
【发布时间】:2021-02-24 08:44:10
【问题描述】:

我在 GitHub 中有一个原始存储库,并且我创建了一个正在开发的本地分支。我有一个运行 Bandit 安全检查的 GitHub Action,但是当我推送更改并从我的 fork 中的分支创建 Pull 请求时,Bandit 测试在当前位于原始存储库中的代码上运行,而不是在新代码上公关。

如何在拉取请求中的代码上运行 GitHub 操作工作流?

仅供参考:这是 yml 文件中当前的“on”语句:

name: Security scan
on:
  push:
    branches:
      - master
  pull_request_target:
    branches: [main, dev]

【问题讨论】:

  • 这是您完整的 github 操作 worfklows yaml 文件吗?我认为steps 部分丢失了。

标签: git github fork github-actions pull-request


【解决方案1】:

我使用this blog post 为我的一个存储库设置 github 操作。检查一下,它可能会有所帮助。也阅读documentation from github 可能会有所帮助。

关于其他人也提到的您的具体问题,您应该使用pull_request。这是一个示例(此 CI 作业仅在将请求拉到主分支时触发):

name: Tests                                                                                 
on:
  pull_request:
    branches:
    - main                                                                                  
jobs:                                                                                       
  tests:                                                                                    
    runs-on: ubuntu-latest                                                                  
    steps:                                                                                  
    - uses: actions/checkout@v2                                                             
    - uses: actions/setup-python@v1                                                         
      with:                                                                                 
        python-version: 3.8                                                                 
        architecture: x64                                                                   
    - run: # security scan bandit

【讨论】:

    【解决方案2】:

    [...] 我们添加了一个新的 pull_request_target 事件,它的行为方式与 pull_request 事件几乎相同,具有相同的过滤器和有效负载集。但是,事件不是针对来自合并提交的工作流和代码运行,而是针对来自拉取请求基础的工作流和代码运行。

    来源:https://github.blog/2020-08-03-github-actions-improvements-for-fork-and-pull-request-workflows/

    您仍然可以通过以下步骤从拉取请求中签出代码:

    - uses: actions/checkout@v2
      with:
        ref: ${{github.event.pull_request.head.ref}}
        repository: ${{github.event.pull_request.head.repo.full_name}}
    

    来源:https://github.community/t/running-code-from-forks-with-pull-request-target/126688/6

    【讨论】:

      【解决方案3】:

      您应该改用pull_request 触发器。如果您的 PR 工作流程需要 secrets,更多有目的和安全使用 pull_request_target 在这里 - https://securitylab.github.com/research/github-actions-preventing-pwn-requests

      【讨论】:

        【解决方案4】:

        触发推送或拉取请求事件的工作流

        使用pull_request

        name: CI
        on: [push, pull_request]
        jobs:
          python-tests:
            runs-on: ubuntu-latest
        
            steps:
              ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-24
          • 2021-02-26
          • 2013-02-05
          • 1970-01-01
          • 2017-04-08
          • 1970-01-01
          • 2020-02-03
          • 1970-01-01
          相关资源
          最近更新 更多