如何使用 Github Actions 获取拉取请求的标题

ste*_*238 30 yaml github github-actions

在 GitHub 中,我有一个名为 的拉取请求[WIP] Dev-123 Sample Pull Request

我想在 GitHub Actions yaml 管道中获取此标题。

GitHub Docs Context中,我似乎找不到需要引用的对象。

ste*_*238 41

拉取请求的标题可以通过以下方式访问github.event.pull_request.title

使用它的工作流程是:

on:
  push:
  pull_request:
   types: [opened, synchronize]

  print_title_of_pr:
    runs-on: ubuntu-20.04
    steps:
    - name : Print Title of PR
      run: echo The Title of your PR is ${{ github.event.pull_request.title }}
Run Code Online (Sandbox Code Playgroud)

  • 就是这样。您还可以获取其他属性,例如使用“github.event.pull_request.body”获取“body”。请在此处查看 API 响应中的完整属性列表:https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request (3认同)

Yar*_*tal 11

上面接受的答案使用起来不安全,并且会使工作流程遭受关键的命令注入攻击。用户控制的输入在使用前应进行消毒。您可以通过添加中间环境变量来缓解这种情况

参考: https ://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions

on:
  push:
  pull_request:
   types: [opened, synchronize]

  print_title_of_pr:
    runs-on: ubuntu-20.04
    steps:
    - name : Print Title of PR
      env:
          TITLE: ${{ github.event.pull_request.title }}
      run: echo The Title of your PR is $TITLE
Run Code Online (Sandbox Code Playgroud)