Repository: axoflow/axoflow-otel-collector-releases
Pull request #65: feat: set appropriate build-time version
Author: login: csatib02
Labels: name: enhancement
description: New feature or request
Created: 2025-05-06T10:40:21Z | Merged: 2025-05-06T11:54:11Z

The runtime version of the agent was derived from `manifest.yaml` which was static on build-time. This PR adds scripts that changes the version number dynamically.


Diff:
diff --git a/.github/workflows/base-ci-goreleaser.yaml b/.github/workflows/base-ci-goreleaser.yaml
index f15cdb711..3a12d6a59 100644
--- a/.github/workflows/base-ci-goreleaser.yaml
+++ b/.github/workflows/base-ci-goreleaser.yaml
@@ -65,6 +65,9 @@ jobs:
           go-version: "~1.24"
           check-latest: true
 
+      - name: Run version update script
+        run: ./scripts/pre-update-version.sh --snapshot
+
       - name: Generate the sources for ${{ inputs.distribution }}
         env:
           DISTRIBUTIONS: ${{ inputs.distribution }}
diff --git a/.github/workflows/base-release.yaml b/.github/workflows/base-release.yaml
index d770a8ce1..601c81b93 100644
--- a/.github/workflows/base-release.yaml
+++ b/.github/workflows/base-release.yaml
@@ -65,6 +65,9 @@ jobs:
           sudo mv llvm-mingw-20250402-ucrt-ubuntu-20.04-x86_64 /opt/llvm-mingw
           sudo ln -s /opt/llvm-mingw/bin/aarch64-w64-mingw32-gcc /usr/local/bin/aarch64-w64-mingw32-gcc
 
+      - name: Run version update script
+        run: ./scripts/pre-update-version.sh "${{ github.ref_name }}"
+
       - name: Generate distribution sources
         run: make generate-sources
 
diff --git a/Makefile b/Makefile
index 19ead4870..202abfd8a 100644
--- a/Makefile
+++ b/Makefile
@@ -23,8 +23,11 @@ generate-sources: go ocb
 	@./scripts/build.sh -d "${DISTRIBUTIONS}" -s true -b ${OTELCOL_BUILDER}
 
 goreleaser-verify: goreleaser
+	@./scripts/pre-update-version.sh --snapshot
+	$(MAKE) generate-sources
 	@cd distributions/axoflow-otel-collector && $(GORELEASER) release --snapshot --clean --skip=sign,sbom
 	@cd ../../
+	@./scripts/post-update-version.sh
 
 ensure-goreleaser-up-to-date: generate-goreleaser
 	@git diff -s --exit-code distributions/*/.goreleaser.yaml || (echo "Check failed: The goreleaser templates have changed but the .goreleaser.yamls haven't. Run 'make generate-goreleaser' and update your PR." && exit 1)
diff --git a/scripts/post-update-version.sh b/scripts/post-update-version.sh
new file mode 100755
index 000000000..9350b133c
--- /dev/null
+++ b/scripts/post-update-version.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+mv "./distributions/axoflow-otel-collector/manifest.yaml.bak" "./distributions/axoflow-otel-collector/manifest.yaml"
+rm -f "./distributions/axoflow-otel-collector/manifest.yaml.bak"
+echo "Restored the original manifest.yaml file."
diff --git a/scripts/pre-update-version.sh b/scripts/pre-update-version.sh
new file mode 100755
index 000000000..274927dc5
--- /dev/null
+++ b/scripts/pre-update-version.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+SNAPSHOT_MODE=false
+for arg in "$@"; do
+  if [ "$arg" == "--snapshot" ]; then
+    SNAPSHOT_MODE=true
+  else
+    VERSION_ARG="$arg"
+  fi
+done
+
+if [ "$SNAPSHOT_MODE" = true ]; then
+    if ! PREVIOUS_TAG=$(git describe --tags --abbrev=0); then
+        echo "Error: Failed to get previous git tag. Are you in a git repository with tags?"
+        exit 1
+    fi
+    PREVIOUS_TAG="${PREVIOUS_TAG#v}"
+    
+    if ! COMMIT_HASH=$(git rev-parse --short=7 HEAD); then
+        echo "Error: Failed to get git commit hash. Are you in a git repository?"
+        exit 1
+    fi
+    
+    NEW_VERSION="${PREVIOUS_TAG}-SNAPSHOT-${COMMIT_HASH}"
+    echo "Creating snapshot version: $NEW_VERSION"
+else
+    if [ -z "$VERSION_ARG" ]; then
+        echo "Usage: $0 <new_version> or $0 --snapshot"
+        echo "Example: $0 0.121.0"
+        exit 1
+    fi
+    NEW_VERSION="$VERSION_ARG"
+fi
+
+CONFIG_FILE="./distributions/axoflow-otel-collector/manifest.yaml"
+if [ ! -f "$CONFIG_FILE" ]; then
+    echo "Error: File $CONFIG_FILE not found!"
+    exit 1
+fi
+
+# Use sed with backup to replace the version line
+sed -i.bak "s/^\([ ]*version:[ ]*\)[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*.*/\1$NEW_VERSION/" "$CONFIG_FILE"
+
+if grep -q "version: $NEW_VERSION" "$CONFIG_FILE"; then
+    echo "Version successfully updated to $NEW_VERSION in $CONFIG_FILE"
+else
+    echo "Failed to update version. Most probably the version has already been updated."
+    exit 1
+fi


Reviews:
user: login: copilot-pull-request-reviewer[bot]
body: ## Pull Request Overview

This PR updates the runtime version of the agent to be determined dynamically rather than statically at build-time. Key changes include:
- Adding a step to run a version update script with a static flag (--snapshot) in the base-release workflow.
- Adding a step to run a version update script with a dynamic argument (${ github.ref_name }) in the base-ci-goreleaser workflow.

### Reviewed Changes

Copilot reviewed 2 out of 5 changed files in this pull request and generated 2 comments.

| File | Description |
| ---- | ----------- |
| .github/workflows/base-release.yaml | Inserts a step to run the version update script using "--snapshot". |
| .github/workflows/base-ci-goreleaser.yaml | Inserts a step to run the version update script using "${{ github.ref_name }}" to pass dynamic value. |

<details>
<summary>Files not reviewed (3)</summary>

* **Makefile**: Language not supported
* **scripts/post-update-version.sh**: Language not supported
* **scripts/pre-update-version.sh**: Language not supported
</details>



state: COMMENTED
author_association: NONE
submitted_at: 2025-05-06T10:40:55Z
user: login: OverOrion
state: COMMENTED
author_association: NONE
submitted_at: 2025-05-06T11:30:10Z
user: login: csatib02
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-06T11:37:48Z
user: login: OverOrion
state: APPROVED
author_association: NONE
submitted_at: 2025-05-06T11:51:00Z

Review comments:
diff_hunk: @@ -65,6 +65,9 @@ jobs:
           sudo mv llvm-mingw-20250402-ucrt-ubuntu-20.04-x86_64 /opt/llvm-mingw
           sudo ln -s /opt/llvm-mingw/bin/aarch64-w64-mingw32-gcc /usr/local/bin/aarch64-w64-mingw32-gcc
 
+      - name: Run version update script
path: .github/workflows/base-release.yaml
user: login: Copilot
body: [nitpick] Consider adding an inline comment to clarify the use of the '--snapshot' flag in this workflow, explaining its intended effect compared to dynamic version updates in other workflows.
```suggestion
      - name: Run version update script
        # The '--snapshot' flag generates a snapshot version for testing purposes,
        # without creating a full release. This is useful for pre-release validation
        # and differs from workflows that dynamically update versions for production releases.
```
created_at: 2025-05-06T10:40:55Z
author_association: NONE
diff_hunk: @@ -65,6 +65,9 @@ jobs:
           go-version: "~1.24"
           check-latest: true
 
+      - name: Run version update script
path: .github/workflows/base-ci-goreleaser.yaml
user: login: Copilot
body: [nitpick] Consider adding an inline comment to explain the use of '${{ github.ref_name }}' as an argument, to help maintainers understand the logic behind using a dynamic version identifier in this workflow.
```suggestion
      - name: Run version update script
        # Pass the branch or tag name of the workflow run to the version update script.
```
created_at: 2025-05-06T10:40:55Z
author_association: NONE
diff_hunk: @@ -0,0 +1,5 @@
+#!/bin/bash
path: scripts/post-update-version.sh
user: login: OverOrion
body: Why is it needed to restore the previous state of `manifest.yaml`?
created_at: 2025-05-06T11:29:48Z
author_association: NONE
diff_hunk: @@ -0,0 +1,5 @@
+#!/bin/bash
path: scripts/post-update-version.sh
user: login: csatib02
body: If doing local tests, it is possible to stuck in a dirty state, resulting in the next call to `make goreleaser-verify` to fail. This is just here to avoid that.
created_at: 2025-05-06T11:37:48Z
author_association: MEMBER

PR comments:
user: login: github-actions[bot]
created_at: 2025-05-06T10:46:10Z
body: ## 🔍 Trivy Security Scan Results

🎉 No vulnerabilities detected!

📄 See the full report in the workflow artifacts.
author_association: NONE