|
| 1 | +# File: .github/workflows/codeberg-mirror.yml |
| 2 | +name: Codeberg Mirror |
| 3 | + |
| 4 | +# Trigger on any push, manual dispatch, and hourly schedule to catch e.g. tags or force-pushes |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - '**' |
| 9 | + |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | + repository_dispatch: |
| 13 | + types: [trigger-workflow] |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + id-token: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + mirror: |
| 21 | + name: Mirror commits & tags to Codeberg |
| 22 | + runs-on: ubuntu-latest |
| 23 | + env: |
| 24 | + CODEBERG_SSH_URL: ${{ vars.CODEBERG_SSH_URL }} |
| 25 | + SSH_KEY: ${{ secrets.CODEBERG_SSH_PRIVATE_KEY }} |
| 26 | + ACTIONS_STEP_DEBUG: true |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Skip if mirror-back from Codeberg |
| 30 | + run: | |
| 31 | + if [[ "$(git log -1 --pretty=format:'%an')" == "Codeberg-mirror" ]]; then |
| 32 | + echo "Commit is from Codeberg mirror. Skipping sync." |
| 33 | + exit 0 |
| 34 | + fi |
| 35 | + |
| 36 | + - name: Validate inputs |
| 37 | + run: | |
| 38 | + if [[ -z "${CODEBERG_SSH_URL}" ]]; then |
| 39 | + echo "ERROR: CODEBERG_SSH_URL is not set as a repo variable." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + if [[ -z "${SSH_KEY}" ]]; then |
| 43 | + echo "ERROR: CODEBERG_SSH_PRIVATE_KEY is not set in secrets." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +
|
| 47 | + - name: Install SSH key |
| 48 | + run: | |
| 49 | + mkdir -p ~/.ssh |
| 50 | + echo "$SSH_KEY" > ~/.ssh/id_ed25519 |
| 51 | + chmod 600 ~/.ssh/id_ed25519 |
| 52 | + printf "Host codeberg.org\n StrictHostKeyChecking no\n" > ~/.ssh/config |
| 53 | +
|
| 54 | + - name: Checkout all history |
| 55 | + |
| 56 | + with: |
| 57 | + fetch-depth: 0 # full history, needed for tags & all branches |
| 58 | + |
| 59 | + - name: Configure Git |
| 60 | + run: | |
| 61 | + git config user.name "Codeberg-mirror" |
| 62 | + git config user.email "[email protected]" |
| 63 | +
|
| 64 | + - name: Add Codeberg remote |
| 65 | + run: | |
| 66 | + git remote remove codeberg || true |
| 67 | + git remote add codeberg "$CODEBERG_SSH_URL" |
| 68 | +
|
| 69 | + - name: Push triggering branch |
| 70 | + run: | |
| 71 | + echo "Pushing branch: ${{ github.ref_name }}" |
| 72 | + git push codeberg ${{ github.ref_name }} --force |
| 73 | +
|
| 74 | + - name: Push all tags |
| 75 | + run: | |
| 76 | + git push codeberg --tags --force |
| 77 | +
|
| 78 | + - name: Verify remote state |
| 79 | + run: | |
| 80 | + echo "Listing Codeberg branches:" |
| 81 | + git ls-remote --heads codeberg |
| 82 | +
|
| 83 | + - name: Notify on failure |
| 84 | + if: failure() |
| 85 | + run: | |
| 86 | + echo "::error ::Mirror to Codeberg failed. Please check the Actions log." |
0 commit comments