Home > Software design >  Is there a way to log error responses from Github Actions?
Is there a way to log error responses from Github Actions?

Time:10-14

I am trying to create a bug tracker that allows me to record the error messages of the python script I run. Here is my YAML file at the moment:

name: Bug Tracker

 #Controls when the workflow will run
on:
  # Triggers the workflow on push request events
  push:
    branches: [ main ]
    
  # Allows you to run this workflow manually from the Actions tab (for testing)
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    # Self Hosted Runner
    runs-on: windows-latest

    # Steps for tracker to get activated
    steps:
      # Checks-out your repository under BugTracker so the job can find it
      - uses: actions/checkout@v2
      - name: setup python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      # Runs main script to look for 
      - name: Run File and collect bug
        id: response
        run:  |
          echo Running File...
          python script.py
          echo "${{steps.response.outputs.result}}"

Every time I run the workflow I can't save the error code. By save the error code, I mean for example... if the python script produces "Process completed with exit code 1." then I can save that to a txt file. I've seen cases where I could save if it runs successfully. I've thought about getting the error in the python script but I don't want to have to add the same code to every file if I don't have to. Any thoughts? Greatly appreciate any help or suggestions.

Update: I have been able to successfully use code in python to save to the txt file. However, I'm still looking to do this in Github if anyone has any suggestions.

CodePudding user response:

You could :

  • redirect the output to a log file while capturing the exit code
  • set an output with the exit code value like:
echo ::set-output name=status::$status
  • in another step, commit the log file
  • in a final step, check that the exit code is success (0) otherwise exit the script with this exit code

Using ubuntu-latest, it would be like this:

name: Bug Tracker

on: [push,workflow_dispatch]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: setup python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Run File and collect logs
        id: run
        run:  |
          echo Running File...
          status=$(python script.py > log.txt 2>&1; echo $?)
          cat log.txt
          echo ::set-output name=status::$status
      - name: Commit log
        run: |
          git config --global user.name 'GitHub Action'
          git config --global user.email '[email protected]'
          git add -A
          git checkout master
          git diff-index --quiet HEAD || git commit -am "deploy workflow logs"
          git push
      - name: Check run status
        if: steps.run.outputs.status != '0'
        run: exit "${{ steps.run.outputs.status }}"

On windows, I think you would need to update this part:

status=$(python script.py > log.txt 2>&1; echo $?)
cat log.txt
  • Related