From f8d1a64ec6d1a0b01a20f25cc24e28f1aba7aaa4 Mon Sep 17 00:00:00 2001 From: cluttrdev Date: Sat, 16 Nov 2024 21:54:55 +0100 Subject: [PATCH] ci(github): Add release sync workflow --- .github/release.sh | 98 ++++++++++++++++++++++------------- .github/workflows/release.yml | 18 +++++++ .gitlab/gitlab-ci.yml | 18 +++++++ 3 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/release.yml diff --git a/.github/release.sh b/.github/release.sh index ca45eae..3c9fa0a 100755 --- a/.github/release.sh +++ b/.github/release.sh @@ -1,44 +1,72 @@ #!/usr/bin/env bash set -euo pipefail -owner=cluttrdev -repo=gitlab-clickhouse-exporter - -tag=${RELEASE_TAG} -assets=${RELEASE_ASSETS} - -echo "creating release for ${tag}" -response=$(curl -L -s \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer ${GITHUB_TOKEN}" \ - -H "X-GitHub-Api-Version 2022-11-28" \ - -d "{\"tag_name\": \"${tag}\", \"name\": \"${tag}\", \"body\": \"\"}" \ - https://api.github.com/repos/${owner}/${repo}/releases \ - 2>/dev/null +echo "Fetching GitLab release" +project_id=50817395 # cluttrdev/gitlab-exporter +response=$( + curl -sSL --fail-with-body \ + "https://gitlab.com/api/v4/projects/${project_id}/releases/${TAG_NAME}" ) +if [ $? -ne 0 ]; then + echo "${response}" + exit 1 +fi + +# extract release notes +release_notes=$(jq -r '.description' <<< "${response}" | sed 's/$/\\n/' | tr -d '\n') + +# adjust commit link url +release_notes=$(sed 's#gitlab.com/cluttrdev/gitlab-exporter/-/commit/#github.com/cluttrdev/gitlab-exporter-mirror/commit/#g' <<< "${release_notes}") -error=$(jq -r '.errors[0].code // empty' <<< $response) -[ -n "$error" ] && { - echo $error +release_assets=$(jq -r '.assets.links[]|[.name, .url] | @tsv' <<< "${response}") + +echo "Creating GitHub release" +response=$( + curl "https://api.github.com/repos/cluttrdev/gitlab-exporter-mirror/releases" \ + -sSL --fail-with-body \ + --header "Accept: application/vnd.github+json" \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "X-GitHub-Api-Version 2022-11-28" \ + --data "{\"tag_name\":\"${TAG_NAME}\", \"body\":\"${release_notes}\"}" +) +if [ $? -ne 0 ]; then + echo "${response}" exit 1 -} +fi -release_id=$(jq -r '.id // empty' <<< $response) -[ -n "$release_id" ] || { - echo "No release with tag: ${tag}" - exit 2 +upload_url=$(jq -r '.upload_url // empty' <<<"${response}") +[ -n "${upload_url}" ] || { + echo "Missing upload url" + echo "${response}" + exit 1 } +upload_url="${upload_url%\{*}" + +echo "Synching release assets" +echo "${release_assets}" | \ + while IFS=$'\t' read -r name url; do + printf "\t%s" "${name}" + + printf " donwload..." + resp=$(curl -sSL --fail-with-body "${url}" -o "${name}") + if [ $? -ne 0 ]; then + echo "${resp}" + exit 1 + fi -upload_url=https://uploads.github.com/repos/${owner}/${repo}/releases/${release_id}/assets -for asset in ${assets}; do - name=$(basename ${asset}) - echo "uploading asset: ${name}" - response=$(curl -L -s \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer ${GITHUB_TOKEN}" \ - -H "X-GitHub-Api-Version 2022-11-28" \ - -H "Content-Type: application/octet-stream" \ - ${upload_url}?name=${name} \ - --data-binary "@${asset}" - ) -done + printf " upload..." + resp=$( + curl "${upload_url}?name=${name}" \ + -sSL --fail-with-body \ + --header "Accept: application/vnd.github+json" \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --header "X-GitHub-Api-Version 2022-11-28" \ + --header "Content-Type: application/octet-stream" \ + --data-binary "@${name}" + ) + if [ $? -ne 0 ]; then + echo "${resp}" + exit 1 + fi + printf " done\n" + done diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..18db46b --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,18 @@ +name: Release + +on: workflow_dispatch + +jobs: + release: + runs-on: ubuntu-latest + if: ${{ startsWith(github.ref, 'refs/tags/') }} + permissions: + contents: write # allow creating a release + steps: + - name: Check out the repository to the runner + uses: actions/checkout@v4 + - name: Sync release + env: + TAG_NAME: ${{ github.ref_name }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: .github/release.sh diff --git a/.gitlab/gitlab-ci.yml b/.gitlab/gitlab-ci.yml index d7a644e..75064a7 100644 --- a/.gitlab/gitlab-ci.yml +++ b/.gitlab/gitlab-ci.yml @@ -116,6 +116,24 @@ release: script: - .gitlab/release.sh +release-sync: + stage: release + rules: + - if: $CI_COMMIT_TAG != null + needs: + - release + variables: + GITHUB_OWNER: cluttrdev + GITHUB_REPO: gitlab-exporter + script: + - | # Trigger GitHub release sync workflow + curl \ + -sSL --fail-with-body \ + --header "Accept: application/vnd.github.v3+json" \ + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ + --data "{\"ref\":\"${CI_COMMIT_TAG}\"}" \ + "https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/actions/workflows/release.yml/dispatches" + release-image: stage: release rules: