11

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.

roy
  • 6,344
  • 24
  • 92
  • 174

1 Answers1

13

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"}'
        ...
GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • `job_params` are different for both triggers. – roy Jun 11 '21 at 17:23
  • 1
    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 action](https://github.com/dorny/paths-filter) – GuiFalourd Jun 11 '21 at 17:27
  • 1
    I updated the answer with an example using this paths-filter action – GuiFalourd Jun 11 '21 at 17:38
  • 1
    Thanks for the pointer to dorny/paths-filter; I used it to solve [a similar filtering issue](https://github.community/t/how-to-trigger-a-github-action-for-a-branch-or-changes-to-a-path/207444/2?u=theory). – theory Oct 24 '21 at 16:36