Skip to content

Commit 551fb68

Browse files
committed
workflows: Auto go mod tidy dependabot PRs
Do to our repo structure of go modules, we frequently hit issues that dependabout bumps versions, but other modules that reference the changes module don't get updated, so we have error with go mod and sum not being tidied. Attempt to create a workflow that will automatically do this change, so that we don't need to do it manually anymore. Signed-off-by: stevenhorsman <[email protected]>
1 parent 172c096 commit 551fb68

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Automatically run go mod tidy on dependabot PRs
2+
on:
3+
pull_request_target:
4+
branches: [ main ]
5+
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
8+
cancel-in-progress: true
9+
10+
permissions: {}
11+
12+
jobs:
13+
tidy-dependabot-pr:
14+
name: Run go mod tidy on dependabot PR
15+
runs-on: ubuntu-24.04
16+
if: github.event.pull_request.user.login == 'dependabot[bot]'
17+
permissions:
18+
contents: write # We need permissions to push new content to the PR
19+
pull-requests: write # Permissions to create a new PR and close the original
20+
21+
steps:
22+
- name: Checkout the PR Code
23+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
24+
with:
25+
fetch-depth: 0
26+
ref: ${{ github.event.pull_request.head.sha }}
27+
persist-credentials: false
28+
29+
- name: Rebase the code
30+
if: github.event_name == 'pull_request_target'
31+
working-directory: ./
32+
run: |
33+
./hack/ci-helper.sh rebase-atop-of-the-latest-target-branch
34+
35+
- name: Read properties from versions.yaml
36+
id: read-properties
37+
run: |
38+
go_version="$(yq '.tools.golang' src/cloud-api-adaptor/versions.yaml)"
39+
[ -n "$go_version" ]
40+
echo "go_version=${go_version}" >> "${GITHUB_OUTPUT}"
41+
42+
- name: Setup Golang version
43+
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
44+
with:
45+
go-version: ${{ steps.read-properties.outputs.go_version }}
46+
cache-dependency-path: "**/go.sum"
47+
cache: false
48+
49+
- name: Go tidy check
50+
run: |
51+
./hack/go-tidy.sh
52+
echo "Go mod tidy made: $(git diff)"
53+
54+
- name: Check if go mod tidy produces changes
55+
id: go-mod-tidy-changed
56+
run: |
57+
if [ -n "$(git status --porcelain)" ]; then
58+
echo "changed=true" >> "${GITHUB_OUTPUT}"
59+
else
60+
echo "changed=false" >> "${GITHUB_OUTPUT}"
61+
fi
62+
63+
- name: Commit and push changes
64+
if: steps.go-mod-tidy-changed.outputs.changed == 'true'
65+
uses: devops-infra/action-commit-push@8a2d9d73c3f506468129be2e4409e60dbed70357 # v1.0.3
66+
with:
67+
github_token: ${{ secrets.GITHUB_TOKEN }}
68+
commit_message: "chore: Auto-run go mod tidy after Dependabot update"
69+
70+
- name: Create new pull request
71+
if: steps.go-mod-tidy-changed.outputs.changed == 'true'
72+
id: create_pr
73+
uses: devops-infra/action-pull-request@b2895bff2ff66579f6704d717a1ea75fad919e84 # v1.0.2
74+
with:
75+
github_token: ${{ secrets.GITHUB_TOKEN }}
76+
body: "**Automated pull request**<br><br>Updating dependabot PR with go mod tidy"
77+
title: ${{ github.event.commits[0].message }}
78+
79+
- name: Close original Pull Request
80+
if: steps.go-mod-tidy-changed.outputs.changed == 'true'
81+
run: gh pr close "${ORIGINAL_PR}" --comment "PR replaced with ${REPLACEMENT_PR}"
82+
env:
83+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84+
ORIGINAL_PR: ${{ github.event.pull_request.number }}
85+
REPLACEMENT_PR: ${{ steps.create_pr.outputs.pr_number }}

0 commit comments

Comments
 (0)