-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (97 loc) · 3.75 KB
/
update_go_modules.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
name: Update Go Modules
on:
schedule:
- cron: '0 0 * * *' # Runs every day at midnight
workflow_dispatch: # Allows manual triggering of the workflow
jobs:
update_go_modules:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Ensure running on main branch
id: check_branch
run: |
if [ "${{ github.ref }}" != "refs/heads/main" ]; then
echo "Not on main branch, exiting."
exit 1
fi
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.17'
- name: Save current go.mod
run: cp go.mod go.mod.bak
- name: Get current Go version
id: get_current_go_version
run: |
CURRENT_GO_VERSION=$(grep '^go ' go.mod | cut -d ' ' -f 2)
echo "CURRENT_GO_VERSION=$CURRENT_GO_VERSION" >> $GITHUB_ENV
- name: Get latest Go version
id: get_latest_go_version
run: |
LATEST_GO_VERSION=$(go version | cut -d ' ' -f 3 | sed 's/go//')
echo "LATEST_GO_VERSION=$LATEST_GO_VERSION" >> $GITHUB_ENV
- name: Update Go version in go.mod if needed
id: update_go_version
run: |
if [ "${{ env.CURRENT_GO_VERSION }}" != "${{ env.LATEST_GO_VERSION }}" ]; then
sed -i "s/^go .*/go $LATEST_GO_VERSION/" go.mod
echo "GO_VERSION_UPDATED=true" >> $GITHUB_ENV
else
echo "GO_VERSION_UPDATED=false" >> $GITHUB_ENV
fi
- name: Update Go modules
run: |
go get -u ./...
go mod tidy
- name: Get updated modules
id: get_updated_modules
run: |
echo "Updated modules:" > updated_modules.txt
go list -m -u all | awk 'NR>1' >> updated_modules.txt
- name: Check for changes
id: check_changes
run: |
if git diff --quiet; then
echo "No changes in go.mod or go.sum"
echo "exit_code=0" >> $GITHUB_ENV
else
echo "Changes found in go.mod or go.sum"
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
BRANCH_NAME="update-go-modules-$TIMESTAMP"
git checkout -b $BRANCH_NAME
git add go.mod go.sum
git commit -m "Update Go modules and Go version to the latest version"
git push --set-upstream origin $BRANCH_NAME
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
echo "exit_code=1" >> $GITHUB_ENV
fi
- name: Generate PR body
if: env.exit_code == '1'
id: generate_pr_body
run: |
echo "# Updated Go Modules" > pr_body.md
echo "" >> pr_body.md
echo "The following Go modules were updated:" >> pr_body.md
echo "" >> pr_body.md
cat updated_modules.txt >> pr_body.md
echo "" >> pr_body.md
if [ "${{ env.GO_VERSION_UPDATED }}" == "true" ]; then
echo "Go version updated from ${{ env.CURRENT_GO_VERSION }} to ${{ env.LATEST_GO_VERSION }}" >> pr_body.md
fi
pr_body=$(<pr_body.md)
echo "::set-output name=pr_body::$pr_body"
- name: Create Pull Request
if: env.exit_code == '1'
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Update Go modules and Go version to the latest version
branch: ${{ env.BRANCH_NAME }}
base: main
title: Update Go modules and Go version
body: ${{ steps.generate_pr_body.outputs.pr_body }}
labels: go-modules, automated-update