From 042e2a2bac3c35080f5b8a222f3a4667d3e57431 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Wed, 13 May 2026 12:55:58 +0200 Subject: [PATCH] Enhance release process by validating versioning and adding Gitea release creation Signed-off-by: Hans Kokx --- .gitea/workflows/ci.yml | 90 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index eacd8e0..a52000b 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -115,8 +115,45 @@ jobs: exit 1 fi - if [ "$PR_VERSION" = "$BASE_VERSION" ]; then - echo "pubspec.yaml version was not updated (still $PR_VERSION)." + parse_semver_core() { + printf '%s' "$1" | sed -E 's/^([0-9]+\.[0-9]+\.[0-9]+).*/\1/' + } + + BASE_CORE="$(parse_semver_core "$BASE_VERSION")" + PR_CORE="$(parse_semver_core "$PR_VERSION")" + + if ! printf '%s' "$BASE_CORE" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then + echo "Base version is not valid semver: $BASE_VERSION" + exit 1 + fi + + if ! printf '%s' "$PR_CORE" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then + echo "PR version is not valid semver: $PR_VERSION" + exit 1 + fi + + BASE_MAJOR="${BASE_CORE%%.*}" + BASE_REST="${BASE_CORE#*.}" + BASE_MINOR="${BASE_REST%%.*}" + BASE_PATCH="${BASE_REST#*.}" + + PR_MAJOR="${PR_CORE%%.*}" + PR_REST="${PR_CORE#*.}" + PR_MINOR="${PR_REST%%.*}" + PR_PATCH="${PR_REST#*.}" + + if [ "$PR_MAJOR" -lt "$BASE_MAJOR" ]; then + echo "pubspec.yaml version must be greater than base version ($BASE_VERSION -> $PR_VERSION)." + exit 1 + fi + + if [ "$PR_MAJOR" -eq "$BASE_MAJOR" ] && [ "$PR_MINOR" -lt "$BASE_MINOR" ]; then + echo "pubspec.yaml version must be greater than base version ($BASE_VERSION -> $PR_VERSION)." + exit 1 + fi + + if [ "$PR_MAJOR" -eq "$BASE_MAJOR" ] && [ "$PR_MINOR" -eq "$BASE_MINOR" ] && [ "$PR_PATCH" -le "$BASE_PATCH" ]; then + echo "pubspec.yaml version must be at least 0.0.1 greater than base version ($BASE_VERSION -> $PR_VERSION)." exit 1 fi @@ -146,9 +183,11 @@ jobs: uses: actions/checkout@v4 - name: Setup Dart uses: dart-lang/setup-dart@v1 - - name: Publish package on push to main + - name: Publish package and create Gitea release on push to main env: PUB_CREDENTIALS_JSON: ${{ secrets.PUB_CREDENTIALS_JSON }} + RELEASE_TOKEN: ${{ secrets.GITHUB_TOKEN }} + FALLBACK_RELEASE_TOKEN: ${{ secrets.GITEA_RELEASE_TOKEN }} run: | set -euo pipefail @@ -182,3 +221,48 @@ jobs: dart pub get dart pub publish --dry-run dart pub publish --force + + TOKEN="${RELEASE_TOKEN:-}" + if [ -z "$TOKEN" ]; then + TOKEN="${FALLBACK_RELEASE_TOKEN:-}" + fi + + if [ -z "$TOKEN" ]; then + echo "Missing release token. Provide default workflow token or GITEA_RELEASE_TOKEN." + exit 1 + fi + + SERVER_URL="${GITHUB_SERVER_URL:-${GITEA_SERVER_URL:-}}" + REPO_PATH="${GITHUB_REPOSITORY:-${GITEA_REPOSITORY:-}}" + + if [ -z "$SERVER_URL" ] || [ -z "$REPO_PATH" ]; then + echo "Missing repository context required for Gitea API release creation." + exit 1 + fi + + TAG="v${PACKAGE_VERSION}" + RELEASE_BY_TAG_URL="${SERVER_URL}/api/v1/repos/${REPO_PATH}/releases/tags/${TAG}" + + if curl -fsS -H "Authorization: token ${TOKEN}" "$RELEASE_BY_TAG_URL" >/dev/null; then + echo "Release ${TAG} already exists; skipping release creation." + exit 0 + fi + + CREATE_RELEASE_URL="${SERVER_URL}/api/v1/repos/${REPO_PATH}/releases" + RELEASE_PAYLOAD="{\"tag_name\":\"${TAG}\",\"target\":\"${GITHUB_SHA}\",\"target_commitish\":\"${GITHUB_SHA}\",\"title\":\"${TAG}\",\"name\":\"${TAG}\",\"note\":\"Release ${TAG}\",\"body\":\"Release ${TAG}\",\"draft\":false,\"prerelease\":false}" + + HTTP_CODE="$(curl -sS -o /tmp/gitea-release-response.json -w '%{http_code}' \ + -X POST "$CREATE_RELEASE_URL" \ + -H "Authorization: token ${TOKEN}" \ + -H 'Content-Type: application/json' \ + -d "$RELEASE_PAYLOAD")" + + if [ "$HTTP_CODE" = "201" ]; then + echo "Created Gitea release for ${TAG}." + elif [ "$HTTP_CODE" = "409" ]; then + echo "Release or tag ${TAG} already exists; treating as success." + else + echo "Failed to create Gitea release. HTTP ${HTTP_CODE}." + cat /tmp/gitea-release-response.json + exit 1 + fi