baruchiro
baruchiro

Reputation: 5801

Github Actions Functions on the console output

Say I want to validate that my code doesn't contain the foo string. So I'm using the Linux grep tool and count the output lines with wc -l.

Now I want to run a step only if the results of the grep | wc -l eqauls to 0.

How can I achieve that?

Upvotes: 3

Views: 12040

Answers (2)

smac89
smac89

Reputation: 43098

You just have to make sure that the command that runs grep exits unsuccessfully and then in the next step use if: success().

So the grep task can use something like: [ $(grep 'foo' action.ts | wc -l) = 0 ]. If the output is not zero, the command will exit with non-zero status.

In the dependent task, use if: success(), and this task will only run if the previous task success successful.

Upvotes: 1

Samira
Samira

Reputation: 9731

That should do the trick.

job:
 steps:
 - run:   echo ::set-env name=GHA_COUNTER::$(grep "thing" code.h | wc -l)
   shell: bash
 - run: echo "There's a thing in my code!"
   if:  env.GHA_COUNTER == '0'

::set-env pushes result of grep | wcto env context, which is what if needs to work.

If you don't want to pollute environment for any reason, ::set-output is another option.

job:
 steps:
 - run:   echo ::set-output name=things::$(grep "thing" code.h | wc -l)
   id:    counter
   shell: bash 
 - run: echo "There's a thing in my code!"
   if:  steps.counter.outputs.things == '0'

Upvotes: 2

Related Questions