forked from 030/gomod-go-version-updater-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
175 lines (158 loc) · 6.59 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
name: gomod-go-version-updater-action
description: gomod-go-version-updater-action
inputs:
extra-pr-label:
description: |
Whether an extra PR label is required.
github-token-for-downloading-private-go-modules:
description: |
Whether private go modules have to be downloaded.
gomod-go-version-updater-action-log-level:
description: |
Change the default INFO log level to: DEBUG, WARNING, ERROR or CRITICAL.
runs:
using: "composite"
steps:
- name: display the gh cli version
run: gh --version
shell: bash
- uses: actions/checkout@v4.2.2
with:
ref: main
- name: set branch name
run: |
echo "DEPENDENCIES_LABEL=dependencies" >> $GITHUB_ENV
echo "GO_LABEL=go" >> $GITHUB_ENV
echo "GOMOD_GO_VERSION_UPDATER_LABEL=gomod-go-version-updater" >> $GITHUB_ENV
echo "GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH=update-go-version-in-go-mod-file" >> $GITHUB_ENV
shell: bash
- uses: actions/setup-python@v5.4.0
with:
python-version: 3.9.18
- name: Install dependencies
run: |
pip install requests
shell: bash
- name: Create labels
run: |
check_label_exists() {
local label_name="$1"
LABEL_EXISTS=$(
gh label list --json name |
jq -r '
.[] |
select(.name == "'"$label_name"'") |
.name
'
)
if [ -z "$LABEL_EXISTS" ]; then
echo "label: '${label_name}' does NOT exist"
return 1
fi
}
if ! check_label_exists ${DEPENDENCIES_LABEL}; then
gh label create "${DEPENDENCIES_LABEL}" \
--color "#0366d6" \
--description "Pull requests that update a dependency file"
fi
if ! check_label_exists ${GO_LABEL}; then
gh label create "${GO_LABEL}" \
--color "#16e2e2" \
--description "Pull requests that update Go code"
fi
if ! check_label_exists ${GOMOD_GO_VERSION_UPDATER_LABEL}; then
gh label create "${GOMOD_GO_VERSION_UPDATER_LABEL}" \
--color "#F50BAB" \
--description "Pull requests that update Go version in the go.mod file"
fi
shell: bash
env:
GH_TOKEN: ${{ github.token }}
- name: update go version and push branch
run: |
git fetch -p -P
if (git ls-remote --exit-code --heads origin refs/heads/${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH}); then
echo "Branch '${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH}' already exists."
git checkout ${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH}
else
git checkout -b ${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH}
fi
python --version
python3 --version
if [ -n "${{ inputs.gomod-go-version-updater-action-log-level }}" ]; then
export GOMOD_GO_VERSION_UPDATER_ACTION_LOGGING_LEVEL=${{ inputs.gomod-go-version-updater-action-log-level }}
echo "GOMOD_GO_VERSION_UPDATER_ACTION_LOGGING_LEVEL: ${GOMOD_GO_VERSION_UPDATER_ACTION_LOGGING_LEVEL}"
fi
export GOMOD_GO_VERSION_UPDATER_ACTION_OUTPUT=$(python ${{ github.action_path }}/main.py 2>&1)
echo "GOMOD_GO_VERSION_UPDATER_ACTION_OUTPUT: ${GOMOD_GO_VERSION_UPDATER_ACTION_OUTPUT}"
export GOMOD_GO_VERSION_UPDATER_ACTION_NEW_GOLANG_VERSION_OUTPUT=$(echo ${GOMOD_GO_VERSION_UPDATER_ACTION_OUTPUT} |\
grep "bump golang version" |\
sed "s|.*\(bump.*\)|\1|" |\
head -n 1)
echo "GOMOD_GO_VERSION_UPDATER_ACTION_NEW_GOLANG_VERSION_OUTPUT: ${GOMOD_GO_VERSION_UPDATER_ACTION_NEW_GOLANG_VERSION_OUTPUT}"
export GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE="build(deps): ${GOMOD_GO_VERSION_UPDATER_ACTION_NEW_GOLANG_VERSION_OUTPUT}"
echo "GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE: ${GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE}"
if [ -n "$(git status --porcelain)" ]; then echo "There are uncommitted changes."; else echo "No changes to commit." && exit 0; fi
go_mod_file="go.mod"
echo "adding ${go_mod_file} file..."
[ -f "${go_mod_file}" ] && git add "${go_mod_file}"
echo "adding nested Dockerfiles..."
find . -name 'Dockerfile' -exec git add {} +
echo "git config user..."
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
echo "git commit..."
if ! git commit -m "${GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE}"; then git commit --amend --no-edit; fi
echo "git push..."
git push origin ${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH} -f
if gh pr list --json title | jq -e '.[] | select(.title | test("build\\(deps\\): bump golang version from .* to .*"))'; then
echo "PR exists already. Updating the 'title' and 'description'..."
gh pr edit ${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH} \
--body "${GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE}" \
--title "${GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE}"
exit 0
fi
labels=("${DEPENDENCIES_LABEL}" "${GO_LABEL}" "${GOMOD_GO_VERSION_UPDATER_LABEL}")
if [ -n "${{ inputs.extra-pr-label }}" ]; then
labels+=("${{ inputs.extra-pr-label }}")
fi
echo "Labels:"
for label in "${labels[@]}"; do
echo "'$label'"
done
echo "creating pr..."
label_args=()
for label in "${labels[@]}"; do
label_args+=(--label "$label")
done
gh pr create \
--base main \
--body "${GOMOD_GO_VERSION_UPDATER_ACTION_MESSAGE}" \
--fill \
--head "${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH}" \
"${label_args[@]}"
shell: bash
env:
GH_TOKEN: ${{ github.token }}
- uses: actions/setup-go@v5.3.0
with:
go-version-file: "go.mod"
cache: false
- run: |
git config --global url.https://${{ inputs.github-token-for-downloading-private-go-modules }}@github.com/.insteadOf https://github.com/
shell: bash
if: ${{ inputs.github-token-for-downloading-private-go-modules != '' }}
- name: go mod tidy
run: |
go mod tidy
shell: bash
- name: commit and force push if needed
run: |
if [ -n "$(git status --porcelain)" ]; then echo "There are uncommitted changes."; else echo "No changes to commit." && exit 0; fi
git add .
git commit --amend --no-edit
git push origin ${GOMOD_GO_VERSION_UPDATER_ACTION_BRANCH} -f
shell: bash
env:
GH_TOKEN: ${{ github.token }}