Stephan Ahlf
Stephan Ahlf

Reputation: 3497

How to auto merge pull request on github?

Is it possible to merge pull request automaticaly to master branch on github after success of travis test webhook?

Upvotes: 12

Views: 7514

Answers (5)

thehale
thehale

Reputation: 1786

I recently wrote a blog post describing how to automatically merge trusted pull requests on GitHub.

Essentially, after enabling the required settings on your repository, you can use the following GitHub Actions workflow to tell GitHub to merge the PR automatically after required status checks pass.

name: Auto-merge Dependabot PRs

on: pull_request

permissions:
  contents: write
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: ${{ github.actor == 'dependabot[bot]' }}
    steps:
      - name: Approve a PR
        run: gh pr review --approve "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
      - name: Enable auto-merge for Dependabot PRs
        run: gh pr merge --auto --rebase "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

If you want to allow auto-merging PRs from users other than dependabot, change the if condition to use a list of trusted author names:

if: ${{ contains(fromJson('["dependabot[bot]", "octocat"]'), github.actor) }}

P.S. I recognize that the OP asked specifically about Travis CI, but this was the top Google Search result when I started figuring out how to auto merge PRs on GitHub. Hopefully my answer helps others in my same shoes.

Upvotes: 3

Adam Millerchip
Adam Millerchip

Reputation: 23147

I run a bot that does this.

Mergery is:

  • Free, including for private repositories.
  • Fast. It's event-driven, it doesn't run on a schedule.
  • Simple. No configuration required. Just label your PRs with automerge.

Upvotes: 0

bdougie
bdougie

Reputation: 781

GitHub recently shipped this auto-merge feature in beta. To use this, you can enable it in the repo settings. Just keep in mind you will need to add branch protection rules as well.

See the documentation for more info.

https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/automatically-merging-a-pull-request

Upvotes: 3

jd_
jd_

Reputation: 510

You can use Mergify to do this.

It allows to configure rules and define criteria for your pull request to be automatically merged. In your case, setting something like "Travis check is OK and one reviewer approved the PR" would allow the PR to be automatically merged.

(Disclosure: I'm part of the Mergify team.)

Upvotes: 5

Guillaume
Guillaume

Reputation: 18895

You can most probably add an after_success action to your .travis.yml that would merge the PR using GitHub API. I do not know of any ready to use script for this, but there is no reason for it to be hard. Special care needed for authentication ...

Upvotes: 4

Related Questions