Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a GitHub Actions step only if previous step has run

I've set up a workflow in GitHub actions to run my tests and create an artifact of the test coverage. The stripped down version of my YAML file looks like this:

name: Build

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Other steps here

      - name: Build app
      - name: Run tests
      - name: Create artifact of test coverage

      # Other steps here

The problem is that the artifact does not get created when the tests fail.

I figured out about if: always() condition the from the docs, but this will also cause this step to run when my Build app step fails. I don't want that to happen because there is nothing to archive in that case.

How can I only run this step if the previous step has run (either succeeded or failed)?

like image 678
Duncan Luk Avatar asked Feb 28 '20 14:02

Duncan Luk


People also ask

Do GitHub Actions jobs run in parallel?

You can configure a GitHub Actions workflow to be triggered when an event occurs in your repository, such as a pull request being opened or an issue being created. Your workflow contains one or more jobs which can run in sequential order or in parallel.

How long can a GitHub action last?

Job execution time - Each job in a workflow can run for up to 6 hours of execution time. If a job reaches this limit, the job is terminated and fails to complete. Workflow run time - Each workflow run is limited to 35 days. If a workflow run reaches this limit, the workflow run is cancelled.


1 Answers

Try checking success() OR failure().

name: Build

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Other steps here

      - name: Build app
      - name: Run tests
      - name: Create artifact of test coverage
        if: success() || failure()

      # Other steps here

Alternatively, create a step output of the exit code that you can check in later steps. For example:

      - name: Build app
        id: build
        run: |
          <build command>
          echo ::set-output name=exit_code::$?

      - name: Run tests

      - name: Create artifact of test coverage
        if: steps.build.outputs.exit_code == 0
like image 82
peterevans Avatar answered Sep 28 '22 08:09

peterevans