How to mirror a git repo

Running in BKCI

./mirror-git-repo.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
#!/bin/bash

[[ "$IS_TRACE" == "true" ]] && set -x

set -eu

WORK_DIR="${WORKSPACE}/${BK_CI_BUILD_ID}"
mkdir -p ${WORK_DIR}
cd ${WORK_DIR}

# Create a bare clone of the origin repo
git clone --progress --bare ${ORIGIN_GIT_URL}

cd $(basename ${ORIGIN_GIT_URL})

# Mirror-push to the target repo
git push --force --progress --mirror https://${TARGET_GIT_USER_NAME}:${TARGET_GIT_ACCESS_TOKEN}@$(echo "${TARGET_GIT_URL}" | awk -F'^http[s]?://' '{print $2}')

if [ "$IS_ENABLE_GIT_LFS" == 'true' ]; then
git lfs install || exit 1
# Pull in the repo's Git lfs Storage objects.
git lfs fetch --all
# Push the repo's Git lfs objects to target repo.
git lfs push --all https://${TARGET_GIT_USER_NAME}:${TARGET_GIT_ACCESS_TOKEN}@$(echo "${TARGET_GIT_URL}" | awk -F'^http[s]?://' '{print $2}')
fi

setEnv "BK_CI_BUILD_REMARK" "${ORIGIN_GIT_URL} to ${TARGET_GIT_URL}"
作者

Sony Dog

发布于

2022-10-24

更新于

2023-12-26

许可协议

CC BY-NC-SA 4.0