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


    runs-on: ubuntu-latest

      - uses: actions/checkout@v2

      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
          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
          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.

  • It is possible to run multiple bash commands in github actions. It is also possible to put your commands in a script and to run that as well in the pipeline. Could you add the error message and indicate whether your command runs locally?
    – 030
    Aug 29 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.