57 lines
2.0 KiB
YAML
57 lines
2.0 KiB
YAML
name: Sync to mirror repository
|
|
run-name: ${{ gitea.actor }} is syncing to mirror 🔄
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
sync:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0 # Fetch all history for all branches
|
|
|
|
- name: Configure Git
|
|
run: |
|
|
git config --global user.name "${{ vars.GIT_USER_NAME }}"
|
|
git config --global user.email "${{ vars.GIT_USER_EMAIL }}"
|
|
|
|
- name: Add mirror remote and push
|
|
env:
|
|
GIT_USER_NAME: ${{ vars.GIT_USER_NAME }}
|
|
MIRROR_URL: ${{ secrets.MIRROR_REPO_URL }}
|
|
MIRROR_TOKEN: ${{ secrets.MIRROR_REPO_TOKEN }}
|
|
run: |
|
|
# Clean variables (remove potential whitespace/newlines)
|
|
CLEAN_MIRROR_URL=$(echo "$MIRROR_URL" | tr -d '[:space:]')
|
|
CLEAN_USER=$(echo "$GIT_USER_NAME" | tr -d '[:space:]')
|
|
CLEAN_TOKEN=$(echo "$MIRROR_TOKEN" | tr -d '[:space:]')
|
|
|
|
# URL encode special characters in credentials
|
|
ENCODED_USER=$(printf %s "$CLEAN_USER" | jq -sRr @uri)
|
|
ENCODED_TOKEN=$(printf %s "$CLEAN_TOKEN" | jq -sRr @uri)
|
|
|
|
# Build authenticated URL
|
|
if [[ "$CLEAN_MIRROR_URL" == https://* ]]; then
|
|
# URL with https protocol
|
|
CLEAN_URL="${CLEAN_MIRROR_URL#https://}"
|
|
AUTH_URL="https://${ENCODED_USER}:${ENCODED_TOKEN}@${CLEAN_URL}"
|
|
elif [[ "$CLEAN_MIRROR_URL" == http://* ]]; then
|
|
# URL with http protocol
|
|
CLEAN_URL="${CLEAN_MIRROR_URL#http://}"
|
|
AUTH_URL="http://${ENCODED_USER}:${ENCODED_TOKEN}@${CLEAN_URL}"
|
|
else
|
|
# URL without protocol
|
|
AUTH_URL="https://${ENCODED_USER}:${ENCODED_TOKEN}@${CLEAN_MIRROR_URL}"
|
|
fi
|
|
|
|
# Add mirror remote with authentication
|
|
git remote add mirror "$AUTH_URL"
|
|
|
|
# Push to mirror
|
|
git push --set-upstream mirror main --force
|