Github action on push if-and-only-if not an active PR

Viewed 27

I have a github repo with a GitHub Action CI Workflow that runs jobs pytest and job2 on pull requests, and both jobs must pass.

I also want another workflow to run that same pytest on every push (call this pytest_on_push), so users are warned if commits break a branch before they make a PR.

However, I do not want pytest_on_push to run on pushes to a branch AFTER the branch has an open PR, because it unnecessarily duplicates the pytest job.

How can this be done?

I've tried without success:

  • preventing jobs or steps in pytest_on_push via if: github.event_name != 'pull_request'
  • Looking for some syntax to define on: push if not pull_request

Example files:

name: CI Workflow
on:
  pull_request:
    types: [ opened, synchronize, reopened ]

jobs:
  pytest:
    name: pytest
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: Run Pytest
        run: make test-ci

      - name: Archive unit test reports
        uses: actions/upload-artifact@v1
        with:
          name: coverage.xml
          path: ./coverage.xml

  job2:
    name: job2 for POR
    needs: pytest
    runs-on: ubuntu-latest
    
    steps:
      {do stuff}
name: pytest_on_push
on:
  # Apply pytest on push to any branch expect dev & prod
  # (these should be tested by ci.yml operating on any PR to those branches)
  # Can condition be defined to ignore if pull_request? 
  push:
    branches-ignore:
      - develop
      - production

jobs:
  pytest:
    name: pytest
    runs-on: ubuntu-latest
    # Can an if statement here prevent job steps running?

    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Run Pytest
        run: make test-ci
0 Answers
Related