Dough Git

clove/sandrone

.github/workflows/mirror.yml

back to .github/workflows

# go to my backup
name: Mirror

on:
  push:
    branches:
      - main

jobs:
  mirror:
    if: github.repository == 'doughmination/sandrone'
    runs-on: ubuntu-latest

    permissions:
      contents: read

    concurrency:
      group: mirror-${{ github.repository }}
      cancel-in-progress: false

    env:
      MIRROR_HOST: backup.doughmination.gay
      MIRROR_USERNAME: ${{ secrets.MIRROR_USERNAME }}
      MIRROR_REPO: ${{ secrets.MIRROR_REPO }}
      MIRROR_TOKEN: ${{ secrets.MIRROR_TOKEN }}

    steps:
      - name: Clone every ref
        env:
          SOURCE_TOKEN: ${{ github.token }}
          SERVER_URL: ${{ github.server_url }}
        run: |
          set -euo pipefail

          sourceHost="${SERVER_URL#https://}"

          git clone --mirror \
            "https://x-access-token:${SOURCE_TOKEN}@${sourceHost}/${GITHUB_REPOSITORY}.git" \
            source.git

      - name: Drop pull-request refs
        working-directory: source.git
        run: |
          set -euo pipefail

          git for-each-ref --format='%(refname)' 'refs/pull/*' \
            | while read -r ref; do
                git update-ref -d "$ref"
              done

      - name: Push mirror
        working-directory: source.git
        run: |
          set -euo pipefail

          if [ -z "$MIRROR_USERNAME" ]; then
            echo "MIRROR_USERNAME is not set" >&2
            exit 1
          fi

          if [ -z "$MIRROR_REPO" ]; then
            echo "MIRROR_REPO is not set" >&2
            exit 1
          fi

          targetUrl="https://${MIRROR_HOST}/${MIRROR_USERNAME}/${MIRROR_REPO}.git"

          echo "Mirroring ${GITHUB_REPOSITORY} -> ${targetUrl}"

          git remote add mirror "$targetUrl"

          # Credentials go through a helper so the token never lands in .git/config
          git -c credential.helper= \
              -c "credential.helper=!f() { echo username=\"\$MIRROR_USERNAME\"; echo password=\"\$MIRROR_TOKEN\"; }; f" \
              push --mirror mirror