How to trigger different action only on merge

Hey,

I’m having a problem implementing my workflow in a single action file:

I want to run:

* only step1 on every push to feature branch (other than master)

* only step1 on a pull request

* step1 and step2 on merge

This is my pipeline:

name: Test

on:
  push:
    branches-ignore:
      - master
  pull_request:

jobs:

  build:
    name: Testing
    runs-on: ubuntu-latest
    steps:

    - name: Step 1
      run: |
        echo " Step 1"

    - name: Step 2
      if: github.event.pull_request.merged == true
      run: |
        echo " Step 2"

I can implement my requirements with 2 files but I would like to keep it in one if that is possible.

Hi @danielinclouds ,

Merge pull request will not trigger the workflow. By default, the pull_request’s activity type is opened, synchronize, and reopened. Hence you need to add ‘closed’ type for pull request event. Code sample as below:

name: Test

on:
  push:
    branches-ignore:
      - master
<font color="#FF6600"> pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed</font>jobs:

  build:
    name: Testing
    runs-on: ubuntu-latest
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

      - name: Step 1
        run: |
          echo " Step 1"

      - name: Step 2
        if: github.event.pull_request.merged == true
        run: |
          echo " Step 2"

mergepr.png

Hope it’s clear and helpful!

1 Like

Thanks, @weide-zhou that was exactly what I was looking for.

Thanks! I’d been looking for this for a while.

There are some styling that mess up with the readability of your answer :slight_smile:

Thanks @vadorequest, this is caused by the community forum update, i tried but cannot edit the code. You can ignore the styling code like <font></font> :joy:.