#!/usr/bin/env bash function _log() { local timestamp=$(date +'%Y-%m-%d %H:%M.%S') local SET_COLOR='\033[32m' local CLEAR_COLOR='\033[0m' printf "$SET_COLOR%s \$$CLEAR_COLOR %s\n" "$timestamp" "$*" } CUR_DIR=$(cd $(dirname $0); pwd) # 兼容工蜂私钥场景 if [ "${PLUGIN_GIT_TOKEN}" ]; then export PLUGIN_GIT_USERNAME=private export PLUGIN_GIT_PASSWORD=${PLUGIN_GIT_TOKEN} fi if [[ -z "${PLUGIN_REPO_SLUG}" ]]; then export PLUGIN_REPO_SLUG=${ORANGE_REPO_SLUG} fi if [[ -z "${PLUGIN_GIT_PROTOCOL}" ]]; then export PLUGIN_GIT_PROTOCOL="https" fi # 检查变量是否设置 if [[ -z "${PLUGIN_BUILD_USER}" ]]; then echo "env PLUGIN_BUILD_USER not set" exit 1 fi if [[ -z "${PLUGIN_TARGET_URL}" ]]; then echo "env PLUGIN_TARGET_URL not set" exit 1 fi # 设置git凭证 if [ "${PLUGIN_GIT_USERNAME}" ] && [ "${PLUGIN_GIT_PASSWORD}" ] then echo "use git username/password" # 如果传的是ssh私钥 elif [ "${PLUGIN_GIT_SSH_PRIVATE_KEY}" ] then mkdir -p /root/.ssh echo -e "${PLUGIN_GIT_SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa chmod 0600 /root/.ssh/id_rsa envsubst < /root/.ssh/config-generator > /root/.ssh/config chmod 600 ~/.ssh/config ssh-keyscan ${PLUGIN_GIT_DOAMIN} > ~/.ssh/known_hosts echo "use git ssh private key" else echo "no git credentials" exit 1 fi mkdir -p "${PLUGIN_GIT_CACHE}" envsubst < /root/.gitconfig-generator-${PLUGIN_GIT_PROTOCOL} > /root/.gitconfig git config --list mkdir -p /yyds/${PLUGIN_WORKSPACE_ID}/upper mkdir -p /yyds/${PLUGIN_WORKSPACE_ID}/work mkdir -p /yyds/${PLUGIN_WORKSPACE_ID}/${PLUGIN_WORKSPACE_NAME} ln -fs /yyds/${PLUGIN_WORKSPACE_ID}/${PLUGIN_WORKSPACE_NAME} /workspace upperdir="/yyds/${PLUGIN_WORKSPACE_ID}/upper" workdir="/yyds/${PLUGIN_WORKSPACE_ID}/work" merged="/yyds/${PLUGIN_WORKSPACE_ID}/${PLUGIN_WORKSPACE_NAME}" rm -rf "/tmp/CHECKOUT_IN_COW" # 加锁填充/更新缓存 _log "flock -xon \"${PLUGIN_GIT_CACHE}\" ${CUR_DIR}/git-clone" flock -xon "${PLUGIN_GIT_CACHE}" ${CUR_DIR}/git-clone if ! [ -e "/tmp/FETCHED_IN_CLONE" ]; then # 等待缓存填充完毕,超时退出 _log "flock -xo -w500 \"${PLUGIN_GIT_CACHE}\" echo done" flock -xo -w500 "${PLUGIN_GIT_CACHE}" echo done ret=$?; [ $ret -ne 0 ] && exit $ret; fi _log "copy-on-write for ${merged}" mount -t overlay git-clone-yyds -o lowerdir="${PLUGIN_GIT_CACHE}",upperdir="${upperdir}",workdir="${workdir}",index=off ${merged} ret=$?; [ $ret -ne 0 ] && ! [ "$PLUGIN_IS_RETRY" = "true" ] && exit $ret; touch "/tmp/CHECKOUT_IN_COW" # 在复制后的副本上进行后续操作 _log "cd ${merged} && ${CUR_DIR}/git-checkout" cd "${merged}" && ${CUR_DIR}/git-checkout ret=$?; [ $ret -ne 0 ] && exit $ret; _log "done"