Skip to content

Commit 12cd128

Browse files
GHA-156 Add Bump Project Version Action to update version in Maven and Gradle files (#95)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3398d23 commit 12cd128

File tree

6 files changed

+281
-17
lines changed

6 files changed

+281
-17
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test Bump Version Script
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
paths:
8+
- 'bump-version/**'
9+
- '.github/workflows/test-bump-version.yml'
10+
workflow_dispatch:
11+
12+
jobs:
13+
test-bump-version:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
18+
19+
- name: Set up bash permissions
20+
run: |
21+
chmod +x bump-version/bump_version.sh
22+
chmod +x bump-version/test_bump_version.sh
23+
24+
- name: Run bump version tests
25+
shell: bash
26+
run: |
27+
bash bump-version/test_bump_version.sh
28+

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ A centralized collection of reusable GitHub Actions designed to streamline and a
44

55
## Available Actions
66

7-
| Action | Description |
8-
|--------|-------------|
9-
| [Check Releasability Status](check-releasability-status/README.md) | Checks the releasability status and extracts the version if successful |
10-
| [Create Integration Ticket](create-integration-ticket/README.md) | Creates a Jira integration ticket with a custom summary and links it to another existing ticket |
11-
| [Create Pull Request](create-pull-request/README.md) | Creates or updates a pull request using the `gh` CLI, with vault-based token resolution |
12-
| [Create Jira Release Ticket](create-jira-release-ticket/README.md) | Automates the creation of an "Ask for release" ticket in Jira |
13-
| [Create Jira Version](create-jira-version/README.md) | Creates a new version in a Jira project, with the ability to automatically determine the next version number |
14-
| [Get Jira Release Notes](get-jira-release-notes/README.md) | Fetches Jira release notes and generates the release notes URL for a given project and version |
15-
| [Get Jira Version](get-jira-version/README.md) | Extracts a Jira-compatible version number from a release version by formatting it appropriately for Jira |
16-
| [Get Release Version](get-release-version/README.md) | Extracts the release version from the repox status on a specified branch |
17-
| [Lock Branch](lock-branch/README.md) | Locks or unlocks a branch by modifying the `lock_branch` setting in branch protection rules |
18-
| [Notify Slack on Failure](notify-slack/README.md) | Sends a Slack notification when a job fails |
19-
| [Publish GitHub Release](publish-github-release/README.md) | Publishes a GitHub Release with notes fetched from Jira or provided directly |
20-
| [Release Jira Version](release-jira-version/README.md) | Releases a Jira version and creates the next one |
21-
| [Update Analyzer](update-analyzer/README.md) | Updates an analyzer version in SonarQube or SonarCloud and creates a pull request |
22-
| [Update Release Ticket Status](update-release-ticket-status/README.md) | Updates the status of a Jira release ticket and can change its assignee |
23-
| [Update Rule Metadata](update-rule-metadata/README.md) | Automates updating rule metadata across all supported languages using the rule-api tooling |
7+
| Action | Description |
8+
|-------------------------------------------------------------------------|-------------|
9+
| [Bump Project Version](bump-version/README.md) | Updates the version in Maven and Gradle files across your repository, with support for excluding specific modules |
10+
| [Check Releasability Status](check-releasability-status/README.md) | Checks the releasability status and extracts the version if successful |
11+
| [Create Integration Ticket](create-integration-ticket/README.md) | Creates a Jira integration ticket with a custom summary and links it to another existing ticket |
12+
| [Create Pull Request](create-pull-request/README.md) | Creates or updates a pull request using the `gh` CLI, with vault-based token resolution |
13+
| [Create Jira Release Ticket](create-jira-release-ticket/README.md) | Automates the creation of an "Ask for release" ticket in Jira |
14+
| [Create Jira Version](create-jira-version/README.md) | Creates a new version in a Jira project, with the ability to automatically determine the next version number |
15+
| [Get Jira Release Notes](get-jira-release-notes/README.md) | Fetches Jira release notes and generates the release notes URL for a given project and version |
16+
| [Get Jira Version](get-jira-version/README.md) | Extracts a Jira-compatible version number from a release version by formatting it appropriately for Jira |
17+
| [Get Release Version](get-release-version/README.md) | Extracts the release version from the repox status on a specified branch |
18+
| [Lock Branch](lock-branch/README.md) | Locks or unlocks a branch by modifying the `lock_branch` setting in branch protection rules |
19+
| [Notify Slack on Failure](notify-slack/README.md) | Sends a Slack notification when a job fails |
20+
| [Publish GitHub Release](publish-github-release/README.md) | Publishes a GitHub Release with notes fetched from Jira or provided directly |
21+
| [Release Jira Version](release-jira-version/README.md) | Releases a Jira version and creates the next one |
22+
| [Update Analyzer](update-analyzer/README.md) | Updates an analyzer version in SonarQube or SonarCloud and creates a pull request |
23+
| [Update Release Ticket Status](update-release-ticket-status/README.md) | Updates the status of a Jira release ticket and can change its assignee |
24+
| [Update Rule Metadata](update-rule-metadata/README.md) | Automates updating rule metadata across all supported languages using the rule-api tooling |
2425

2526
## Available Workflows
2627

bump-version/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Bump Project Version GitHub Action
2+
3+
This action updates the version in Maven and Gradle files across your repository. It supports excluding specific modules from the version bump.
4+
5+
## Inputs
6+
7+
| Name | Description | Required | Default |
8+
|------------------|------------------------------------------------------------------|----------|----------|
9+
| version | The new version (without `-SNAPSHOT`) | Yes | |
10+
| token | The GitHub token for PR creation | No | |
11+
| excluded-modules | Comma-separated list of modules to exclude from version bumping | No | |
12+
| base-branch | The base branch for the pull request | No | `master` |
13+
| pr-labels | Comma-separated list of labels to add to the pull request | No | |
14+
15+
## Outputs
16+
17+
| Name | Description |
18+
|-------------------|---------------------------------------|
19+
| pull-request-url | URL of the created pull request |
20+
21+
## Usage
22+
23+
```yaml
24+
- name: Bump version
25+
id: bump
26+
uses: SonarSource/release-github-actions/bump-version@v1
27+
with:
28+
version: '1.2.3'
29+
token: ${{ secrets.GITHUB_TOKEN }}
30+
excluded-modules: 'moduleA,moduleB'
31+
32+
- name: Show PR URL
33+
run: echo "PR URL: ${{ steps.bump.outputs.pull-request-url }}"
34+
```
35+
36+
## How it works
37+
- Updates all `pom.xml` and `gradle.properties` files to the new version.
38+
- Skips files in modules listed in `excluded-modules`.
39+
- Commits changes and creates a pull request.

bump-version/action.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Bump Project Version
2+
3+
inputs:
4+
version:
5+
description: The new version (without -SNAPSHOT)
6+
required: true
7+
token:
8+
description: The GitHub token for PR creation
9+
required: false
10+
excluded-modules:
11+
description: A comma-separated list of modules to exclude from version bumping
12+
required: false
13+
base-branch:
14+
description: The base branch for the pull request
15+
required: false
16+
default: master
17+
pr-labels:
18+
description: Comma-separated list of labels to add to the pull request
19+
required: false
20+
21+
runs:
22+
using: 'composite'
23+
steps:
24+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
25+
- name: Bump version
26+
shell: bash
27+
env:
28+
VERSION: "${{ inputs.version }}-SNAPSHOT"
29+
EXCLUDED_MODULES: ${{ inputs.excluded-modules || '' }}
30+
run: |
31+
bash "${GITHUB_ACTION_PATH}/bump_version.sh"
32+
- name: Create Pull Request for Version Bump
33+
uses: SonarSource/release-github-actions/create-pull-request@v1
34+
id: create-pr
35+
env:
36+
TITLE_MESSAGE: Prepare next development iteration ${{ inputs.version }}
37+
with:
38+
token: ${{ inputs.token }}
39+
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
40+
commit-message: ${{ env.TITLE_MESSAGE }}
41+
title: ${{ env.TITLE_MESSAGE }}
42+
base: ${{ inputs.base-branch }}
43+
branch: bot/prepare-next-development-iteration-${{ inputs.version }}
44+
labels: ${{ inputs.pr-labels }}
45+
reviewers: ${{ github.actor }}
46+
47+
outputs:
48+
pull-request-url:
49+
description: URL of the version bump pull request
50+
value: ${{ steps.create-pr.outputs.pull-request-url }}

bump-version/bump_version.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Inputs via environment:
5+
# - VERSION: version string with -SNAPSHOT suffix (e.g., 1.2.3-SNAPSHOT)
6+
# - EXCLUDED_MODULES: optional comma-separated list of module paths to exclude
7+
8+
VERSION=${VERSION:-}
9+
EXCLUDED_MODULES=${EXCLUDED_MODULES:-}
10+
11+
if [[ -z "$VERSION" ]]; then
12+
echo "ERROR: VERSION env var is required" >&2
13+
exit 1
14+
fi
15+
16+
# Build dynamic exclusion args from EXCLUDED_MODULES (comma-separated)
17+
EXCLUDE_ARGS=()
18+
if [[ -n "$EXCLUDED_MODULES" ]]; then
19+
IFS=',' read -r -a modules <<< "$EXCLUDED_MODULES"
20+
for m in "${modules[@]}"; do
21+
# Trim leading/trailing whitespace
22+
m_trimmed="${m##+([[:space:]])}"
23+
m_trimmed="${m_trimmed%%+([[:space:]])}"
24+
# Fallback trim if extglob isn't enabled
25+
if [[ -z "${m_trimmed}" ]]; then
26+
m_trimmed="$m"
27+
fi
28+
# Remove surrounding spaces using sed as a backup
29+
m_trimmed="$(printf "%s" "$m_trimmed" | sed 's/^\s\+//;s/\s\+$//')"
30+
# Skip empty entries
31+
if [[ -z "$m_trimmed" ]]; then
32+
continue
33+
fi
34+
# Append exclusion for all files under the module path
35+
EXCLUDE_ARGS+=( -not -path "./${m_trimmed}/*" )
36+
done
37+
fi
38+
39+
# Update all pom.xml files to the new snapshot version, excluding specified modules
40+
# Keep this robust by using a clear sed pattern
41+
find . -type f -name "pom.xml" \
42+
"${EXCLUDE_ARGS[@]}" \
43+
-exec sed -i "s#<version>.*-SNAPSHOT</version>#<version>${VERSION}</version>#g" {} +
44+
45+
# Update gradle.properties to the new snapshot version, if present
46+
if [[ -f gradle.properties ]]; then
47+
sed -i "s/^version=.*-SNAPSHOT$/version=${VERSION}/" gradle.properties || true
48+
fi
49+
50+
echo "Bump complete: VERSION=${VERSION}, EXCLUDED_MODULES=${EXCLUDED_MODULES}"

bump-version/test_bump_version.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Basic tests for bump_version.sh using a temporary workspace
5+
# This script is intended to be run locally or in CI to validate behavior.
6+
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
BUMP_SCRIPT="${SCRIPT_DIR}/bump_version.sh"
9+
10+
assert() {
11+
local msg="$1"; shift
12+
if ! "$@"; then
13+
echo "ASSERT FAIL: ${msg}" >&2
14+
exit 1
15+
fi
16+
return 0
17+
}
18+
19+
# Create temp workspace
20+
TMPDIR="$(mktemp -d)"
21+
cleanup() { rm -rf "$TMPDIR"; return 0; }
22+
trap cleanup EXIT
23+
24+
# Scaffold files
25+
mkdir -p "$TMPDIR/moduleA" "$TMPDIR/moduleB" "$TMPDIR/e2e/projects" "$TMPDIR/e2e/benchmarks"
26+
27+
cat > "$TMPDIR/pom.xml" <<'XML'
28+
<project>
29+
<version>0.0.1-SNAPSHOT</version>
30+
</project>
31+
XML
32+
33+
cat > "$TMPDIR/moduleA/pom.xml" <<'XML'
34+
<project>
35+
<version>0.0.1-SNAPSHOT</version>
36+
</project>
37+
XML
38+
39+
cat > "$TMPDIR/moduleB/pom.xml" <<'XML'
40+
<project>
41+
<version>0.0.1-SNAPSHOT</version>
42+
</project>
43+
XML
44+
45+
cat > "$TMPDIR/gradle.properties" <<'PROPS'
46+
version=0.0.1-SNAPSHOT
47+
PROPS
48+
49+
cat > "$TMPDIR/e2e/projects/pom.xml" <<'XML'
50+
<project>
51+
<version>0.0.1-SNAPSHOT</version>
52+
</project>
53+
XML
54+
55+
cat > "$TMPDIR/e2e/benchmarks/pom.xml" <<'XML'
56+
<project>
57+
<version>0.0.1-SNAPSHOT</version>
58+
</project>
59+
XML
60+
61+
# Run without exclusions
62+
(
63+
cd "$TMPDIR"
64+
VERSION="1.2.3-SNAPSHOT" EXCLUDED_MODULES="" bash "$BUMP_SCRIPT"
65+
)
66+
67+
# Verify root pom updated
68+
assert "root pom updated" grep -q '<version>1.2.3-SNAPSHOT</version>' "$TMPDIR/pom.xml"
69+
# Verify gradle.properties updated
70+
assert "gradle.properties updated" grep -q 'version=1.2.3-SNAPSHOT' "$TMPDIR/gradle.properties"
71+
# Verify e2e files still updated (since we didn't add static exclusions in script)
72+
# Our script does not include static e2e exclusions; test only dynamic
73+
74+
# Reset files
75+
sed -i 's/1.2.3-SNAPSHOT/0.0.1-SNAPSHOT/g' "$TMPDIR"/pom.xml "$TMPDIR"/moduleA/pom.xml "$TMPDIR"/moduleB/pom.xml "$TMPDIR"/e2e/projects/pom.xml "$TMPDIR"/e2e/benchmarks/pom.xml
76+
sed -i 's/version=1.2.3-SNAPSHOT/version=0.0.1-SNAPSHOT/' "$TMPDIR/gradle.properties"
77+
78+
# Run with exclusions for moduleA and e2e/*
79+
(
80+
cd "$TMPDIR"
81+
VERSION="2.0.0-SNAPSHOT" EXCLUDED_MODULES="moduleA,e2e/projects,e2e/benchmarks" bash "$BUMP_SCRIPT"
82+
)
83+
84+
# Root pom should be updated
85+
assert "root pom updated v2" grep -q '<version>2.0.0-SNAPSHOT</version>' "$TMPDIR/pom.xml"
86+
# moduleA pom should remain old due to exclusion
87+
assert "moduleA excluded" grep -q '<version>0.0.1-SNAPSHOT</version>' "$TMPDIR/moduleA/pom.xml"
88+
# moduleB pom should be updated
89+
assert "moduleB updated" grep -q '<version>2.0.0-SNAPSHOT</version>' "$TMPDIR/moduleB/pom.xml"
90+
# e2e files should remain old due to exclusion
91+
assert "e2e projects excluded" grep -q '<version>0.0.1-SNAPSHOT</version>' "$TMPDIR/e2e/projects/pom.xml"
92+
assert "e2e benchmarks excluded" grep -q '<version>0.0.1-SNAPSHOT</version>' "$TMPDIR/e2e/benchmarks/pom.xml"
93+
# gradle.properties updated to v2
94+
assert "gradle.properties v2" grep -q 'version=2.0.0-SNAPSHOT' "$TMPDIR/gradle.properties"
95+
96+
echo "All tests passed"

0 commit comments

Comments
 (0)