Oleksandr Pelykh
Oleksandr Pelykh

Reputation: 79

How to use variable in job conditional rule

I want to run a job on condition: (previous job output returns a string, for example "this is sometext")

- name: some job
  if: contains(${{steps.previous-step.outputs.someoutput}}, 'sometext')
  run: echo 'is executed'

The problem is that this job is always executed so condition is always true even if steps.previous-step.outputs.someoutput does not contain expected text.

But it always works if I use contains function without variable inside, e.g. just contains('some other text', 'sometext')

Upvotes: 2

Views: 1391

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 22970

You don't need the ${{ }} when using the contains function on a if expression.

The correct syntax is:

  job1:
      runs-on: ubuntu-latest
      steps:
          - id: step1
            run: echo "::set-output name=test::hello"
          - name: step2
            if: contains(steps.step1.outputs.test, 'hello')
            run: echo 'is executed'
          - name: step3
            if: contains(steps.step1.outputs.test, 'not_hello')
            run: echo 'not executed'

Here, step2 will be executed, but not step3.

I made an example in this workflow run (job7) if you want to check.

Upvotes: 2

Related Questions