This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
action.yml
91 lines (81 loc) · 3.88 KB
/
action.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
name: copy workflow go
description: Copy workflow steps specific to go
runs:
using: "composite"
steps:
- uses: actions/setup-go@v3
with:
# This should be the same Go version we use in the go-check workflow.
# go mod tidy, go vet, staticcheck and gofmt might behave differently depending on the version.
go-version: "1.19.x"
- name: bump go.mod go version if needed
uses: protocol/multiple-go-modules@v1.2
with:
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
# We want our modules to support two Go versions at a time.
# As of August 2022, Go 1.19 is the latest stable.
# go.mod's Go version declares the language version being used.
# As such, it has to be the minimum of all Go versions supported.
# Bump this every six months, as new Go versions come out.
TARGET_VERSION=1.18
PREVIOUS_TARGET_VERSION=1.17
# Note that the "<" comparison doesn't understand semver,
# but it should be good enough for the foreseeable future.
CURRENT_VERSION=$(go list -m -f {{.GoVersion}})
if [[ $CURRENT_VERSION < $TARGET_VERSION ]]; then
echo "GO_VERSION_BUMP=1" >> $GITHUB_ENV
# We tidy immediately because some updates require it
# We ensure dependencies are resolved to versions supported by the PREVIOUS_TARGET_VERSION of Go
# It also modifies Go version in the go.mod which ensures there's a diff
go mod tidy -go=$PREVIOUS_TARGET_VERSION && go mod tidy -go=$TARGET_VERSION
# Just in case, check again if everything's OK after the version upgrade
go mod tidy
# In the future, "go fix" may make changes to Go code,
# such as to adapt to language changes or API deprecations.
# This is largely a no-op as of Go 1.17, and that's fine.
go fix ./...
git add .
# We don't tidy, because the next step does that.
# Separate commits also help with reviews.
git commit -m "bump go.mod to Go $TARGET_VERSION and run go fix"
# As of Go 1.19 io/ioutil is deprecated
# We automate its upgrade here because it is quite a widely used package
if [[ $TARGET_VERSION == "1.18" ]]; then
while read file; do
sed -i 's/ioutil.NopCloser/io.NopCloser/' "${file}";
sed -i 's/ioutil.ReadAll/io.ReadAll/' "${file}";
# Skipping ReadDir replacement because it's a bit more complicated
# See https://pkg.go.dev/io/ioutil#ReadDir
# sed -i 's/ioutil.ReadDir/os.ReadDir/' "${file}";
sed -i 's/ioutil.ReadFile/os.ReadFile/' "${file}";
sed -i 's/ioutil.TempDir/os.MkdirTemp/' "${file}";
sed -i 's/ioutil.TempFile/os.CreateTemp/' "${file}";
sed -i 's/ioutil.WriteFile/os.WriteFile/' "${file}";
done <<< "$(find . -type f -name '*.go')"
goimports -w .
git add .
git commit -m "stop using the deprecated io/ioutil package"
fi
fi
- name: go mod tidy (on initial workflow deployment)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 }}
uses: protocol/multiple-go-modules@v1.2
with:
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
go mod tidy
if ! git diff --quiet; then
git add .
git commit -m "run go mod tidy"
fi
- name: gofmt -s (on initial workflow deployment and on new Go version)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 || env.GO_VERSION_BUMP == 1}}
working-directory: ${{ env.TARGET_REPO_DIR }}
shell: bash
run: |
gofmt -s -w .
if ! git diff --quiet; then
git add .
git commit -m "run gofmt -s"
fi