Github Action Trigger on multiple on: push: paths
Asked Answered
T

2

12

In single repo, I want to create more than one trigger for different paths [paths: 'frontend/**' and paths: 'backend/**'] with different parameters for (same) build jobs. Following are the two workflow.

name: Trigger Jenkins Build [ Build-Portal ]
on:
  push:
    branches: [ develop ]
    paths: 'frontend/**'
    types: [closed]
jobs:
  build:
    name: Triggering Jenkins Build [ Build-Portal ]
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged == true
    steps:
    - name: Trigger Build-Portal
      uses: actions/trigger-jenkins@develop
      with:
        ...
        job_name: "Build-Portal"
        job_params: '{"FRESH_BUILD":"True", "UI":"True", "BUILD_BRANCH":"develop", "DEPLOY_DEV":"True"}'
        ...

and

name: Trigger Jenkins Build [ Build-Portal ]
on:
  push:
    branches: [ develop ]
    paths: 'backend/**'
    types: [closed]
jobs:
  build:
    name: Triggering Jenkins Build [ Build-Portal ]
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged == true
    steps:
    - name: Trigger Build-Portal
      uses: actions/trigger-jenkins@develop
      with:
        ...
        job_name: "Build-Portal"
        job_params: '{"FRESH_BUILD":"True", "API":"True", "BUILD_BRANCH":"develop", "DEPLOY_DEV":"True"}'
        ...

Can I combine these two in a single workflow file (.github/workflows/) or it need to have separate files for each ?

Note : job_params are different in for both triggers.

Teplica answered 11/6, 2021 at 17:15 Comment(0)
M
15

According to the github action documentation, it should work in the same workflow using multiple paths.

If you also use the paths-filter action you can get to the result you want with something like this:

Example:

name: Trigger Jenkins Build [ Build-Portal ]
on:
  push:
    branches: [ develop ]
    paths: 
       - 'frontend/**'
       - 'backend/**'
    types: [closed]
jobs:
  build:
    name: Triggering Jenkins Build [ Build-Portal ]
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged == true
    steps:
      - uses: dorny/paths-filter@v2
        id: changes
        with:
           filters: |
              backend:
                - 'backend/**'
              frontend:
                - 'frontend/**'
    - name: Trigger Build-Portal FRONTEND
      # run only if some file in 'frontend' folder was changed
      if: steps.changes.outputs.frontend == 'true'
      uses: actions/trigger-jenkins@develop
      with:
        ...
        job_name: "Build-Portal"
        job_params: '{"FRESH_BUILD":"True", "UI":"True", "BUILD_BRANCH":"develop", "DEPLOY_DEV":"True"}'
        ...
     - name: Trigger Build-Portal BACKEND
      # run only if some file not 'backend' folder was changed
      if: steps.changes.outputs.backend == 'true'
      uses: actions/trigger-jenkins@develop
      with:
        ...
        job_name: "Build-Portal"
        job_params: '{"FRESH_BUILD":"True", "API":"True", "BUILD_BRANCH":"develop", "DEPLOY_DEV":"True"}'
        ...
Maledict answered 11/6, 2021 at 17:21 Comment(4)
job_params are different for both triggers.Teplica
Sorry, I didn't see the UI and API difference in the job_params. In that case, you can use 2 jobs with an if condition different according to the path used. It is also possible to use the paths-filter actionMaledict
I updated the answer with an example using this paths-filter actionMaledict
Thanks for the pointer to dorny/paths-filter; I used it to solve a similar filtering issue.Casarez
O
0

I don't see the reason behind this complexity. Just use 2 YML files

frontend.yml => this is where you put path condition to match frontend path

on:
 push:
  branches: [ develop ]
  paths: 
   - 'frontend/**'

backend.yml => this is where you put path condition to match backend path

on:
 push:
  branches: [ develop ]
  paths: 
   - 'backend/**'

Both files will be evaluated everytime a push is done and GH will decide which flow to run.

You can always use a default yml file to run anyway whenever any change occurs outside these paths

Octavo answered 24/3 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.