|
| 1 | +name: Mirror to GitLab |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "**" |
| 7 | + tags: |
| 8 | + - "**" |
| 9 | + release: |
| 10 | + types: [published, edited, deleted] |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + mirror-git: |
| 15 | + name: Mirror branches, tags, history and signatures |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Bare-mirror clone the source repo |
| 19 | + env: |
| 20 | + GH_TOKEN: ${{ github.token }} |
| 21 | + run: | |
| 22 | + set -euo pipefail |
| 23 | + git clone --mirror "https://x-access-token:${GH_TOKEN}@github.com/eskopp/JavaCrypt.git" source.git |
| 24 | +
|
| 25 | + - name: Push mirror to GitLab |
| 26 | + env: |
| 27 | + GITLAB_PAT: ${{ secrets.GITLAB_PAT }} |
| 28 | + run: | |
| 29 | + set -euo pipefail |
| 30 | + cd source.git |
| 31 | + # A bare --mirror clone only holds refs/heads/* and refs/tags/*, so |
| 32 | + # `push --mirror` from here is safe - no refs/remotes/* pollution like |
| 33 | + # there would be from actions/checkout. GPG commit/tag signatures are |
| 34 | + # part of the git objects themselves and travel along automatically. |
| 35 | + git push --mirror "https://oauth2:${GITLAB_PAT}@gitlab.erik-skopp.de/ags-erfurt/JavaCrypt.git" |
| 36 | +
|
| 37 | + mirror-releases: |
| 38 | + name: Mirror GitHub Releases to GitLab Releases |
| 39 | + runs-on: ubuntu-latest |
| 40 | + needs: mirror-git |
| 41 | + steps: |
| 42 | + - name: Sync releases via API |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ github.token }} |
| 45 | + GITLAB_PAT: ${{ secrets.GITLAB_PAT }} |
| 46 | + GITLAB_PROJECT: ags-erfurt%2FJavaCrypt |
| 47 | + run: | |
| 48 | + set -euo pipefail |
| 49 | +
|
| 50 | + releases=$(curl -s -H "Authorization: token $GH_TOKEN" \ |
| 51 | + "https://api.github.com/repos/eskopp/JavaCrypt/releases?per_page=100") |
| 52 | +
|
| 53 | + echo "$releases" | jq -c '.[]' | while read -r release; do |
| 54 | + tag=$(echo "$release" | jq -r '.tag_name') |
| 55 | + name=$(echo "$release" | jq -r '.name // .tag_name') |
| 56 | + body=$(echo "$release" | jq -r '.body // ""') |
| 57 | +
|
| 58 | + exists=$(curl -s -o /dev/null -w "%{http_code}" \ |
| 59 | + -H "PRIVATE-TOKEN: $GITLAB_PAT" \ |
| 60 | + "https://gitlab.erik-skopp.de/api/v4/projects/$GITLAB_PROJECT/releases/$tag") |
| 61 | +
|
| 62 | + payload=$(jq -n --arg tag "$tag" --arg name "$name" --arg desc "$body" \ |
| 63 | + '{tag_name: $tag, name: $name, description: $desc}') |
| 64 | +
|
| 65 | + if [ "$exists" = "200" ]; then |
| 66 | + echo "Updating GitLab release for $tag" |
| 67 | + curl -s -X PUT -H "PRIVATE-TOKEN: $GITLAB_PAT" -H "Content-Type: application/json" \ |
| 68 | + -d "$payload" \ |
| 69 | + "https://gitlab.erik-skopp.de/api/v4/projects/$GITLAB_PROJECT/releases/$tag" > /dev/null |
| 70 | + else |
| 71 | + echo "Creating GitLab release for $tag" |
| 72 | + curl -s -X POST -H "PRIVATE-TOKEN: $GITLAB_PAT" -H "Content-Type: application/json" \ |
| 73 | + -d "$payload" \ |
| 74 | + "https://gitlab.erik-skopp.de/api/v4/projects/$GITLAB_PROJECT/releases" > /dev/null |
| 75 | + fi |
| 76 | +
|
| 77 | + # Link release assets instead of re-uploading them (points back at the |
| 78 | + # original GitHub asset URLs). |
| 79 | + echo "$release" | jq -c '.assets[]?' | while read -r asset; do |
| 80 | + asset_name=$(echo "$asset" | jq -r '.name') |
| 81 | + asset_url=$(echo "$asset" | jq -r '.browser_download_url') |
| 82 | + link_payload=$(jq -n --arg name "$asset_name" --arg url "$asset_url" \ |
| 83 | + '{name: $name, url: $url}') |
| 84 | + curl -s -X POST -H "PRIVATE-TOKEN: $GITLAB_PAT" -H "Content-Type: application/json" \ |
| 85 | + -d "$link_payload" \ |
| 86 | + "https://gitlab.erik-skopp.de/api/v4/projects/$GITLAB_PROJECT/releases/$tag/assets/links" > /dev/null |
| 87 | + done |
| 88 | + done |
0 commit comments