2

I need github actions to run only on the QAS branch and deploy event. It should run on 'pull_request' and 'pull' and only on the QAS branch.

name: Cypress

on: [deployment_status]

jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest

    steps:
      - name: Print URL
        run: echo Testing URL ${{ github.event.deployment_status.target_url }}

      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2-beta
        with:
          node-version: 14

      - name: Install modules
        run: yarn

      - name: Run Cypress
        uses: cypress-io/github-action@v2

But I wanted something like this:

name: Cypress
    
    on:
      deployment_status:
        pull_request:
          branches:
            - qas
        push:
          branches:
            - qas
    
    jobs:...
Marcos.C
  • 31
  • 2

1 Answers1

1

It's currently not possible to achieve what you want using triggers conditions alone. This is due to the fact that those conditions are configured to work as OR and not as AND.

It that case, a workaround is to use one trigger condition - for example the on: [deployment_status] you are currently using - then add a filter at the job level to check the branch name based on the github.ref from the Github Context.

In your case, I guess it would look like this:

name: Cypress

on: [deployment_status]

jobs:
  e2e:
    if: ${{ github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/qas' }}
    runs-on: ubuntu-latest

    steps:
      [...]

Note: Let me know if the job if condition is working as expected (I don't have a poc repo with a webhook configured to try it with the github.event.deployment_status.state as well).

Note 2: It may not be necessary to use the ${{ }} around the condition:

if: github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/qas'
GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • This way it doesn't work in case I'm doing a `pull_request` from a branch in QAS or a `push` on that branch in `pull_request`. In the case of a `pull_request` or a `push` in this `pull_request` on a branch it is not QAS but the name of the branch I created for `pull_request`, am I right? – Marcos.C Aug 26 '21 at 21:28
  • Yes, you're correct. However, you can always add other conditions to the `if` based on each situation to check if it match your context. It's not always elegant, but it does the job (it's a workaround). – GuiFalourd Aug 27 '21 at 00:03