0

I'm trying to find out the best way to do the following: Let's say we have 2 branches - develop and master after a push in 'develop' I want to trigger a CI\CD job, and it's working fine but the tricky part is to do same thing for the 'master branch' I mean, after the 'develop' branch finished and merged the changes into master. Shouldn't the Webhook consider that as a 'push' and activate the CI again?

So just to be clear, the webhook is only working for the 'develop' ci\cd job and not continuing to 'master'. (working actions are in bold) push to 'develop' -> trigger CI --> if pass trigger CD --> if pass trigger CI for 'master'

maya li
  • 33
  • 3

2 Answers2

2

you can set up your webhook to trigger the CI/CD job for the 'master' branch when changes are merged from 'develop' to 'master'. This can be achieved by configuring the webhook to listen to the 'push' event on the 'master' branch.

Here's how you can configure this:

  1. Configure the webhook to listen to 'push' events on the 'develop' branch.

  2. In your CI/CD job for the 'develop' branch, add a step to merge changes into the 'master' branch.

  3. Configure the webhook to listen to 'push' events on the 'master' branch as well.

  4. In your CI/CD job for the 'master' branch, add a step to build and deploy the changes.

This way, when changes are pushed to the 'develop' branch, the webhook will trigger the CI/CD job for the 'develop' branch. After the changes are merged into the 'master' branch, the webhook will trigger the CI/CD job for the 'master' branch as well.

Another way to do it, if you want better control is to use GitHub actions in the middle, and write a very simple job that triggers the webhook when ever there is push to master/develop branch.

Let me know if you want further information about the implementation of a github actions workflow.

user15937765
  • 219
  • 1
  • 7
  • First of all - thanks :) Second - as far as I understand, Github Webhook are for repository and not single branch, am I wrong on that? because that surly can make things easier for me :) And last - yes, I would be happy to know more about github action, but the question here is - should I use this or should I just write my script in the CD groovy file and trigger the job from there? – maya li Mar 26 '23 at 11:40
2

As for GH Actions, you can set a workflow that directly reacts to any action: Such as push to develop or merge to master. You can write the workflow with two conditioned steps so one time it will run the webhook on push to 'develop' and on the second step it will run the workflow when there was a merge to master from develop.

The workflow should look somewhat like this:

    name: Trigger Jenkins Pipeline

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

jobs:
  trigger-jenkins-pipeline:
    runs-on: ubuntu-latest
    steps:
    - name: Trigger Jenkins pipeline for develop branch
      if: github.ref == 'refs/heads/develop'
      uses: technicalpanda/http-request-action@v1.0.0
      with:
        url: 'http://jenkins.example.com/job/develop/build'
        method: 'POST'
        headers: |
          Content-Type: application/json
        body: '{}'
    - name: Trigger Jenkins pipeline for master branch
if: github.event.pull_request.merged && github.event.pull_request.base.ref == 'master' && github.event.pull_request.head.ref == 'develop'
      uses: technicalpanda/http-request-action@v1.0.0
      with:
        url: 'http://jenkins.example.com/job/master/build'
        method: 'POST'
        headers: |
          Content-Type: application/json
        body: '{}'
user15937765
  • 219
  • 1
  • 7