Search code examples
githubgithub-apigithub-actions

How to trigger github CI once PR get merged


I want to trigger my GitHub ci after merge request get merged. because i want to ship my source from GitHub to gitlab after merge request merged in GitHub

Example - https://github.com/kumaresan-subramani/ej2-blaz-doc/pull/7

CI getting triggered once pull request getting created, but after merge it won't trigger CI so that I am not able to ship my source from github to gitlab

enter image description here

My yml file :

name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
    types: [opened, closed]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - name: set environment variables
      uses: allenevans/[email protected]
      with:
          MY_ENV_VAR: 'my value'
          GIT_USER: ${{ secrets.USER }}
          GIT_TOKEN: ${{ secrets.TOKEN }}
    - name: Install
      run: npm i
    - name: Publish
      run: npm run publish
      if: github.event.pull_request.merged == 'true'

Solution

  • You need to compare the field value merged with boolean value, not string.

    github.event.pull_request.merged == true
    

    instead of github.event.pull_request.merged == 'true'

    You could also write:

    if: github.event.pull_request.merged
    

    For instance the following workflow compare the values when you open/close a PR :

    on: 
      pull_request:
        types: [opened, closed]
    name: build
    jobs:
      build:
        name: Input check
        runs-on: ubuntu-latest
        steps:
          - name: Checking your input
            run: |
              echo "github.event.pull_request.merged           : $MERGED_RAW"
              echo "github.event.pull_request.merged == 'true' : $MERGED_TRUE_STR"
              echo "github.event.pull_request.merged  == true  : $MERGED_TRUE_BOOL"
            env:
              MERGED_RAW: ${{ github.event.pull_request.merged }}
              MERGED_TRUE_STR: ${{ github.event.pull_request.merged == 'true' }}
              MERGED_TRUE_BOOL: ${{ github.event.pull_request.merged == true }}
    

    When you merged the PR, you've got the following result :

    github.event.pull_request.merged           : true
    github.event.pull_request.merged == 'true' : false
    github.event.pull_request.merged == true   : true