Search code examples
githubyamlgithub-actionsdevopscicd

How to write yml script to run the steps after pull request merge


I have a .yaml file inside folder .github/workflows/workflow.yml to deploy the react app on github pages when a pull request is merged but it is failing saying Invalid workflow file. You have an error in your yaml syntax on line 13 Please help me

name: Github Pages Deploy

on:
  push:
    branches:
      - 'master'
  pull_request:
    branches:
      - 'master'
    types: [closed]

jobs:
  if: github.event.pull_request.merged == true
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [15.x, 16.x]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install and build
        run: npm install --save --legacy-peer-deps && npm run build

      - name: Deploy
        uses: JamesIves/[email protected]
        with:
          branch: gh-pages # The branch the action should deploy to
          folder: build # The folder the action should deploy

The line number 13 is this if: github.event.pull_request.merged == true


Solution

  • I think the issue is here.

    jobs:
      if: github.event.pull_request.merged == true
      build:
        runs-on: ubuntu-latest
    
        strategy:
          matrix:
            node-version: [15.x, 16.x]
    
        steps:
          - name: Checkout
            uses: actions/checkout@v1
    
          - name: Install and build
            run: npm install --save --legacy-peer-deps && npm run build
    
          - name: Deploy
            uses: JamesIves/[email protected]
            with:
              branch: gh-pages # The branch the action should deploy to
              folder: build # The folder the action should deploy
    

    The if statement has to be inside of jobs.<job_id>(In this case it's build). So try to change it to

    jobs:
      build:
        if: github.event.pull_request.merged == true
        runs-on: ubuntu-latest
    
        strategy:
          matrix:
            node-version: [15.x, 16.x]
    
        steps:
          - name: Checkout
            uses: actions/checkout@v1
    
          - name: Install and build
            run: npm install --save --legacy-peer-deps && npm run build
    
          - name: Deploy
            uses: JamesIves/[email protected]
            with:
              branch: gh-pages # The branch the action should deploy to
              folder: build # The folder the action should deploy