forked from k8scat/action-mirror-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.sh
executable file
·270 lines (240 loc) · 7.55 KB
/
mirror.sh
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
# Created by K8sCat <[email protected]>
# Modified by CynicalSloths <[email protected]>
set +e
PROTOCOL_SSH="ssh"
PROTOCOL_HTTPS="https"
function init_workdir() {
WORKDIR="/data/repos"
mkdir -p ${WORKDIR}
cd ${WORKDIR} || exit 1
}
function init_functions() {
for f in /mirror-git/functions/*; do
# shellcheck disable=SC1090
source "${f}"
done
}
function init_env() {
export INPUT_SOURCE_TOKEN_USERNAME="${INPUT_SOURCE_TOKEN_USERNAME:-${INPUT_SOURCE_USERNAME}}"
export INPUT_SOURCE_PROTOCOL="${INPUT_SOURCE_PROTOCOL:-https}"
export INPUT_SOURCE_HOST="${INPUT_SOURCE_HOST:-github.com}"
export INPUT_DEST_TOKEN_USERNAME="${INPUT_DEST_TOKEN_USERNAME:-${INPUT_DEST_USERNAME}}"
export INPUT_DEST_PROTOCOL="${INPUT_DEST_PROTOCOL:-https}"
export INPUT_DEST_HOST="${INPUT_DEST_HOST:-github.com}"
export INPUT_PUSH_TAGS="${INPUT_PUSH_TAGS:-true}"
export INPUT_PUSH_BRANCH_ONLY="${INPUT_PUSH_BRANCH_ONLY:-false}"
export INPUT_PUSH_BRANCH_NAME="${INPUT_PUSH_BRANCH_NAME:-main}"
export INPUT_NOTIFY_PREFIX="${INPUT_NOTIFY_PREFIX:-Mirror Git}"
export INPUT_NOTIFY_SUFFIX="${INPUT_NOTIFY_SUFFIX:-Powered by https://github.com/CynicalSloths/action-mirror-git}"
export INPUT_IGNORE_ERROR="${INPUT_IGNORE_ERROR:-false}"
}
function init_git() {
git config --global http.postBuffer 524288000
}
function init_token_username() {
if [[ -z "${INPUT_SOURCE_TOKEN_USERNAME}" ]]; then
export INPUT_SOURCE_TOKEN_USERNAME="${INPUT_SOURCE_USERNAME}"
fi
if [[ -z "${INPUT_DEST_TOKEN_USERNAME}" ]]; then
export INPUT_DEST_TOKEN_USERNAME="${INPUT_DEST_USERNAME}"
fi
}
function init_scripts() {
# create_repo
if [[ -n "${INPUT_DEST_CREATE_REPO_SCRIPT}" ]]; then
if [[ $(echo "${INPUT_DEST_CREATE_REPO_SCRIPT}" | wc -l) -eq 1 && "${INPUT_DEST_CREATE_REPO_SCRIPT}" =~ "http".*"://".* ]]; then
if ! curl -L "${INPUT_DEST_CREATE_REPO_SCRIPT}" -o /usr/bin/create_repo; then
notify "Download create_repo script failed"
exit 1
fi
else
echo "${INPUT_DEST_CREATE_REPO_SCRIPT}" > /usr/bin/create_repo
fi
chmod +x /usr/bin/create_repo
fi
}
# message
function init_message() {
if [[ -n "${INPUT_SLACK_WEBHOOK}" ]]; then
export SLACK_WEBHOOK="${INPUT_SLACK_WEBHOOK}"
fi
if [[ -n "${INPUT_DINGTALK_WEBHOOK}" ]]; then
export DINGTALK_WEBHOOK="${INPUT_DINGTALK_WEBHOOK}"
fi
if [[ -n "${INPUT_LARK_WEBHOOK}" ]]; then
export LARK_WEBHOOK="${INPUT_LARK_WEBHOOK}"
fi
}
function notify() {
local msg="${1}"
echo "notify: ${msg}"
if [[ -n "${INPUT_NOTIFY_PREFIX}" ]]; then
msg="[${INPUT_NOTIFY_PREFIX}] ${msg}"
fi
if [[ -n "${GITHUB_SERVER_URL}" && -n "${GITHUB_REPOSITORY}" && -n "${GITHUB_RUN_ID}" ]]; then
local run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
msg="${msg}\n\nrun_url: ${run_url}"
fi
if [[ -n "${INPUT_NOTIFY_SUFFIX}" ]]; then
msg="${msg}\n\n${INPUT_NOTIFY_SUFFIX}"
fi
if [[ -n "${SLACK_WEBHOOK}" ]]; then
slack_notify "${msg}"
fi
if [[ -n "${DINGTALK_WEBHOOK}" ]]; then
dingtalk_notify "${msg}"
fi
if [[ -n "${LARK_WEBHOOK}" ]]; then
lark_notify "${msg}"
fi
return 0
}
function write_ssh_config() {
type=$1
host=$2
port=$3
username=$4
private_key=$5
local privkey_file="${SSH_DIR}/id_rsa_${username}"
echo "${private_key}" > "${privkey_file}"
chmod 0600 "${privkey_file}"
new_host="${type}.${username}.${host}"
cat >> "${SSH_DIR}/config" <<EOF
Host ${new_host}
HostName ${host}
User git
Port ${port:-22}
IdentityFile ${privkey_file}
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
EOF
echo "${new_host}"
}
function init_ssh() {
SSH_DIR="${HOME}/.ssh"
mkdir -p "${SSH_DIR}"
chmod 0700 "${SSH_DIR}"
if [[ "${INPUT_SOURCE_PROTOCOL}" = "${PROTOCOL_SSH}" ]]; then
INPUT_SOURCE_HOST=$(write_ssh_config "source" "${INPUT_SOURCE_HOST}" "${INPUT_SOURCE_PORT}" "${INPUT_SOURCE_USERNAME}" "${INPUT_SOURCE_PRIVATE_KEY}")
export INPUT_SOURCE_HOST
fi
if [[ "${INPUT_DEST_PROTOCOL}" = "${PROTOCOL_SSH}" ]]; then
INPUT_DEST_HOST=$(write_ssh_config "dest" "${INPUT_DEST_HOST}" "${INPUT_DEST_PORT}" "${INPUT_DEST_USERNAME}" "${INPUT_DEST_PRIVATE_KEY}")
export INPUT_DEST_HOST
fi
}
function gen_source_addr() {
if [[ "${INPUT_SOURCE_PROTOCOL}" = "${PROTOCOL_SSH}" ]]; then
source_addr="git@${INPUT_SOURCE_HOST}:${INPUT_SOURCE_USERNAME}/${INPUT_SOURCE_REPO_NAME}.git"
echo "${source_addr}"
return
fi
if [[ "${INPUT_SOURCE_PROTOCOL}" = "${PROTOCOL_HTTPS}" ]]; then
source_addr="https://${INPUT_SOURCE_TOKEN_USERNAME}:${INPUT_SOURCE_TOKEN}@${INPUT_SOURCE_HOST}"
if [[ -n "${INPUT_SOURCE_PORT}" ]]; then
source_addr="${source_addr}:${INPUT_SOURCE_PORT}"
fi
source_addr="${source_addr}/${INPUT_SOURCE_USERNAME}/${INPUT_SOURCE_REPO_NAME}.git"
echo "${source_addr}"
return
fi
notify "Unknown source protocol: ${INPUT_SOURCE_PROTOCOL}"
return 1
}
function gen_dest_addr() {
if [[ "${INPUT_DEST_PROTOCOL}" = "${PROTOCOL_SSH}" ]]; then
dest_addr="git@${INPUT_DEST_HOST}:${INPUT_DEST_USERNAME}/${INPUT_DEST_REPO_NAME}.git"
echo "${dest_addr}"
return
fi
if [[ "${INPUT_DEST_PROTOCOL}" = "${PROTOCOL_HTTPS}" ]]; then
dest_addr="https://${INPUT_DEST_TOKEN_USERNAME}:${INPUT_DEST_TOKEN}@${INPUT_DEST_HOST}"
if [[ -n "${INPUT_DEST_PORT}" ]]; then
dest_addr="${dest_addr}:${INPUT_DEST_PORT}"
fi
dest_addr="${dest_addr}/${INPUT_DEST_USERNAME}/${INPUT_DEST_REPO_NAME}.git"
echo "${dest_addr}"
return
fi
notify "Unknown dest protocol: ${INPUT_DEST_PROTOCOL}"
return 1
}
function mirror() {
IFS=","
# shellcheck disable=SC2076
cd ${WORKDIR} || return 1
if ! source_addr=$(gen_source_addr); then
exit 1
fi
echo "source_addr: ${source_addr}"
# Clone
if [[ "${INPUT_PUSH_BRANCH_ONLY}" = "true" ]]; then
if ! git clone -b "${INPUT_PUSH_BRANCH_NAME}" --single-branch --bare "${source_addr}" "${INPUT_SOURCE_REPO_NAME}"; then
notify "Failed to clone repo: ${source_addr}"
if [[ "${INPUT_IGNORE_ERROR}" = "true" ]]; then
return 1
fi
return 1
fi
else
if ! git clone --bare "${source_addr}" "${INPUT_SOURCE_REPO_NAME}"; then
notify "Failed to clone repo: ${source_addr}"
if [[ "${INPUT_IGNORE_ERROR}" = "true" ]]; then
return 1
fi
return 1
fi
fi
if [[ -n "${INPUT_DEST_CREATE_REPO_SCRIPT}" ]]; then
echo "Creating repo: ${INPUT_DEST_REPO_NAME}"
if ! create_repo; then
notify "Failed to create repo: ${INPUT_DEST_REPO_NAME}"
if [[ "${INPUT_IGNORE_ERROR}" = "true" ]]; then
return 1
fi
return 1
fi
fi
if ! dest_addr=$(gen_dest_addr); then
exit 1
fi
echo "dest_addr: ${dest_addr}"
repo_dir="${WORKDIR}/${INPUT_SOURCE_REPO_NAME}"
cd "${repo_dir}" || exit 1
if ! git push --all -f "${dest_addr}"; then
notify "Failed to push branches: ${dest_addr}"
if [[ "${INPUT_IGNORE_ERROR}" = "true" ]]; then
return 1
fi
return 1
fi
# shellcheck disable=SC2076
if [[ "${INPUT_PUSH_TAGS}" = "true" && ! "${INPUT_SKIP_TAGS_REPOS}" =~ ",${INPUT_DEST_REPO_NAME}," ]]; then
if ! git push --tags -f "${dest_addr}"; then
notify "Failed to push tags: ${dest_addr}"
if [[ "${INPUT_IGNORE_ERROR}" = "true" ]]; then
return 1
fi
return 1
fi
fi
}
function main() {
init_workdir
init_functions
init_env
init_git
init_token_username
init_scripts
init_message
init_ssh
notify "Mirror Git started"
if mirror; then
notify "Mirror Git finished"
else
notify "Mirror Git failed"
return 1
fi
}
main "$@"