royB
royB

Reputation: 12977

print the outcome of a step in github actions job

I'm trying to upload an artifact that logs the result of a mvn build. the code will explain better:

jobs:
  job1:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2
    ...
    - name: mvn-build
      continue-on-error: true
      run: |
        mvn package ...
      
      # This doesn't work because on mvn fail - the step is terminated with an error signal > 0
        STATUS=$?
        if [ $STATUS -eq 0 ]; then
        echo 1 > runs/log.txt
        else
        echo 0 > runs/log.txt
        fi

    # This part does create the file (upload-artifact@v1) but the with an empty content
    - name: print-result
      env:
        OUTCOME: ${{ steps.mvn-build.outcome }}
      run: |
        echo "$OUTCOME" > runs/log.txt

Upvotes: 2

Views: 1238

Answers (1)

xandermonkey
xandermonkey

Reputation: 4412

The job terminates because a command exits with a nonzero code. Just don't run that command at top level and you'll be fine!

jobs:
  job1:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2
    ...
    - name: mvn-build
      continue-on-error: true
      run: |
        if  mvn package ... ; then
        echo 1 > runs/log.txt
        else
        echo 0 > runs/log.txt
        fi

    # This part does create the file (upload-artifact@v1) but the with an empty content
    - name: print-result
      env:
        OUTCOME: ${{ steps.mvn-build.outcome }}
      run: |
        echo "$OUTCOME" > runs/log.txt

more information on this bash behavior here: https://unix.stackexchange.com/a/22728/178425

Upvotes: 1

Related Questions