| 1 | name: Sync to GitCode |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: [main] |
| 6 | release: |
| 7 | types: [published] |
| 8 | schedule: |
| 9 | - cron: "17,47 * * * *" |
| 10 | workflow_dispatch: {} |
| 11 | workflow_call: |
| 12 | inputs: |
| 13 | release_tag: |
| 14 | required: true |
| 15 | type: string |
| 16 | source_sha: |
| 17 | required: true |
| 18 | type: string |
| 19 | secrets: |
| 20 | GITCODE_TOKEN: |
| 21 | required: true |
| 22 | |
| 23 | concurrency: |
| 24 | group: gitcode-sync-${{ github.ref }} |
| 25 | cancel-in-progress: false |
| 26 | |
| 27 | permissions: |
| 28 | contents: read |
| 29 | |
| 30 | jobs: |
| 31 | sync: |
| 32 | if: >- |
| 33 | github.repository_owner == 'codewhale-hq' && |
| 34 | !(github.event_name == 'release' && github.event.release.prerelease) && |
| 35 | (!contains(fromJSON('["push","schedule","release"]'), github.event_name) || vars.GITCODE_MIRROR == 'enabled') |
| 36 | runs-on: ubuntu-24.04 |
| 37 | timeout-minutes: 30 |
| 38 | steps: |
| 39 | - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7 |
| 40 | with: |
| 41 | fetch-depth: 0 |
| 42 | ref: ${{ inputs.source_sha || github.sha }} |
| 43 | |
| 44 | - name: Push to GitCode |
| 45 | env: |
| 46 | GITCODE_TOKEN: ${{ secrets.GITCODE_TOKEN }} |
| 47 | GITCODE_USER: ${{ vars.GITCODE_USERNAME || 'HunterBown' }} |
| 48 | GITCODE_REPO: ${{ vars.GITCODE_REPOSITORY || 'HunterBown/Codewhale' }} |
| 49 | RELEASE_TAG: ${{ inputs.release_tag || github.event.release.tag_name }} |
| 50 | SOURCE_SHA: ${{ inputs.source_sha || github.sha }} |
| 51 | shell: bash |
| 52 | run: | |
| 53 | set -euo pipefail |
| 54 | |
| 55 | if [ -z "${GITCODE_TOKEN:-}" ]; then |
| 56 | echo "::error::GITCODE_TOKEN secret is not set; cannot push to GitCode." >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
| 60 | ENCODED_USER="$(printf '%s' "${GITCODE_USER}" | python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))')" |
| 61 | ENCODED_TOKEN="$(printf '%s' "${GITCODE_TOKEN}" | python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))')" |
| 62 | git remote add gitcode "https://${ENCODED_USER}:${ENCODED_TOKEN}@gitcode.com/${GITCODE_REPO}.git" |
| 63 | |
| 64 | if [[ -n "${RELEASE_TAG}" ]]; then |
| 65 | if ! [[ "${RELEASE_TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || |
| 66 | [[ "$(git rev-parse 'HEAD^{commit}')" != "${SOURCE_SHA}" ]]; then |
| 67 | echo "::error::GitCode release source does not match the frozen tag input." >&2 |
| 68 | exit 1 |
| 69 | fi |
| 70 | ./scripts/release/verify-remote-tag.sh \ |
| 71 | "https://github.com/${GITHUB_REPOSITORY}.git" "${RELEASE_TAG}" "${SOURCE_SHA}" |
| 72 | scripts/release/mirror-push.sh gitcode "tag ${RELEASE_TAG}" "refs/tags/${RELEASE_TAG}:refs/tags/${RELEASE_TAG}" |
| 73 | elif [[ "${GITHUB_REF}" != refs/heads/main ]]; then |
| 74 | echo "::error::GitCode mirrors main only; dispatch this workflow from main." >&2 |
| 75 | exit 1 |
| 76 | elif [[ "${GITHUB_EVENT_NAME}" == workflow_dispatch ]]; then |
| 77 | scripts/release/mirror-push.sh gitcode "main" HEAD:refs/heads/main --force |
| 78 | scripts/release/mirror-push.sh gitcode "release tags" "refs/tags/v*:refs/tags/v*" |
| 79 | else |
| 80 | scripts/release/mirror-push.sh gitcode "main" HEAD:refs/heads/main --force |
| 81 | fi |
| 82 | |
| 83 | - name: Mirror the latest release to GitCode |
| 84 | if: github.event_name == 'schedule' |
| 85 | env: |
| 86 | GH_TOKEN: ${{ github.token }} |
| 87 | shell: bash |
| 88 | run: | |
| 89 | set -euo pipefail |
| 90 | TAG="$(gh api "repos/${GITHUB_REPOSITORY}/releases/latest" --jq .tag_name)" |
| 91 | if [[ -z "$(git ls-remote --tags gitcode "refs/tags/${TAG}")" ]]; then |
| 92 | scripts/release/mirror-push.sh gitcode "tag ${TAG}" "refs/tags/${TAG}:refs/tags/${TAG}" |
| 93 | fi |
| 94 | ./scripts/release/verify-remote-tag.sh gitcode "${TAG}" "$(git rev-parse "refs/tags/${TAG}^{commit}")" |
| 95 |