githubyamlgithub-actionspep8github-ci

github-actions: a multi-line output from a previous step with 'uses' in a next step of a workflow


I do not understand, how to collect all of the output from previous step that implemented as uses to public it as a PR comment in the next step of the workflow.

There are many answers about collecting a multi-line output using variables, $GITHUB_OUTPUT, &GITHUB_STEP_SUMMARY, echo <<EOF etc, but for that the own script need to be used.

My script is:

name: check-pep8
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  check-pep8:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      checks: write
      pull-requests: write

    steps:
      - uses: actions/checkout@v4

      - name: Run PEP-8 check
        id: pep8-action
        continue-on-error: true
        uses: quentinguidee/pep8-action@v1
        with:
          arguments: '--max-line-length=120'

      - name: Comment PR
        if: steps.pep8-action.status != 'success'
        uses: thollander/actions-comment-pull-request@v3
        with:
          message: |
            ${{ join(steps.pep8-action.outputs.output, ' ') }}

The full output of the step pep8-action is

./corpora_deduplication.py:42:15: E701 multiple statements on one line (colon)
./corpora_deduplication.py:47:41: E222 multiple spaces after operator
./corpora_deduplication.py:47:41: E251 unexpected spaces around keyword / parameter equals
./corpora_deduplication.py:47:121: E501 line too long (145 > 120 characters)
./corpora_deduplication.py:48:81: E702 multiple statements on one line (semicolon)
./corpora_deduplication.py:48:121: E501 line too long (163 > 120 characters)
./corpora_deduplication.py:61:3: E271 multiple spaces after keyword
./corpora_deduplication.py:61:13: E225 missing whitespace around operator
Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
./corpora_deduplication.py:47:41: E222 multiple spaces after operator
./corpora_deduplication.py:47:41: E251 unexpected spaces around keyword / parameter equals
./corpora_deduplication.py:47:121: E501 line too long (145 > 120 characters)
./corpora_deduplication.py:48:81: E702 multiple statements on one line (semicolon)
./corpora_deduplication.py:48:121: E501 line too long (163 > 120 characters)
./corpora_deduplication.py:61:3: E271 multiple spaces after keyword
./corpora_deduplication.py:61:13: E225 missing whitespace around operator

I see the only first line in the steps.pep8-action.outputs.output:

{
"exit-code": "1",
"output": "./corpora_deduplication.py:42:15: E701 multiple statements on one line (colon)"
}

Is there any work-around for that?


Solution

  • Thanks to Benjamin W. for his notice.

    I went the other way and did everything according to the documentation.

    # Install Python dependencies, run tests and lint Python code
    # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
    name: Python package
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
    jobs:
      checks:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          checks: write
          pull-requests: write
    
        steps:
        - name: Checkout repo
          uses: actions/checkout@v4
    
        - name: Set up Python
          uses: actions/setup-python@v3
          with:
            # Semantic version range syntax or exact version of a Python version
            python-version: '3.x'
            # Optional - x64 or x86 architecture, defaults to x64
            architecture: 'x64'
            # Cache all dependencies
            cache: 'pip'
    
        - name: Install dependencies
          run: |
            python -m  pip install --upgrade pip
            python -m  pip install flake8 pytest
            if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
        
        - name: Lint with flake8
          continue-on-error: true
          id: pep8-action
          run: |
            {
              # Keep the multi-line output
              echo 'output<<EOF'
              # stop the build if there are Python syntax errors or undefined names
              flake8 . --count --select=E9,F63,F7,F82 --show-source --exit-zero
              echo ---
              # exit-zero treats all errors as warnings
              flake8 . --count --max-complexity=10 --max-line-length=120 --exit-zero
              echo EOF
            } >> "$GITHUB_OUTPUT"
    
        - name: Comment PR
          if: ${{ contains(steps.pep8-action.outputs.output, 'py') }}
          uses: thollander/actions-comment-pull-request@v3
          with:
            message: |
              ${{ join(steps.pep8-action.outputs.output, ' ') }}
    
        - name: Test with pytest
          run: |
            pytest
    
        - name: Lint check is failed
          if: ${{ contains(steps.pep8-action.outputs.output, 'py') }}
          run: exit 1