Dough Git

clove/doughGit

.github/workflows/mirror.yml

back to .github/workflows

name: Mirror

on:
  push:

jobs:
  mirror:
    runs-on: ubuntu-latest

    permissions:
      contents: read

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

    env:
      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

          targetUrl="https://git.doughmination.gay/clove/doughGit.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="clove"; echo password=\"\$MIRROR_TOKEN\"; }; f" \
              push --mirror mirror