获取 bash 命令的输出并将其放入用于 GitHub 操作的 yaml 消息中?

Taking a bash command's output and putting it into a message in yaml for GitHub actions?

我有以下 Github 操作工作流程,旨在从 coverage.txt 文件中读取我们的代码覆盖行并将覆盖范围打印为注释。

name: Report Code Coverage

on:
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test:coverage

      ## I need help around here, as test:coverage generates a file and I need to get a value from a file

      - uses: mshick/add-pr-comment@v1
        with:
          message: |
            **{{Where I want the code coverage value to go}}**
            
            !
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          repo-token-user-login: 'github-actions[bot]' # The user.login for temporary GitHub tokens
          allow-repeats: false # This is the default

我挣扎的地方是获取文件的输出,我可以使用 awk '/^Lines/ { print }' < coverage.txt 通过 bash 获得(不确定是否有更好的替代方法)并将其插入到消息,目前只是打招呼。

我找到了这个 article on yaml variables,但是当我将其中的一些代码添加到我自己的文件时,GitHub 操作不再识别它(这是我知道的唯一方法测试 yaml)。通常它会在某处失败并给我一个错误,但经过多次尝试,唯一有效的方法就是将其删除。

很可能我遗漏了一些明显的东西,因为我不太了解 yaml,甚至不知道某些关键字可能是什么。

或者,直接将文件内容转储到邮件中是否更容易?这也可以接受。

您可以创建一个步骤来获取输出变量的覆盖率,然后下一步可以访问该输出变量。

请注意,要使用此方法,您需要为步骤和 ID 以及设置的输出变量指定一个变量名,以便您可以在同一作业的后续步骤中访问它。

下面是您的工作流程示例,但如果您想查看 运行 演示,这里是存储库 https://github.com/meroware/demo-coverage-set-output

  - name: Run tests
    run: npm run test:coverage
  - name: Get coverage output
    id: get_coverage
    run: echo "::set-output name=coverage::$(awk '/^Lines/ { print  }' < test.txt)"
  - uses: mshick/add-pr-comment@v1
    with:
      message: |
        Coverage found ${{steps.get_coverage.outputs.coverage}}
        
        !
      repo-token: ${{ secrets.GITHUB_TOKEN }}
      repo-token-user-login: 'github-actions[bot]' # The user.login for temporary GitHub tokens
      allow-repeats: false # This is the default

如果您想了解有关此主题的更多信息,我会做很多 github actions tutorials here。干杯!!