How can I submit the output of a step in GitHub Actions as a comment?

171 Views Asked by At

I'm trying to configure lighthouse CI with GitHub Actions for the first time.

My current configuration looks like this:

name: Pull Request Checks

on:
  pull_request:

  lighthouseci:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout Github Repository'
        uses: actions/checkout@v3

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

      - name: 'build'
        run: |
          npm install
          npm run build

      - name: 'run lighthouse'
        id: lighthouse
        run: |
          npm install -g @lhci/[email protected]
          lhci autorun

      - name: Output lighthouse results
        run: |
          echo "Performance: ${{ steps.lighthouse.outputs }}" >> $GITHUB_STEP_SUMMARY

Everything up until Output lighthouse results works.
The goal for Output lighthouse results is to post a comment in the PR with an overview of the results from my lighthouse test. This part doesn't work and I am struggling to understand how this is even supposed to work.

I tried checking the docs:

and went digging into the source code of https://github.com/treosh/lighthouse-ci-action/tree/main

I also looked at some other examples of actions with outputs and some of them do something like this:

echo "lhci=$(lhci autorun)\n" >> $GITHUB_OUTPUT

But I get this error message:

Error: Unable to process file command 'output' successfully.

Presumably because the output is not in a structured format?

This answered some questions but left some open:

  1. steps.lighthouse.outputs is some object, but I don't know how I can figure out what that object looks like at all. And the only reason I found this key in the first place is because I stole it from some blog post. Is there any documentation here I am missing?
  2. The build output is nice but I'd like to see it as a comment under the PR. What approach should I be taking for that?

Even just some of the terminology that I need to google would be really helpful as this is a completely new area for me.

1

There are 1 best solutions below

0
On

Since you are using this value within the same job, you should use $GITHUB_ENV instead of $GITHUB_OUTPUT. Heres something you can try:

 - name: 'run lighthouse'
   run: |
     npm install -g @lhci/[email protected]
     echo "lighthouse_output=$(lhci autorun)" >> $GITHUB_ENV

  - name: Output lighthouse results
    run: |
      echo "Performance: ${{ env.lighthouse_output }}" >> $GITHUB_STEP_SUMMARY