github worflow fails when running a grep command that does not found anything

Viewed 13

I'm working on a workflow that has the following step:

  - name: Analyze blabla
    run: grep -Ri --include \*.ts 'stringToBeSearched' ./tmp/bla > ./tmp/results.txt
    shell: bash

This works well in the case the grep command founds something. Then the found lines are dumped into results.txt and the returncode is 1, and the workflow goes to the next step as expected

But in the case the grep command does not found the searched strings, then an empty file is saved as result.txt (what correct until this point), but the result code is 0, and the step is set as failed, and the whole workflow fails.

Is there a way to not set the step as failed when the result code is 0?

Thanks

1 Answers

You could use the continue-on-error step option:

jobs.<job_id>.steps[*].continue-on-error

Prevents a job from failing when a step fails. Set to true to allow a job to pass when this step fails

Like:

  - name: Analyze blabla
    continue-on-error: true
    id: grep
    run: grep -Ri --include \*.ts 'stringToBeSearched' ./tmp/bla > ./tmp/results.txt
    shell: bash

You. could check the outcome of the step in order to understand if was failed or not like:

steps.<id>.outcome != 'success'

See outcome doc here

Related