Condition if in GHA need

559 Views Asked by At

I have following jobs:

jobs:
 build:
 name: Build
 runs-on: [ self-hosted, linux ]
 steps:
 - uses: actions/checkout@v2
 - name: Build
   run: dotnet build

unit-test:
 name: Unit test
 if: github.event_name != 'release'
 runs-on: [ self-hosted, linux ]
 needs: build
 steps:
 - uses: actions/checkout@v2
 - name: Run tests
   run: dotnet test

publish:
 name: Publish artifacts.zip
 runs-on: [ self-hosted, linux ]
 needs: unit-test
 steps:
 - uses: actions/checkout@v2
 - run: dotnet publish
 - name: Create artifact
   run: |
     mkdir -p ./code

I don't want to run unit tests on release, but I still need publish to run after build and I cant figure out how to do that.

I want to change publish job to: if github.event_name != 'release' then needs: build otherwise needs: unit-test as it is now. How conditions can be defined in such case?

1

There are 1 best solutions below

0
On

I think you could achieve what you want by setting the conditionals at the step level instead of at the job level. That way, GHA would consider the job as run, even though no step has been run. If it hadn't considered the job as run due to the fact that no step has been run, you could add a dummy step (eg. - run: echo "Done!") that would be always run.

jobs:
 build:
 name: Build
 runs-on: [ self-hosted, linux ]
 steps:
 - uses: actions/checkout@v2
 - name: Build
   run: dotnet build

unit-test:
 name: Unit test
 runs-on: [ self-hosted, linux ]
 needs: build
 steps:
 - uses: actions/checkout@v2
   if: github.event_name != 'release'
 - name: Run tests
   run: dotnet test
   if: github.event_name != 'release'

publish:
 name: Publish artifacts.zip
 runs-on: [ self-hosted, linux ]
 needs: unit-test
 steps:
 - uses: actions/checkout@v2
 - run: dotnet publish
 - name: Create artifact
   run: |
     mkdir -p ./code