How to trigger github CI after merge request get merged?

Hi Team,

I want to trigger CI after MR get merged.

My usecase, i want to ship my source from github to gitlap once mr get merged

@kumaresan-subramani,
Once a PR is merged, it will be closed automatically. So, you can use the closed activity type of Pull request event to trigger the CI workflow when the PR is merged.

For example, a PR is opened for merging from feature branch into master branch. You can setup a workflow runs on pull_request.closed in the feature branch.

on:
  pull_request:
    types: [closed]

When the PR is merged, the PR closed automatically, and the workflow is triggered to run in the master branch (github.ref = master).

NOTE:
The closed activity type will trigger the workflow as long as the PR is closed, including manually close the PR without merged. So, if you want the workflow runs only when the PR is closed by merged, you can add the following if statement for each job in the workflow.

if: github.event.pull_request.merged == 'true'

This if statement will skip all the jobs in the workflow when the PR is closed without merged.

1 Like

Hi @brightran,

Thanks for your reply.

i will try and update you further

@brightran, still not get triggered

I need same ci after mr gets merged

Then only i can move source from github to gitlap succesfully

i have tried only with closed event too but still no luck for me

@kumaresan-subramani,
If possible, Please share you repository with us, so that we can check more details of your workflow to analyze the root cause.

1 Like

can you able to provide call session ?

if yes, which will helpful for me

@kumaresan-subramani,
I have found the link you shared in your previous reply (this reply seems is hidden by the community, however I can expand it and view the content).

i have tried only with closed event too but still no luck for me

What is the specific issue? The workflow run was not triggered, or have extra runs were triggered?
I check the workflow file in the “kumaresan-subramani-patch-5” branch of your repository, and I can see the below configuration:

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
    types: [opened, closed]

Under this configuration, the workflow will be triggered in the following situations:

  1. If this workflow file is existing in master branch, when pushing commits into master.
  2. If this workflow file is existing in the “kumaresan-subramani-patch-5” branch, when open a PR for merging from “kumaresan-subramani-patch-5” into master.
  3. If this workflow file is existing in the “kumaresan-subramani-patch-5” branch, when the PR is merged from “kumaresan-subramani-patch-5” into master.

In your cause when the PR is merged, this workflow will be triggered twice with push and pull_request.closed. If you want to only trigger the workflow once, you can remove push and only keep pull_request.

  pull_request:
    branches: [ master ]
    types: [opened, closed]
1 Like

@brightran

i have tried your suggestion, while applying your suggestion still same issue

@brightran

have one more issue too, which is while taking changed file names i am using below command in my gulp tasks

var changes = shelljs.exec('git diff --name-only');
   var changedFileNames = changes.stdout.split('\n');

but it always return only package.json file.

@kumaresan-subramani,

  1. About your first question:
    I checked PR you shared, the workflow worked fine, and I did not find any unexpected things occurred.
    The workflow was triggered twice is correct behavior, as I explained above, the “pull_request.open” and the “pull_request.closed” each triggered the workflow once. Because of the below configuration:
pull_request:
  branches: [ master ]
  types: [opened, closed]

If you want the workflow is triggered only when the PR is closed, no other situations to trigger the workflow, you can remove the activity type “opened” and only keep “closed”.

pull_request:
  branches: [ master ]
  types: [closed]
  1. About your second question:
    You need to know that when using the action Checkout V2, by default the action only fetches only a single commit. In this situation, the “git diff” command will return nothing or errors.
    If you want to list the changed files, you should check out the latest two commits at least. To do this, you need to set the value of the input parameter “fetch-depth” to be “2” at least. And then execute the “git diff” command. More details, see here.
    For example:
jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: Get changed files
        run: |
          git diff --name-only HEAD^ HEAD
1 Like

@brightran

Thank you so much

both solution 1 & 2 works

Question1

i have set pull request types like below

 pull_request:
    branches: [ master ]
    types: [opened, edited, closed, reopened]

But when i edit my PR,CI wont get trigger

Question 2

i have set like below as your previous suggestion

Question 2

i have set like below as your previous suggestion.

But my publish task not get triggered after merge also

@kumaresan-subramani,
About Question1:
The activity type “ pull_request.edited ” is available to the following situations:

  • Edit the title of the PR.
  • Edit the description of the PR (not to be confused with comments).
  • Change the base branch of the PR. In your case, due to you have set branches filter (branches: [ master ]) on pull_request, only when the base branch is changed to master can trigger the workflow.

About Question2:
This should be able to work as expected.

if: github.event.pull_request.merged == true
1 Like

@kumaresan-subramani,
How are things going? Is my suggestion helpful to you?
Any updates, feel free to tell me.

HI @brightran,

Thaks for getting back to me

still facing same issue while editing my PR, CI not get triggered

Repo steps

@kumaresan-subramani,

I noticed that there are some conflicts have not been resolved in the PR.


When the PR has conflicts, the pull_request event can’t trigger any workflows before the conflicts are resolved.
You need to resolve all the conflicts exist in the PR, then the pull_request event can trigger the workflow normally.

Still facing same problem

When PR created CI trigger as ecpected when i edit created branch CI does not trigger again

For example: create PR in that repo and then edit

@kumaresan-subramani,

From the workflow file in the “kumaresan-subramani-patch-23” branch, I can see you are using the event configuration below:

on:
  pull_request:
    branches: [ master ]
    types: [opened, closed]

According to this event configuration, this workflow is triggered only when one of the following activity occurs:

  • Open a PR for merging into master branch from the branch where the workflow file is located in.
  • Closed this PR.

Have you seen my above explanation about activity type “pull_request.edited”?
Now, let me explain again, “pull_request.edited” is only available to one of the following situations:

  • Edit the title of the PR.
  • Edit the description of the PR (not to be confused with comments).
  • Change the base branch of the PR to another branch. For example, in your case the base branch is master, if you change the base branch from master to another branch, the activity “pull_request.edited” occurs to trigger the workflow.

If you want the workflow can be triggered when you edit the PR, you need to add the activity type “pull_request.edited” in the workflow file in the source branch.

on:
  pull_request:
    branches: [ master ]
    types: [opened, closed, edited]

Still take your case as an example, the PR has been opened, the source branch is “kumaresan-subramani-patch-23” and the target branch is master. When you push some new commits to the “kumaresan-subramani-patch-23” branch, if you want this behavior can trigger the workflow to run for the PR, you need to add the activity type “pull_request.synchronize” in the workflow file in source branch.

on:
  pull_request:
    branches: [ master ]
    types: [opened, closed, edited, synchronize]
1 Like

Thanks dude …!

:smiling_face_with_three_hearts: :innocent: :money_mouth_face:

All are working fine now…

You’re welcome, @kumaresan-subramani .
If you have any other questions about this ticket, feel free to contact me.

1 Like