Skip to content

Commit

Permalink
Merge pull request #2161 from fluxcd/update-pr-auto-update
Browse files Browse the repository at this point in the history
Update the GitHub Actions Auto Pull Request use case
  • Loading branch information
matheuscscp authored Feb 27, 2025
2 parents 13799c2 + 3cabae7 commit 5bef78f
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions content/en/flux/use-cases/gh-actions-auto-pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ description: "How to configure GitHub Pull Requests for Flux image updates."
weight: 50
---

{{% alert color="warning" title="Disclaimer" %}}
Note that this guide needs review in consideration of Flux v2.0.0, and likely needs to be refreshed.

Expect this doc to either be archived soon, or to receive some overhaul.
{{% /alert %}}

This guide shows how to configure GitHub Actions to open a pull request whenever a selected branch is pushed.

From [Image Update Guide] we saw that Flux can set `.spec.git.push.branch` to [Push updates to a different branch] than the one used for checkout.
Expand Down Expand Up @@ -39,42 +33,46 @@ To create the pull request whenever automation creates a new branch, in your man
name: Staging Auto-PR
on:
create:
branches: ['staging']
branches: [staging]
jobs:
pull-request:
name: Open PR to main
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v3
name: checkout
- uses: repo-sync/pull-request@v2
name: pull-request
- name: Checkout
uses: actions/checkout@v4
with:
destination_branch: "main"
pr_title: "Pulling ${{ github.ref }} into main"
pr_body: ":crown: *An automated PR*"
pr_reviewer: "kingdonb"
pr_draft: true
github_token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Create Pull Request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # used implicitly by the gh CLI to authenticate with GitHub
GITHUB_REPO: ${{ github.repository }}
GITHUB_REF: ${{ github.ref }}
run: |
gh pr create \
--repo=${GITHUB_REPO} \
--head=staging \
--base=main \
--title="Pulling ${GITHUB_REF} into main" \
--body=":crown: *An automated PR*" \
--reviewer=kingdonb \
--draft
```

You can use the [GitHub Pull Request Action] workflow to automatically open a pull request against a destination branch. When `staging` is merged into the `main` branch, changes are deployed in production. Be sure to delete the branch after merging so that the workflow runs the next time that the image automation finds something to change.
In the example above, `--head` is the source branch and `--base` is the destination branch.

You can use th workflow above to automatically open a pull request against a destination branch. When `staging` is merged into the `main` branch, changes are deployed in production. Be sure to delete the branch after merging so that the workflow runs the next time that the image automation finds something to change, for that you can go to your
repository settings and enable the `Automatically delete head branches` option.

{{% alert title="Additional options" %}}
The "GitHub Pull Request Action" reference linked above documents more options, like `pr_reviewer` and `pr_assignee`, that setting will help make this workflow more usable. You can assign reviewers, labels, (use markdown emojis in the `pr_body`, make variable substitutions in the title, etc.)
The `gh pr create` CLI command used in the workflow above has more useful options, like `--fill-first`, `--label` and `--assignee`, that setting will help make this workflow more usable. You can assign reviewers, labels, (use markdown emojis in the `--body`, make variable substitutions in the title, etc.)
{{% /alert %}}

The [Create Pull Request Action] action might be a viable option to use instead, in case it's necessary to make some scripted edits in the same workflow (eg. manifest generation routines.)

With your own scripts, manifests can be updated with any current tags to make the staging branch ready for deployment. The "Create Pull Request" workflow can find and commit any updates for you.

This way you can automatically push changes to a `staging` branch and require review with manual approval of any automatic image updates, before they are applied on your production clusters.

Experiment with these strategies to find the right automated workflow solution for your team!

[Image Update Guide]: /flux/guides/image-update/
[Push updates to a different branch]: /flux/guides/image-update/#push-updates-to-a-different-branch
[GitHub Pull Request Action]: https://github.com/marketplace/actions/github-pull-request-action
[Create Pull Request Action]: https://github.com/marketplace/actions/create-pull-request

0 comments on commit 5bef78f

Please sign in to comment.