Skip to content

Instantly share code, notes, and snippets.

@disafronov
Last active October 16, 2025 06:18
Show Gist options
  • Save disafronov/872f620fcf91d72b9c72647d3b0cc95b to your computer and use it in GitHub Desktop.
Save disafronov/872f620fcf91d72b9c72647d3b0cc95b to your computer and use it in GitHub Desktop.

Revisions

  1. disafronov revised this gist Oct 16, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mirror-source-branch.yaml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    name: Mirror source branch

    on:
    "on":
    schedule:
    - cron: '0 3 * * *'
    workflow_dispatch:
  2. disafronov created this gist Oct 16, 2025.
    45 changes: 45 additions & 0 deletions mirror-source-branch.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    name: Mirror source branch

    on:
    schedule:
    - cron: '0 3 * * *'
    workflow_dispatch:

    permissions:
    contents: write

    env:
    SOURCE_REPO: https://github.com/original-author/project.git
    SOURCE_NAME: source
    TARGET_BRANCH: source

    jobs:
    sync:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
    uses: actions/checkout@v4

    - name: Determine default branch of source
    id: default_branch
    run: |
    DEFAULT_BRANCH=$(git ls-remote --symref $SOURCE_REPO HEAD | awk '/^ref:/ {print $2}' | sed 's|refs/heads/||')
    echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT
    - name: Fetch default branch from source
    run: |
    DEFAULT_BRANCH=${{ steps.default_branch.outputs.default_branch }}
    REMOTE_REF=$SOURCE_NAME/$DEFAULT_BRANCH
    git remote add $SOURCE_NAME $SOURCE_REPO || true
    git fetch $SOURCE_NAME $DEFAULT_BRANCH:refs/remotes/$REMOTE_REF
    - name: Mirror to 'source' branch only if changes exist
    run: |
    DEFAULT_BRANCH=${{ steps.default_branch.outputs.default_branch }}
    REMOTE_REF=$SOURCE_NAME/$DEFAULT_BRANCH
    git switch -C $TARGET_BRANCH $REMOTE_REF
    if ! git diff --quiet origin/$TARGET_BRANCH; then
    git push origin $TARGET_BRANCH --force
    fi