-
Notifications
You must be signed in to change notification settings - Fork 14
Fix Dependabot auto-merge by using pull_request_target for elevated permissions #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7952d7e
Bump github/codeql-action from 4.32.2 to 4.32.3
dependabot[bot] f0e43ce
Initial plan
Copilot d39f9a4
Use pull_request_target to fix Dependabot auto-merge permissions
Copilot ad151c4
Add documentation for Dependabot auto-merge workflow
Copilot 735598d
Fix error handling and improve regex pattern specificity
Copilot 4502886
Refine regex patterns and clarify documentation
Copilot 61e25dc
Apply markdownlint fixes
github-actions[bot] fcf19a8
Remove auto-approval step per project lead request
Copilot 3dd61c0
Address review comments: add pipefail and constrain trigger types
Copilot e626540
Explicitly specify bash shell for pipefail compatibility
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # Dependabot Auto-Merge Workflow | ||
|
|
||
| ## Overview | ||
|
|
||
| The `dependabot-auto-merge.yaml` workflow automatically enables auto-merge for Dependabot pull requests targeting the `main` branch. Once all branch protection requirements are met (approvals, CI checks), the PR will merge automatically. | ||
|
|
||
| ## How It Works | ||
|
|
||
| 1. **Triggers:** The workflow runs when: | ||
| - A Dependabot PR is opened or updated (`pull_request_target`) | ||
| - A review is submitted on a Dependabot PR (`pull_request_review`) | ||
| - CI checks complete on a Dependabot PR branch (`check_suite`) | ||
|
|
||
| 2. **Verification:** The workflow verifies that: | ||
| - The PR author is `dependabot[bot]` | ||
| - The base branch is `main` | ||
|
|
||
| 3. **Actions:** | ||
| - Enables auto-merge on the PR | ||
|
|
||
| 4. **Merge:** Once all branch protection requirements are satisfied (including **manual approval** and passing CI checks), GitHub automatically merges the PR. | ||
|
|
||
| ## Important: Use of `pull_request_target` | ||
|
|
||
| This workflow uses the `pull_request_target` event instead of `pull_request` to obtain elevated permissions needed to enable auto-merge on protected branches. | ||
|
|
||
| **Why this is needed:** | ||
| When workflows are triggered by Dependabot PRs using the `pull_request` event, the `GITHUB_TOKEN` has **read-only permissions** for security reasons, even if the workflow declares `contents: write` and `pull-requests: write`. This is a GitHub security feature to prevent malicious dependency updates from modifying the repository. | ||
|
|
||
| **Why this is safe:** | ||
| Using `pull_request_target` for Dependabot auto-merge is safe because: | ||
|
|
||
| 1. We verify the PR author is `dependabot[bot]` before taking any action | ||
| 2. We don't check out or execute code from the PR | ||
| 3. We only enable auto-merge, which still requires all branch protection rules to pass | ||
| 4. Dependabot PRs are created by GitHub's trusted Dependabot service | ||
|
|
||
| ## Alternative Solutions | ||
|
|
||
| If you prefer not to use `pull_request_target`, there are more secure alternatives: | ||
|
|
||
| ### Option 1: Use a GitHub App (Recommended) | ||
|
|
||
| Create a GitHub App with appropriate permissions and use it to generate tokens: | ||
|
|
||
| 1. Create a GitHub App with `contents: write` and `pull_requests: write` permissions | ||
| 2. Install the app on the repository | ||
| 3. Generate an installation access token in the workflow | ||
| 4. Use the token instead of `secrets.GITHUB_TOKEN` | ||
|
|
||
| Example: | ||
|
|
||
| ```yaml | ||
| - uses: actions/create-github-app-token@v1 | ||
| id: app-token | ||
| with: | ||
| app-id: ${{ secrets.APP_ID }} | ||
| private-key: ${{ secrets.PRIVATE_KEY }} | ||
| - name: Enable auto-merge | ||
| env: | ||
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | ||
| run: gh pr merge --auto --merge "$PR_NUMBER" | ||
| ``` | ||
|
|
||
| ### Option 2: Use a Personal Access Token (PAT) | ||
|
|
||
| Create a PAT with appropriate permissions and store it as a repository secret: | ||
|
|
||
| 1. Create a PAT with `repo` scope | ||
| 2. Store it as a repository secret (e.g., `DEPENDABOT_PAT`) | ||
| 3. Use it in the workflow | ||
|
|
||
| Example: | ||
|
|
||
| ```yaml | ||
| - name: Enable auto-merge | ||
| env: | ||
| GH_TOKEN: ${{ secrets.DEPENDABOT_PAT }} | ||
| run: gh pr merge --auto --merge "$PR_NUMBER" | ||
| ``` | ||
|
|
||
| **Note:** This is simpler than using a GitHub App but less secure because PATs have broader access and don't expire automatically. | ||
|
|
||
| ### Option 3: Repository Settings | ||
|
|
||
| Configure the repository to allow Dependabot to bypass branch protection: | ||
|
|
||
| 1. Go to repository Settings → Branches → Branch protection rules | ||
| 2. Edit the rule for `main` | ||
| 3. Add `dependabot[bot]` to the list of actors who can bypass required pull requests | ||
|
|
||
| **Note:** This approach has security implications and is generally not recommended. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Auto-merge is not being enabled | ||
|
|
||
| If the workflow runs but auto-merge is not enabled, check the workflow logs for error messages: | ||
|
|
||
| - **"auto-merge is already enabled"** - Auto-merge was already set on a previous run | ||
| - **"not authorized for this protected branch"** - The token doesn't have sufficient permissions. This can occur if: | ||
| - Branch protection rules require additional permissions beyond what `pull_request_target` provides | ||
| - The repository has custom protection rules that prevent even elevated tokens from enabling auto-merge | ||
| - Consider using a GitHub App or PAT with appropriate permissions (see Alternative Solutions above) | ||
| - **"Required status checks"** - Waiting for CI checks to pass | ||
| - **"Required approving review"** - Waiting for approval (the workflow attempts to approve automatically) | ||
|
|
||
| ### The PR is not merging automatically | ||
|
|
||
| Even after auto-merge is enabled, the PR won't merge until: | ||
|
|
||
| 1. All required reviews are approved | ||
| 2. All required status checks pass | ||
| 3. No blocking conversations exist (if required) | ||
| 4. All other branch protection requirements are met | ||
|
|
||
| Check the PR's "Merge" section for the current status. | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| - The workflow only runs on PRs opened by `dependabot[bot]` | ||
| - It does not check out or run code from the PR | ||
| - Auto-merge still requires all branch protection rules to pass | ||
| - The workflow is transparent and auditable (all runs are visible in the Actions tab) | ||
|
|
||
| ## References | ||
|
|
||
| - [GitHub Docs: Automatically merging a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request) | ||
| - [GitHub Docs: Automating Dependabot with GitHub Actions](https://docs.github.com/en/code-security/dependabot/working-with-dependabot/automating-dependabot-with-github-actions) | ||
| - [Dependabot fetch-metadata action](https://github.com/dependabot/fetch-metadata) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.