From d00430bf45f1c5db127a621fa9680fed9fb9e0e7 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Wed, 16 Sep 2026 18:30:12 +0530 Subject: [PATCH 1/2] OpenConceptLab/ocl_issues#2733 | GHA to create release, tag and generate changelog --- .github/workflows/build.yml | 63 ++++++++--------- Dockerfile | 3 +- release_version.sh | 133 ++++++++++++++++++++++++++++-------- set_build_version.sh | 3 +- 4 files changed, 135 insertions(+), 67 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 275aed02..5b3b65d0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,6 +3,9 @@ name: Build on: workflow_dispatch: +permissions: + contents: write + env: AWS_REGION: "us-east-2" # set this to your preferred AWS region, e.g. us-west-1 ECR_REPOSITORY: "oclweb2" # set this to your Amazon ECR repository name @@ -48,11 +51,11 @@ jobs: steps: - name: Checkout repo uses: actions/checkout@v3 - - name: Get npm version - id: package-version - uses: martinbeentjes/npm-get-version-action@v1.3.1 - - name: Add GITHUB_SHA_SHORT env property with commit short sha - run: echo "GITHUB_SHA_SHORT=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - name: Compute version and short sha + id: version + run: | + echo "tag=$(bash release_version.sh full-version)" >> $GITHUB_OUTPUT + echo "sha=$(bash release_version.sh sha)" >> $GITHUB_OUTPUT - name: Log in to Docker Hub uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a with: @@ -71,31 +74,25 @@ jobs: with: context: . push: true - tags: openconceptlab/oclweb2:nightly, openconceptlab/oclweb2:${{ steps.package-version.outputs.current-version}}-${{env.GITHUB_SHA_SHORT}} - build-args: SOURCE_COMMIT=${{env.GITHUB_SHA_SHORT}} + tags: openconceptlab/oclweb2:nightly, openconceptlab/oclweb2:${{ steps.version.outputs.tag }} + build-args: SOURCE_COMMIT=${{ steps.version.outputs.sha }} cache-from: type=gha cache-to: type=gha,mode=max release: needs: [build] runs-on: ubuntu-latest - name: Release draft + name: Release environment: rc steps: - name: Checkout repo uses: actions/checkout@v3 - - name: Get npm version - id: package-version - uses: martinbeentjes/npm-get-version-action@v1.3.1 - - name: Add GITHUB_SHA_SHORT env property with commit short sha - run: echo "GITHUB_SHA_SHORT=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV - - name: Tag and release - uses: avakar/tag-and-release@v1 with: - tag_name: ${{ steps.package-version.outputs.current-version}}-${{env.GITHUB_SHA_SHORT}} - draft: true + fetch-depth: 0 + - name: Publish release and bump version + run: bash release_version.sh publish env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} deploy-qa: needs: [release] @@ -115,16 +112,14 @@ jobs: uses: aws-actions/amazon-ecr-login@v1 - name: Checkout repo uses: actions/checkout@v3 - - name: Get npm version - id: package-version - uses: martinbeentjes/npm-get-version-action@v1.3.1 - - name: Add GITHUB_SHA_SHORT env property with commit short sha - run: echo "GITHUB_SHA_SHORT=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - name: Compute version and short sha + id: version + run: echo "tag=$(bash release_version.sh full-version)" >> $GITHUB_OUTPUT - name: Push image to Amazon ECR id: push-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ steps.package-version.outputs.current-version}}-${{env.GITHUB_SHA_SHORT}} + IMAGE_TAG: ${{ steps.version.outputs.tag }} run: | # Build a docker container and # push it to ECR so that it can @@ -170,16 +165,14 @@ jobs: uses: aws-actions/amazon-ecr-login@v1 - name: Checkout repo uses: actions/checkout@v3 - - name: Get npm version - id: package-version - uses: martinbeentjes/npm-get-version-action@v1.3.1 - - name: Add GITHUB_SHA_SHORT env property with commit short sha - run: echo "GITHUB_SHA_SHORT=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - name: Compute version and short sha + id: version + run: echo "tag=$(bash release_version.sh full-version)" >> $GITHUB_OUTPUT - name: Push image to Amazon ECR id: push-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ steps.package-version.outputs.current-version}}-${{env.GITHUB_SHA_SHORT}} + IMAGE_TAG: ${{ steps.version.outputs.tag }} run: | # Build a docker container and # push it to ECR so that it can @@ -223,16 +216,14 @@ jobs: uses: aws-actions/amazon-ecr-login@v1 - name: Checkout repo uses: actions/checkout@v3 - - name: Get npm version - id: package-version - uses: martinbeentjes/npm-get-version-action@v1.3.1 - - name: Add GITHUB_SHA_SHORT env property with commit short sha - run: echo "GITHUB_SHA_SHORT=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - name: Compute version and short sha + id: version + run: echo "tag=$(bash release_version.sh full-version)" >> $GITHUB_OUTPUT - name: Push image to Amazon ECR id: push-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ steps.package-version.outputs.current-version}}-${{env.GITHUB_SHA_SHORT}} + IMAGE_TAG: ${{ steps.version.outputs.tag }} run: | # Build a docker container and # push it to ECR so that it can diff --git a/Dockerfile b/Dockerfile index 736308ae..8401fe42 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,8 @@ ADD start.sh /app/ RUN chmod +x start.sh ADD set_build_version.sh /app/ -RUN chmod +X set_build_version.sh +ADD release_version.sh /app/ +RUN chmod +X set_build_version.sh release_version.sh ARG SOURCE_COMMIT RUN ["bash", "-c", "./set_build_version.sh"] diff --git a/release_version.sh b/release_version.sh index db61e658..05a77030 100755 --- a/release_version.sh +++ b/release_version.sh @@ -1,43 +1,120 @@ #!/bin/bash -set -e +# Repo-agnostic release helper. Copy this file as-is into any OCL repo and +# only edit the two config lines below. +# +# Version format: RELEASE.MAJOR.MINOR[-channel], e.g. "3.0.0-alpha". +# RELEASE and MAJOR (the first two numbers) and the channel label are set by +# hand by editing VERSION_FILE. MINOR (the third number) plus the trailing +# short commit sha are computed and bumped automatically by `publish`. +# +# Usage: +# ./release_version.sh sha [raw-sha] print an 8-char short sha (raw-sha, else $GITHUB_SHA, else git HEAD, else "dev") +# ./release_version.sh current print the version currently in VERSION_FILE +# ./release_version.sh full-version print "-" +# ./release_version.sh publish create a published GitHub Release with a changelog for the current +# version, then bump MINOR in VERSION_FILE and push the bump commit +set -euo pipefail +# --- Per-repo config (only these two lines differ across repos) --- VERSION_FILE="package.json" +VERSION_KEY="version" +# -------------------------------------------------------------- -SOURCE_COMMIT=$(git rev-parse HEAD) -export SOURCE_COMMIT=${SOURCE_COMMIT:0:8} +cmd_sha() { + local raw="${1:-}" + [ -n "$raw" ] || raw="${GITHUB_SHA:-}" + [ -n "$raw" ] || raw="$(git rev-parse HEAD 2>/dev/null || true)" + [ -n "$raw" ] || raw="dev" + echo "${raw:0:8}" +} -PROJECT_VERSION=$(cat $VERSION_FILE \ - | grep version \ - | head -1 \ - | awk -F: '{ print $2 }' \ - | sed 's/[",]//g' \ - | tr -d '[[:space:]]') +cmd_current() { + grep -m1 "$VERSION_KEY" "$VERSION_FILE" \ + | sed -E "s/.*[\"']([0-9]+\.[0-9]+\.[0-9]+[^\"']*)[\"'].*/\1/" +} -TAG="$PROJECT_VERSION-$SOURCE_COMMIT" +cmd_full_version() { + echo "$(cmd_current)-$(cmd_sha)" +} -./set_build_version.sh +next_version() { + local current="$1" release major rest leading suffix + release=$(cut -d. -f1 <<<"$current") + major=$(cut -d. -f2 <<<"$current") + rest=$(cut -d. -f3- <<<"$current") + if [[ "$rest" =~ ^([0-9]+)(.*)$ ]]; then + leading="${BASH_REMATCH[1]}" + suffix="${BASH_REMATCH[2]}" + else + leading=0 + suffix="" + fi + echo "${release}.${major}.$((leading + 1))${suffix}" +} -git checkout -b release -git add config.json -git commit -m "Release version $PROJECT_VERSION" +is_prerelease() { + local current="$1" rest + rest=$(cut -d. -f3- <<<"$current") + [[ "$rest" =~ ^[0-9]+(.*)$ ]] && [ -n "${BASH_REMATCH[1]}" ] +} -git tag "$TAG" +write_version() { + local new_version="$1" + sed -i -E "0,/${VERSION_KEY}/ s/([\"'])[0-9]+\.[0-9]+\.[0-9]+[^\"']*([\"'])/\1${new_version}\2/" "$VERSION_FILE" +} -git remote set-url origin ${GIT_REPO_URL} -git push origin --tags +cmd_publish() { + local current_version sha tag prev_tag changelog new_version default_branch prerelease_args=() -docker pull $DOCKER_IMAGE_ID -docker tag $DOCKER_IMAGE_ID $DOCKER_IMAGE_NAME:$TAG -docker push $DOCKER_IMAGE_NAME:$TAG + current_version="$(cmd_current)" + sha="$(cmd_sha "${GITHUB_SHA:-}")" + tag="${current_version}-${sha}" -if [[ "$INCREASE_MAINTENANCE_VERSION" = true ]]; then - git checkout master + git fetch --tags --quiet || true + prev_tag="$(git describe --tags --abbrev=0 2>/dev/null || true)" + if [ -n "$prev_tag" ]; then + changelog="$(git log "${prev_tag}..HEAD" --pretty=format:'- %s (%h)')" + else + changelog="$(git log --pretty=format:'- %s (%h)')" + fi + [ -n "$changelog" ] || changelog="No changes recorded." - NEW_PROJECT_VERSION=$(echo "${PROJECT_VERSION}" | awk -F. -v OFS=. '{$NF++;print}') - sed -i "s/\"version\": \"$PROJECT_VERSION\"/\"version\": \"$NEW_PROJECT_VERSION\"/" $VERSION_FILE + is_prerelease "$current_version" && prerelease_args=(--prerelease) - git add $VERSION_FILE - git commit -m "[skip ci] Increase maintenance version to $NEW_PROJECT_VERSION" + echo "Publishing release ${tag}" + gh release create "$tag" \ + --target "${GITHUB_SHA:-HEAD}" \ + --title "$tag" \ + --notes "$changelog" \ + "${prerelease_args[@]}" - git push origin master -fi + new_version="$(next_version "$current_version")" + echo "Bumping version: ${current_version} -> ${new_version}" + + default_branch="${GITHUB_REF_NAME:-main}" + git config user.email "github-actions[bot]@users.noreply.github.com" + git config user.name "github-actions[bot]" + git fetch origin "$default_branch" --quiet + git checkout -B "$default_branch" "origin/${default_branch}" + + write_version "$new_version" + git add "$VERSION_FILE" + git commit -m "[skip ci] Bump version to ${new_version}" + + for attempt in 1 2 3; do + if git push origin "$default_branch"; then + break + fi + echo "Push rejected, rebasing and retrying (${attempt})..." + git fetch origin "$default_branch" --quiet + git rebase "origin/${default_branch}" + done +} + +case "${1:-}" in + sha) shift; cmd_sha "${1:-}" ;; + current) cmd_current ;; + full-version) cmd_full_version ;; + publish) cmd_publish ;; + *) echo "Usage: $0 {sha [raw-sha]|current|full-version|publish}" >&2; exit 1 ;; +esac diff --git a/set_build_version.sh b/set_build_version.sh index 0627ccfd..11f61add 100755 --- a/set_build_version.sh +++ b/set_build_version.sh @@ -5,8 +5,7 @@ set -e CONFIG_FILE="config.json" touch ${CONFIG_FILE} -SHA=${SOURCE_COMMIT:-'dev'} -SHA=${SHA:0:8} +SHA=$(./release_version.sh sha "${SOURCE_COMMIT:-}") echo "Setting build version to $SHA in ${CONFIG_FILE}" From 427930acc68dad409db29f34dafe9200d16f924a Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Wed, 23 Sep 2026 18:45:17 +0530 Subject: [PATCH 2/2] OpenConceptLab/ocl_issues#2733 | review feedbacks --- .github/workflows/build.yml | 11 +++++------ release_version.sh | 35 +++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5b3b65d0..23d6506b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,8 +3,9 @@ name: Build on: workflow_dispatch: -permissions: - contents: write +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false env: AWS_REGION: "us-east-2" # set this to your preferred AWS region, e.g. us-west-1 @@ -44,10 +45,6 @@ jobs: name: Build and push image runs-on: ubuntu-latest - #permissions: - # packages: write - # contents: read - steps: - name: Checkout repo uses: actions/checkout@v3 @@ -84,6 +81,8 @@ jobs: runs-on: ubuntu-latest name: Release environment: rc + permissions: + contents: write steps: - name: Checkout repo uses: actions/checkout@v3 diff --git a/release_version.sh b/release_version.sh index 05a77030..ccf80c16 100755 --- a/release_version.sh +++ b/release_version.sh @@ -4,13 +4,13 @@ # # Version format: RELEASE.MAJOR.MINOR[-channel], e.g. "3.0.0-alpha". # RELEASE and MAJOR (the first two numbers) and the channel label are set by -# hand by editing VERSION_FILE. MINOR (the third number) plus the trailing -# short commit sha are computed and bumped automatically by `publish`. +# hand by editing VERSION_FILE. MINOR (the third number) is bumped +# automatically by `publish`. # # Usage: # ./release_version.sh sha [raw-sha] print an 8-char short sha (raw-sha, else $GITHUB_SHA, else git HEAD, else "dev") # ./release_version.sh current print the version currently in VERSION_FILE -# ./release_version.sh full-version print "-" +# ./release_version.sh full-version print "-" for build artifacts # ./release_version.sh publish create a published GitHub Release with a changelog for the current # version, then bump MINOR in VERSION_FILE and push the bump commit set -euo pipefail @@ -29,7 +29,7 @@ cmd_sha() { } cmd_current() { - grep -m1 "$VERSION_KEY" "$VERSION_FILE" \ + grep -m1 -E "^[[:space:]]*[\"']?${VERSION_KEY}[\"']?[[:space:]]*[:=]" "$VERSION_FILE" \ | sed -E "s/.*[\"']([0-9]+\.[0-9]+\.[0-9]+[^\"']*)[\"'].*/\1/" } @@ -60,24 +60,30 @@ is_prerelease() { write_version() { local new_version="$1" - sed -i -E "0,/${VERSION_KEY}/ s/([\"'])[0-9]+\.[0-9]+\.[0-9]+[^\"']*([\"'])/\1${new_version}\2/" "$VERSION_FILE" + sed -i -E "/^[[:space:]]*[\"']?${VERSION_KEY}[\"']?[[:space:]]*[:=]/ s/([\"'])[0-9]+\.[0-9]+\.[0-9]+[^\"']*([\"'])/\1${new_version}\2/" "$VERSION_FILE" } cmd_publish() { - local current_version sha tag prev_tag changelog new_version default_branch prerelease_args=() + local current_version sha tag prev_tag changelog new_version default_branch + local commit_count changelog_limit=50 pushed=false prerelease_args=() current_version="$(cmd_current)" sha="$(cmd_sha "${GITHUB_SHA:-}")" - tag="${current_version}-${sha}" + tag="${current_version}" git fetch --tags --quiet || true prev_tag="$(git describe --tags --abbrev=0 2>/dev/null || true)" if [ -n "$prev_tag" ]; then changelog="$(git log "${prev_tag}..HEAD" --pretty=format:'- %s (%h)')" else - changelog="$(git log --pretty=format:'- %s (%h)')" + changelog="$(git log -n "$changelog_limit" --pretty=format:'- %s (%h)')" + commit_count="$(git rev-list --count HEAD 2>/dev/null || echo 0)" + if [ "$commit_count" -gt "$changelog_limit" ]; then + changelog="${changelog}"$'\n\n'"Showing the latest ${changelog_limit} commits because this repository has no previous release tag." + fi fi [ -n "$changelog" ] || changelog="No changes recorded." + changelog="Source commit: ${sha}"$'\n\n'"${changelog}" is_prerelease "$current_version" && prerelease_args=(--prerelease) @@ -91,7 +97,7 @@ cmd_publish() { new_version="$(next_version "$current_version")" echo "Bumping version: ${current_version} -> ${new_version}" - default_branch="${GITHUB_REF_NAME:-main}" + default_branch="${GITHUB_REF_NAME:-master}" git config user.email "github-actions[bot]@users.noreply.github.com" git config user.name "github-actions[bot]" git fetch origin "$default_branch" --quiet @@ -103,12 +109,21 @@ cmd_publish() { for attempt in 1 2 3; do if git push origin "$default_branch"; then + pushed=true break fi echo "Push rejected, rebasing and retrying (${attempt})..." git fetch origin "$default_branch" --quiet - git rebase "origin/${default_branch}" + if ! git rebase "origin/${default_branch}"; then + git rebase --abort || true + echo "Failed to rebase version bump onto origin/${default_branch}" >&2 + exit 1 + fi done + if [ "$pushed" != true ]; then + echo "Failed to push version bump after 3 attempts" >&2 + exit 1 + fi } case "${1:-}" in