github actions run a job on pull request merge to specific branch #67160
-
Select Topic AreaQuestion Body0 I am trying to run a job within github actions workflow with the following condition. My aim is to run the job if a pull request to develop branch is merged. However, when I merge a pull reqest to develop branch, the job is skipped. Can someone pl point me what I may be doing wrong?
and then the condition for the specific job
If I change the heads to base in the above if condition, its still the same result i.e. the job is skipped. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Option [1]Hello. A potential workaround could be removing the following snippet: on:
pull_request:
# For the develop branch only.
branches:
- develop
# Execute the action on closed (merged) PR.
types:
[closed]
workflow_dispatch: Note: I removed all branches except "develop" from the branches field. If you have separate jobs that run on those branches, consider making another GitHub Actions yml file for this job specifically. Abstraction and separation of concerns is your best friend here. Option [2]GitHub unfortunately does not have the pull-request-merged event. However, in the case that the only action you take with regards to the develop branch is opening and merging PRs to it, you could simply do the following: on:
push:
# For the develop branch only.
branches:
- develop
workflow_dispatch: A merged PR to the develop branch will always trigger the push event, which executes your GitHub actions file's jobs accordingly. Option [3]The final option (and perhaps most reliable option, if done right) is the best of both worlds. It ensures that your jobs only run if the following conditions are met:
on:
pull_request:
branches:
- develop
types:
- closed
# Later, in the jobs section of your actions.yml file.
jobs:
if_merged:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- run: |
echo The PR was successfully merged. Edits (ignore)
|
Beta Was this translation helpful? Give feedback.
-
Thank you so much @elendil7 for providing answers with options. It was really helpful. I have used option 3, and it worked. |
Beta Was this translation helpful? Give feedback.
Option [1]
Hello. A potential workaround could be removing the following snippet:
if: ${{ github.ref == 'refs/heads/develop' && github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true }}
And instead relying solely on the following configuration:
Note: I removed all branches except "develop" from the branches field. If you have separate jobs that run on those branches, consider making another GitHub Actions yml file for this job specifically. Abstractio…