Github 操作如何访问输入

Pau*_*aul 5 yaml action github github-actions

在此输入图像描述

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'
Run Code Online (Sandbox Code Playgroud)

我有以下输入:

  • 我想使用哪个分支
  • 输入字段

如何访问我输入的信息,然后在 github 操作脚本中使用它?

Von*_*onC 18

您现在(2022 年 6 月)有一个替代方案:

您可以尝试利用新功能(2022 年 6 月)

GitHub Actions:跨手动和可重用工作流程的统一输入

workflow_dispatch由上下文触发的工作流workflow_call现在可以使用上下文访问其输入inputs

以前的workflow_dispatch输入位于event有效负载中。
这使得工作流程作者很难拥有一个既可重用又可手动触发的工作流程。

现在,工作流作者可以编写由workflow_dispatch和触发的单个工作流workflow_call,并使用inputs上下文来访问输入值。

对于由 触发的工作流workflow_dispatch,输入在上下文中仍然可用github.event.inputs以保持兼容性。

使用 GitHub Actions 中的输入上下文

在你的情况下:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ inputs.logLevel }}"
        echo "Tags: ${{ inputs.tags }}" 
Run Code Online (Sandbox Code Playgroud)

是否可以避免打印日志中的输入?

为了避免在 GitHub Actions 日志中打印输入,您仍然可以访问输入,但以防止它们直接输出到日志的方式操作它们。

例如,您可以将输入设置为环境变量,然后在脚本中使用它们。环境变量不会自动打印在日志中。

GitHub Actions 具有屏蔽日志中值的功能。您可以使用该::add-mask::命令来屏蔽一个值。***一旦某个值被屏蔽,它将在日志中被替换。

jobs:
  processInputs:
    runs-on: ubuntu-latest
    steps:
      - name: Set up environment variables
        run: |
          echo "LOG_LEVEL=${{ inputs.logLevel }}" >> $GITHUB_ENV
          echo "TAGS=${{ inputs.tags }}" >> $GITHUB_ENV

      - name: Run script with inputs
        run: |
          ./your-script.sh
        env:
          LOG_LEVEL: ${{ secrets.LOG_LEVEL }}
          TAGS: ${{ secrets.TAGS }}
Run Code Online (Sandbox Code Playgroud)

在这里,输入被设置为环境变量。然后将这些变量传递给脚本 ( your-script.sh),该脚本可以在内部使用这些值。
如有必要,您可以使用 屏蔽敏感输入::add-mask::

另一种选择:您可以将输入传递给脚本并在脚本内处理它们。这样,实际值就不会直接打印在日志中。


Gui*_*urd 5

可以在这里找到更新的答案


要在 yaml 文件上使用触发器的输入workflow_dispatch,您需要使用以下语法${{ github.event.inputs.<input_name> }}${{ inputs.<input_name> }}.

例如,在你的情况下:

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ github.event.inputs.logLevel }}"
        echo "Tags: ${{ github.event.inputs.tags }}" 
Run Code Online (Sandbox Code Playgroud)

如果您想检查的话,这里有一个简单的演示:

您还可以查看有关它的官方文档