Mirror All Branches to Organization #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Mirror All Branches to Organization | |
on: | |
push: | |
branches: | |
- '**' | |
schedule: | |
- cron: '0 * * * *' | |
workflow_dispatch: | |
jobs: | |
mirror: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.MIRROR_PAT }} | |
- name: Configure Git | |
run: | | |
git config --global user.name "${{ github.repository_owner }}" | |
git config --global user.email "${{ github.repository_owner }}@users.noreply.github.com" | |
- name: Fetch all branches | |
run: | | |
git fetch --all | |
- name: Set up mirror remote | |
run: | | |
git remote remove mirror 2>/dev/null || true | |
git remote add mirror "https://${{ github.repository_owner }}:${{ secrets.MIRROR_PAT }}@github.com/GD-LevelReqBot/gd-levelreqbot.git" | |
git fetch mirror --prune | |
- name: Process each branch | |
run: | | |
# Fetch all branches from the source and mirror remotes | |
git fetch --all | |
# Get the list of all branches from the source repository | |
for branch in $(git branch -r | grep '^origin/' | sed 's/origin\///'); do | |
echo "Processing branch: $branch" | |
# Create and switch to the local branch based on the remote branch | |
git checkout -B $branch origin/$branch | |
echo "Processing .mirrorignore (if exists)..." | |
if [ -f ".mirrorignore" ]; then | |
while IFS= read -r pattern || [ -n "$pattern" ]; do | |
[[ $pattern =~ ^[[:space:]]*$ || $pattern =~ ^# ]] && continue | |
echo "Removing files matching pattern: $pattern" | |
git rm -rf --cached "$pattern" 2>/dev/null || true | |
done < ".mirrorignore" | |
fi | |
echo "Removing mirror workflow files..." | |
git rm -rf --cached .github/workflows/mirror.yml 2>/dev/null || true | |
git rm -rf --cached .github/workflows/mirror_repo.yml 2>/dev/null || true | |
git commit -m "Mirror sync: Applied .mirrorignore filters" || true | |
# Push the branch to the mirror | |
echo "Pushing branch $branch to the mirror" | |
git push mirror $branch:$branch --force | |
done | |
# Handle branches that exist only in the mirror | |
echo "Checking for branches that exist only in the mirror..." | |
for mirror_branch in $(git branch -r | grep '^mirror/' | sed 's/mirror\///'); do | |
if ! git show-ref --verify --quiet refs/heads/$mirror_branch; then | |
echo "Branch $mirror_branch exists only in the mirror. Creating and pushing empty branch." | |
git checkout --orphan $mirror_branch | |
git rm -rf . | |
echo "This branch is a placeholder for $mirror_branch, created in the mirror only." > README.md | |
git add README.md | |
git commit -m "Create empty branch for $mirror_branch" | |
git push mirror $mirror_branch:$mirror_branch --force | |
fi | |
done | |
- name: Final cleanup | |
run: | | |
git reset --hard | |
git clean -fd |