Codex로 수정
This commit is contained in:
@@ -34,13 +34,32 @@ jobs:
|
||||
BASE_GITEA_USER: ${{ vars.BASE_GITEA_USER }} # The user who owns the token
|
||||
INPUT_BRANCHES: ${{ github.event.inputs.branches }}
|
||||
run: |
|
||||
set -e # Exit immediately if a command exits with a non-zero status.
|
||||
set -euo pipefail
|
||||
|
||||
# Gitea API Header
|
||||
CENTER_ORG="center_dev"
|
||||
AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}"
|
||||
|
||||
set_default_branch_main() {
|
||||
local repo_name="$1"
|
||||
local response http_status body
|
||||
response=$(curl -s -w "\n%{http_code}" -X PATCH -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}")
|
||||
http_status=$(echo "${response}" | tail -n1)
|
||||
body=$(echo "${response}" | sed '$d')
|
||||
|
||||
if [[ "${http_status}" != "200" ]]; then
|
||||
echo "::warning::Failed to set default branch to 'main' for ${CENTER_ORG}/${repo_name} (status ${http_status})"
|
||||
if [[ -n "${body}" ]]; then
|
||||
echo "${body}"
|
||||
fi
|
||||
else
|
||||
echo "Default branch set to 'main' for ${CENTER_ORG}/${repo_name}"
|
||||
fi
|
||||
}
|
||||
|
||||
process_branch() {
|
||||
local branch_name=$1
|
||||
local branch_name
|
||||
branch_name="$(echo "$1" | xargs)" # trim whitespace
|
||||
|
||||
if [[ -z "$branch_name" ]]; then
|
||||
return
|
||||
fi
|
||||
@@ -51,18 +70,21 @@ jobs:
|
||||
repo_name=""
|
||||
if [ "${branch_name}" == "Develop_Net8" ]; then
|
||||
repo_name="base"
|
||||
elif [[ "${branch_name}" == Develop_Net8_* ]]; then
|
||||
repo_name="${branch_name#Develop_Net8_}"
|
||||
else
|
||||
repo_name=$(echo "${branch_name}" | sed 's/^Develop_Net8_//')
|
||||
repo_name="${branch_name}"
|
||||
fi
|
||||
|
||||
|
||||
echo "Target repository name: ${repo_name}"
|
||||
|
||||
# Check if repository exists on Gitea
|
||||
http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/center_dev/${repo_name}")
|
||||
repo_exists=false
|
||||
http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}")
|
||||
|
||||
if [ "${http_status}" == "404" ]; then
|
||||
echo "Repository 'center_dev/${repo_name}' does not exist. Creating it..."
|
||||
create_repo_response=$(curl -s -w "%{http_code}" -X POST -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"name\":\"${repo_name}\",\"private\":true}" "${BASE_GITEA_URL}/api/v1/orgs/center_dev/repos")
|
||||
create_repo_response=$(curl -s -w "%{http_code}" -X POST -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"name\":\"${repo_name}\",\"private\":true,\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/orgs/${CENTER_ORG}/repos")
|
||||
create_status=$(echo "${create_repo_response}" | tail -c 3)
|
||||
if [[ "${create_status}" -ne "201" ]]; then
|
||||
echo "::error::Failed to create repository. API response:"
|
||||
@@ -75,30 +97,60 @@ jobs:
|
||||
exit 1
|
||||
else
|
||||
echo "Repository 'center_dev/${repo_name}' already exists."
|
||||
repo_exists=true
|
||||
fi
|
||||
|
||||
# Define remote URL's hostname, stripping protocol
|
||||
GITEA_HOSTNAME=$(echo "${BASE_GITEA_URL}" | sed -e 's,^\(https*://\)\\,\\",g')
|
||||
GITEA_REMOTE="https://${BASE_GITEA_USER}:${BASE_GITEA_TOKEN}@${GITEA_HOSTNAME}/center_dev/${repo_name}.git"
|
||||
GITEA_HOSTNAME=$(echo "${BASE_GITEA_URL}" | sed -e 's~^https*://~~' -e 's~/$~~')
|
||||
GITEA_REMOTE="https://${BASE_GITEA_USER}:${BASE_GITEA_TOKEN}@${GITEA_HOSTNAME}/${CENTER_ORG}/${repo_name}.git"
|
||||
SOURCE_REPO="ssh://engdev@172.16.42.118/dev_Net8.git"
|
||||
|
||||
# Create a temporary directory for cloning
|
||||
CLONE_DIR=$(mktemp -d)
|
||||
echo "Cloning source into ${CLONE_DIR}..."
|
||||
echo "Working directory: ${CLONE_DIR}"
|
||||
|
||||
git clone --bare "${SOURCE_REPO}" "${CLONE_DIR}"
|
||||
|
||||
cd "${CLONE_DIR}"
|
||||
if ${repo_exists}; then
|
||||
echo "Cloning existing target repository for update..."
|
||||
if ! git clone --mirror "${GITEA_REMOTE}" "${CLONE_DIR}"; then
|
||||
echo "::error::Failed to clone existing target repository ${GITEA_REMOTE}"
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
cd "${CLONE_DIR}"
|
||||
git remote add source "${SOURCE_REPO}"
|
||||
echo "Fetching latest branch '${branch_name}' from source..."
|
||||
if ! git fetch source "+refs/heads/${branch_name}:refs/heads/${branch_name}"; then
|
||||
echo "::error::Failed to fetch branch '${branch_name}' from source repo"
|
||||
cd ..
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
else
|
||||
echo "Cloning source repository for first-time push..."
|
||||
if ! git clone --mirror "${SOURCE_REPO}" "${CLONE_DIR}"; then
|
||||
echo "::error::Failed to clone source repository ${SOURCE_REPO}"
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
cd "${CLONE_DIR}"
|
||||
fi
|
||||
|
||||
git remote set-url origin "${GITEA_REMOTE}"
|
||||
|
||||
echo "Pushing '${branch_name}' to Gitea repository '${repo_name}'..."
|
||||
# Push the specific branch to the main branch of the target repository
|
||||
git push --force "${GITEA_REMOTE}" "refs/heads/${branch_name}:refs/heads/main"
|
||||
if ! git push --force origin "refs/heads/${branch_name}:refs/heads/main"; then
|
||||
echo "::error::Failed to push branch '${branch_name}' to target repository"
|
||||
cd ..
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
|
||||
# Cleanup
|
||||
cd ..
|
||||
rm -rf "${CLONE_DIR}"
|
||||
|
||||
echo "Successfully mirrored ${branch_name} to center_dev/${repo_name}"
|
||||
set_default_branch_main "${repo_name}"
|
||||
echo "================================================="
|
||||
echo ""
|
||||
}
|
||||
@@ -117,4 +169,4 @@ jobs:
|
||||
while IFS= read -r branch_name || [[ -n "$branch_name" ]]; do
|
||||
process_branch "${branch_name}"
|
||||
done < branch_list
|
||||
fi
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user