Skip to content

Commit

Permalink
ci(github): Add release sync workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Nov 18, 2024
1 parent 5829e71 commit f8d1a64
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 35 deletions.
98 changes: 63 additions & 35 deletions .github/release.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions .gitlab/gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f8d1a64

Please sign in to comment.