Run particular jobs while creating pull request in githubactions

Viewed 890

I have implemented ci/cd using GitHub actions. In ci/cd I have three jobs are there when I want to release a tag I want to build these three jobs and when I raise a pull request to a particular branch only two jobs should be executed for health check purposes. for example, I have a feature branch I want to merge this feature branch to the devel branch. when I raise a PR should be run only two jobs. how can I achieve this? below is my sample code.

name: CI

on:
  pull_request:
    branches:
      - master
      - devel
  push:
    tags:
      - '*'
jobs:
  build:
    name: build
    runs-on: self-hosted
    steps:
       --------------
   deploy:
    name: deploy
    runs-on: self-hosted
    steps:
      ------------
   automation-test:
     name: test
     runs-on: self-hosted
     steps:
       ------------
 

here when I raise a PR I want to run build and automation-test jobs.

1 Answers

You have two options here:

  1. Have separate workflow files that run for the right branches.
  2. Use conditionals in your job

The first option is likely the one you want. The only problem here is if the output from one job is used in another, but it doesn't sound like that's the case for you. I would recommend you simply break out your yaml workflows into two separate workflows:

name: CI

on:
  pull_request:
    branches:
      - master
  push:
    tags:
      - '*'
jobs:
  build:
    name: build
    runs-on: self-hosted
    steps:
       --------------
   deploy:
    name: deploy
    runs-on: self-hosted
    steps:
      ------------
   automation-test:
     name: test
     runs-on: self-hosted
     steps:
       ------------
name: PR Builder

on:
  pull_request:
    branches:
      - devel
jobs:
    whatever_testing_jobs_you_like:

The second option might look something like this:

name: CI

on:
  pull_request:
    branches:
      - master
  push:
    tags:
      - '*'
jobs:
  build:
    name: build
    runs-on: self-hosted
    steps:
       --------------
   deploy:
    if: "github.ref != devel" # you might tweak the condition based on your needs
    name: deploy
    runs-on: self-hosted
    steps:
      ------------
   automation-test:
     name: test
     runs-on: self-hosted
     steps:
       ------------

These context values / conditions are well documented

Related