120 lines
4.8 KiB
YAML
120 lines
4.8 KiB
YAML
name: Git Repository Mirroring
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
branches:
|
|
description: 'Comma-separated list of branches to mirror (e.g., Develop_Net8,Develop_Net8_box). If empty, all branches from branch_list file will be mirrored.'
|
|
required: false
|
|
default: ''
|
|
schedule:
|
|
- cron: '0 2 * * *' # Runs every day at 2:00 AM
|
|
|
|
jobs:
|
|
mirror:
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 360 # 6 hours timeout
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up SSH
|
|
env:
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
ssh-keyscan -p 22 172.16.42.118 >> ~/.ssh/known_hosts
|
|
|
|
- name: Mirror Branches
|
|
env:
|
|
BASE_GITEA_TOKEN: ${{ secrets.BASE_GITEA_TOKEN }}
|
|
BASE_GITEA_URL: ${{ vars.BASE_GITEA_URL }} # e.g., https://gitea.example.com
|
|
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.
|
|
|
|
# Gitea API Header
|
|
AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}"
|
|
|
|
process_branch() {
|
|
local branch_name=$1
|
|
if [[ -z "$branch_name" ]]; then
|
|
return
|
|
fi
|
|
|
|
echo "================================================="
|
|
echo "Processing branch: ${branch_name}"
|
|
|
|
repo_name=""
|
|
if [ "${branch_name}" == "Develop_Net8" ]; then
|
|
repo_name="base"
|
|
else
|
|
repo_name=$(echo "${branch_name}" | sed 's/^Develop_Net8_//')
|
|
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}")
|
|
|
|
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_status=$(echo "${create_repo_response}" | tail -c 3)
|
|
if [[ "${create_status}" -ne "201" ]]; then
|
|
echo "::error::Failed to create repository. API response:"
|
|
echo "${create_repo_response}" | head -c -3
|
|
exit 1
|
|
fi
|
|
echo "Repository created successfully."
|
|
elif [ "${http_status}" != "200" ]; then
|
|
echo "::error::Error checking repository. HTTP status: ${http_status}"
|
|
exit 1
|
|
else
|
|
echo "Repository 'center_dev/${repo_name}' already exists."
|
|
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"
|
|
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}..."
|
|
|
|
git clone --bare "${SOURCE_REPO}" "${CLONE_DIR}"
|
|
|
|
cd "${CLONE_DIR}"
|
|
|
|
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"
|
|
|
|
# Cleanup
|
|
cd ..
|
|
rm -rf "${CLONE_DIR}"
|
|
|
|
echo "Successfully mirrored ${branch_name} to center_dev/${repo_name}"
|
|
echo "================================================="
|
|
echo ""
|
|
}
|
|
|
|
if [[ -n "${INPUT_BRANCHES}" ]]; then
|
|
echo "Processing manually specified branches: ${INPUT_BRANCHES}"
|
|
# Split comma-separated string into an array
|
|
IFS=',' read -r -a branches_to_process <<< "${INPUT_BRANCHES}"
|
|
for branch in "${branches_to_process[@]}"; do
|
|
# Trim whitespace
|
|
trimmed_branch=$(echo "$branch" | xargs)
|
|
process_branch "${trimmed_branch}"
|
|
done
|
|
else
|
|
echo "Processing all branches from branch_list file."
|
|
while IFS= read -r branch_name || [[ -n "$branch_name" ]]; do
|
|
process_branch "${branch_name}"
|
|
done < branch_list
|
|
fi |