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

Viewed 847

I have the following Github Action workflow that is intended to read our lines of code coverage from a coverage.txt file and print the coverage as a comment.

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

Where I am struggling is on taking the output of the file, which I can obtain with bash using awk '/^Lines/ { print $2 }' < coverage.txt (not sure if there is a better alternative way) and inserting it into the message, which currently just says hello.

I found this article on yaml variables, but when I added some of that code to my own file, it just was not recognized any longer by GitHub actions (which is the only way I know to test yaml). Normally it would fail somewhere and give me an error, but after multiple attempts, the only thing that worked was to remove it.

It is quite possible that I am missing something obvious as I do not know yaml very well nor even what certain key words might be.

Alternatively, is it easier to just dump the contents of the file into the message? That could be acceptable as well.

1 Answers

You can create a step that gets the coverage to an output variable that then can be accessed by the next step.

Notice that for utilizing this method you will need to give the step and id and the set output variable a variable name so that you can access it in follow up steps within the same job.

Sample with your workflow below, but if you want to see a running demo here is the repo 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 $2 }' < 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

I make a lot of github actions tutorials here if you're looking to grasp more on this topic. Cheers!!

Related