This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
175 lines (152 loc) · 5.44 KB
/
saveweb-go.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: ko-built saveweb Images
on:
push:
branches:
- 'master'
paths:
- '.github/workflows/saveweb-go.yml'
schedule:
- cron: "20 * * * *"
workflow_dispatch:
env:
REGISTRY_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/saveweb
jobs:
ko-build-images:
name: build images with ko-build
runs-on: ubuntu-24.04
steps:
- name: Fetch the latest tag
shell: bash
run: |
set -eo pipefail
curl() { $(type -P curl) -LRq --retry 5 --retry-delay 10 --retry-max-time 60 --fail "$@"; }
cnblogs_repo='git.saveweb.org/saveweb/cnblogs'
cnblogs_latest_tag=$(curl -L -- "https://${cnblogs_repo%%.git}/tags/list" \
| jq -r .results[] | sort -Vr | head -n 1)
huashijie_go_repo='git.saveweb.org/saveweb/huashijie_go'
huashijie_go_latest_tag=$(curl -L -- "https://${huashijie_go_repo%%.git}/tags/list" \
| jq -r .results[] | sort -Vr | head -n 1)
for _var in \
cnblogs_latest_tag \
huashijie_go_latest_tag \
; do
if [[ -z "${!_var}" ]]; then
echo "FATAL: ${_var} is empty!"
exit 1
fi
echo "${_var}=${!_var}" >> "$GITHUB_ENV"
done
echo "cnblogs_repo=${cnblogs_repo%%.git}" >> "$GITHUB_ENV"
echo "huashijie_go_repo=${huashijie_go_repo%%.git}" >> "$GITHUB_ENV"
curl -o '.ko.yaml' -- \
https://raw.githubusercontent.com/saveweb/saveweb-images/master/prod.ko.yaml
- name: Clone the git repos
shell: bash
run: |
set -eo pipefail
git_clone() {
git clone -j "$(nproc)" \
--no-tags --shallow-submodules --recurse-submodules --depth 1 --single-branch \
"$@";
}
clone_with_ref_and_ko_conf() {
if [ "$#" -ne 2 ]; then
echo "Usage: clone_with_ref_and_ko_conf <git_repo> <ref>"
return 1
fi
local git_repo="$1"
local ref="$2"
for _var in git_repo ref; do
if [[ -z "${!_var}" ]]; then
echo "FATAL: ${_var} is empty!"
exit 1
fi
done
local repo_dir="${git_repo##*/}"
git_clone --branch "$ref" -- "$git_repo" || exit 1
cp .ko.yaml "./${repo_dir%%.git}/"
}
export -f git_clone
export -f clone_with_ref_and_ko_conf
clone_project() {
local project="$1"
local tag_varname="${project}_latest_tag"
local repo_varname="${project}_repo"
clone_with_ref_and_ko_conf \
"https://${!repo_varname}.git" \
"${!tag_varname}"
}
export -f clone_project
main() {
local projects=(
cnblogs
huashijie_go
)
printf '%s\0' "${projects[@]}" \
| xargs -0 -r -I {} -P 2 bash -c 'clone_project "{}"'
}
set -x
pwd
main
- uses: actions/setup-go@v5
with:
go-version: 'stable'
check-latest: true
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: ko-build/[email protected]
env:
KO_DOCKER_REPO: docker.io/${{ env.REGISTRY_IMAGE }}
- name: Build the container images
shell: bash
run: |
set -eo pipefail
ko-build() {
# check whether the script is executed inside a git repo
if [[ "$(git rev-parse --is-inside-work-tree)" != 'true' ]]; then
echo "FATAL: this script MUST be executed inside a git repo."
exit 1
fi
if [ "$#" -ne 2 ]; then
echo "Usage: ko-build <cmd_dir> <project>"
return 1
fi
local cmd_dir="$1"
local project="$2"
for _var in cmd_dir project; do
if [[ -z "${!_var}" ]]; then
echo "FATAL: ${_var} is empty!"
exit 1
fi
done
ko build "${cmd_dir}/..." --platform=linux/amd64,linux/arm64 --bare --tags "${project}" || exit 1
rm -f ./.ko.yaml
ko build "${cmd_dir}/..." --platform=linux/amd64,linux/arm64 --bare --tags "${project}-debug" --debug || exit 1
}
export -f ko-build
build_project() {
local project="$1"
local repo_varname="${project}_repo"
local repo_dir="${!repo_varname##*/}"
(
set -eo pipefail
# ugly fix
[[ "$project" = 'huashijie_go' ]] && project='huashijie'
cd "./${repo_dir}/" || exit 1
ko-build './cmd/' "${project}"
)
}
export -f build_project
main() {
local projects=(
cnblogs
huashijie_go
)
printf '%s\0' "${projects[@]}" \
| xargs -0 -r -I {} -P 2 bash -c 'build_project "{}"'
}
set -x
pwd
main