【问题标题】:User input in github actions (specify repo branch, etc)github 操作中的用户输入(指定 repo 分支等)
【发布时间】:2020-04-13 05:24:40
【问题描述】:

我想创建一个 github 操作来创建集成测试环境。我们已经有一个可以执行此操作的 dockerized 脚本,但是,该环境由 2+ 个存储库组成。因此,为了在开发过程中有效,我们需要指定其他 repos 的分支。

例如,假设我在 repo 中有一个 PR:frontend,branch:my-feature-brach。它需要 repo: backend, branch: their-feature-branch。我想从我的 PR 开始构建,它使用该 PR 的分支(在前端 repo 中),并询问我将哪个分支用于后端 repo。

这可能吗?

【问题讨论】:

  • Github 操作以批处理模式在远程机器上运行,所以我看不出它们如何交互。话虽如此,应该很容易通过修改和签入来控制动作的作用。

标签: github github-actions


【解决方案1】:

您可以使用输入定义手动可执行的工作流。

on: 
  workflow_dispatch:
    inputs:
      environment:
        description: 'Define env name'     
        required: true
        default: 'prod'
      branch:
        description: 'Define branch name'     
        required: true
        default: 'master'

您可以使用这些预定义的参数,例如:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Env: ${{ github.event.inputs.environment }}" 
        echo "Branch: ${{ github.event.inputs.branch }}"

我认为您可以用它来支持您的用例。 更多详情here.

【讨论】:

    【解决方案2】:

    您可以使用斜杠命令样式的“ChatOps”解决方案。操作 slash-command-dispatch 可以帮助您使用斜杠命令(例如 /build)从 issue 和 pull request cmets 触发工作流。

    这是拉取请求 cmets 中 build 斜杠命令的基本示例。 REPO_ACCESS_TOKENrepo 作用域Personal Access Token

    name: Slash Command Dispatch
    on:
      issue_comment:
        types: [created]
    jobs:
      slashCommandDispatch:
        runs-on: ubuntu-latest
        steps:
          - name: Slash Command Dispatch
            uses: peter-evans/slash-command-dispatch@v2
            with:
              token: ${{ secrets.REPO_ACCESS_TOKEN }}
              commands: build
              issue-type: pull-request
    

    可以在此工作流程中处理命令。

    name: Build Command
    on:
      repository_dispatch:
        types: [build-command]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          # Checkout the pull request branch
          - uses: actions/checkout@v2
            with:
              repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
              ref: ${{ github.event.client_payload.pull_request.head.ref }}
              path: ${{ github.event.repository.name }}
              token: ${{ secrets.REPO_ACCESS_TOKEN }}
    

    如果您将参数传递给斜杠命令,它们将与有效负载一起传递。例如,分支名称。

    /build ref=their-feature-branch
    

    然后在工作流程中,您可以检查通过参数 ref 传递的分支。

          - uses: actions/checkout@v2
            with:
              repository: backend-repo
              ref: ${{ github.event.client_payload.slash_command.args.named.ref }}
              path: backend-repo
              token: ${{ secrets.REPO_ACCESS_TOKEN }}
    

    这只是简要介绍您可以使用slash-command-dispatch 操作执行的操作。请查看存储库以获取完整详细信息。

    【讨论】:

      【解决方案3】:

      GitHub Actions 可以使用 GitHub API 与 PR 交互。

      这意味着,您的脚本可以在 repo 上创建一个评论,如果它提到分支(你决定格式),它会从分支开始测试。

      另一种可能性是要求 PR 的指定格式(例如,它需要 branch:<branch> 在评论中)。该操作从 PR 描述中提取分支并使用该分支。

      请参阅 the GitHub API docs for PRsGitHub actions docs for the github context(有关 PR 的信息) 供参考。

      【讨论】:

        猜你喜欢
        • 2020-02-17
        • 2021-12-29
        • 2022-08-15
        • 2020-10-01
        • 2020-01-08
        • 2020-03-06
        • 2022-06-20
        • 2021-06-04
        • 1970-01-01
        相关资源
        最近更新 更多