Skip to content

Commit d249aaa

Browse files
committed
BUILD-9398 Set project version in config-maven
1 parent 1e199f7 commit d249aaa

File tree

13 files changed

+307
-124
lines changed

13 files changed

+307
-124
lines changed

.shellspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# kcov (coverage) options
2-
--kcov-options "--include-pattern=build-poetry,get-build-number,pr_cleanup,promote,build-gradle,config-maven,build-maven,config-npm,build-npm,build-yarn,shared"
2+
--kcov-options "--include-pattern=build-poetry,get-build-number,pr_cleanup,promote,build-gradle,config-maven,build-maven,config-npm,build-npm,build-yarn,shared,config-gradle"
33
# --kcov-options "--exclude-pattern=.github,.idea,.git"
44

55
# define minimum coverage (fail otherwise)

build-gradle/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ inputs:
7373
outputs:
7474
project-version:
7575
description: The release version set as Gradle project version in gradle.properties
76-
value: ${{ steps.build.outputs.project-version }}
76+
value: ${{ steps.config-gradle.outputs.project-version }}
7777
BUILD_NUMBER:
7878
description: The build number, incremented or reused if already cached
7979
value: ${{ steps.config-gradle.outputs.BUILD_NUMBER }}

build-gradle/build.sh

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# - SQC_EU_URL: URL of SonarQube server for sqc-eu platform
1313
# - SQC_EU_TOKEN: Access token to send analysis reports to SonarQube for sqc-eu platform
1414
# - RUN_SHADOW_SCANS: If true, run sonar scanner on all 3 platforms. If false, run on the platform provided by SONAR_PLATFORM.
15+
# - CURRENT_VERSION: Current project version as in gradle.properties
1516
# - ARTIFACTORY_ACCESS_TOKEN: Access token to read Repox repositories
1617
# - ARTIFACTORY_DEPLOY_REPO: Name of deployment repository
1718
# - ARTIFACTORY_DEPLOY_USERNAME: Username to deploy to Artifactory
@@ -52,6 +53,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/../shared/common-functions.sh"
5253
: "${GITHUB_OUTPUT:?}"
5354
: "${PULL_REQUEST?}" "${DEFAULT_BRANCH:?}"
5455
: "${RUN_SHADOW_SCANS:?}"
56+
: "${CURRENT_VERSION:?}"
5557
if [[ "${SONAR_PLATFORM:?}" != "none" ]]; then
5658
: "${NEXT_URL:?}" "${NEXT_TOKEN:?}" "${SQC_US_URL:?}" "${SQC_US_TOKEN:?}" "${SQC_EU_URL:?}" "${SQC_EU_TOKEN:?}"
5759
fi
@@ -82,26 +84,6 @@ set_build_env() {
8284
git_fetch_unshallow
8385
}
8486

85-
set_project_version() {
86-
current_version=$($GRADLE_CMD properties --no-scan --no-daemon --console plain | grep 'version:' | tr -d "[:space:]" | cut -d ":" -f 2)
87-
if [[ -z "$current_version" || "$current_version" == "unspecified" ]]; then
88-
echo "ERROR: Could not get valid version from Gradle properties. Got: '$current_version'" >&2
89-
exit 1
90-
fi
91-
export CURRENT_VERSION=$current_version
92-
release_version="${current_version/-SNAPSHOT/}"
93-
if [[ "${release_version}" =~ ^[0-9]+\.[0-9]+$ ]]; then
94-
release_version="${release_version}.0"
95-
fi
96-
release_version="${release_version}.${BUILD_NUMBER}"
97-
echo "Replacing version $current_version with $release_version"
98-
sed -i.bak "s/$current_version/$release_version/g" gradle.properties
99-
echo "project-version=$release_version" >> "$GITHUB_OUTPUT"
100-
echo "PROJECT_VERSION=$release_version" >> "$GITHUB_ENV"
101-
echo "PROJECT_VERSION=$release_version"
102-
export PROJECT_VERSION=$release_version
103-
}
104-
10587
should_deploy() {
10688
# Disable deployment when explicitly requested
10789
if [[ "${DEPLOY}" != "true" ]]; then
@@ -300,7 +282,6 @@ main() {
300282
check_tool java -version
301283
set_gradle_cmd
302284
set_build_env
303-
set_project_version
304285
gradle_build
305286
}
306287

build-maven/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# - ARTIFACTORY_DEPLOY_REPO: Deployment repository name
1818
# - ARTIFACTORY_DEPLOY_USERNAME: Username used by artifactory-maven-plugin
1919
# - ARTIFACTORY_DEPLOY_PASSWORD: Access token to deploy to the repository
20+
# - CURRENT_VERSION: Current project version as in pom.xml
2021
# - DEFAULT_BRANCH: Default branch name (e.g. main)
2122
# - PULL_REQUEST: Pull request number (e.g. 1234) or empty string
2223
#

config-gradle/action.yml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ inputs:
3333
outputs:
3434
BUILD_NUMBER:
3535
description: The build number, incremented or reused if already cached
36-
value: ${{ steps.get_build_number.outputs.BUILD_NUMBER }}
36+
value: ${{ steps.get-build-number.outputs.BUILD_NUMBER }}
37+
current-version:
38+
description: The project version set in the gradle.properties (before replacement). Also set as environment variable CURRENT_VERSION
39+
value: ${{ steps.set-version.outputs.current-version }}
40+
project-version:
41+
description: The project version with build number (after replacement). Also set as environment variable PROJECT_VERSION
42+
value: ${{ steps.set-version.outputs.project-version }}
3743

3844
runs:
3945
using: composite
@@ -63,12 +69,20 @@ runs:
6369
echo "::endgroup::"
6470
6571
- name: Get build number
66-
id: get_build_number
72+
id: get-build-number
6773
uses: ./.actions/get-build-number
6874
with:
6975
host-actions-root: ${{ steps.set-path.outputs.host_actions_root }}
7076

77+
- id: config-gradle-completed
78+
if: env.CONFIG_GRADLE_COMPLETED != ''
79+
shell: bash
80+
run: |
81+
echo "Action already called by $CONFIG_GRADLE_COMPLETED, execution skipped."
82+
echo "skip=true" >> $GITHUB_OUTPUT
83+
7184
- name: Set parameter for Vault
85+
if: steps.config-gradle-completed.outputs.skip != 'true'
7286
shell: bash
7387
env:
7488
ARTIFACTORY_READER_ROLE: ${{ inputs.artifactory-reader-role != '' && inputs.artifactory-reader-role ||
@@ -78,6 +92,7 @@ runs:
7892
7993
- name: Get secrets from Vault
8094
id: secrets
95+
if: steps.config-gradle-completed.outputs.skip != 'true'
8196
uses: SonarSource/vault-action-wrapper@320bd31b03e5dacaac6be51bbbb15adf7caccc32 # 3.1.0
8297
with:
8398
secrets: |
@@ -86,6 +101,7 @@ runs:
86101
${{ inputs.use-develocity == 'true' && 'development/kv/data/develocity token | DEVELOCITY_TOKEN;' || '' }}
87102
88103
- name: Set environment variables for Artifactory authentication
104+
if: steps.config-gradle-completed.outputs.skip != 'true'
89105
shell: bash
90106
env:
91107
ARTIFACTORY_URL:
@@ -117,14 +133,15 @@ runs:
117133
118134
# Configure Gradle for comprehensive caching and build summary reporting. It does not install Gradle.
119135
- name: Configure Gradle
136+
if: steps.config-gradle-completed.outputs.skip != 'true'
120137
uses: gradle/actions/setup-gradle@748248ddd2a24f49513d8f472f81c3a07d4d50e1 # v4.4.4
121138
with:
122139
develocity-injection-enabled: ${{ inputs.use-develocity == 'true' }}
123140
cache-disabled: true
124141
develocity-plugin-version: '4.0'
125142

126143
- name: Generate Gradle Cache Key
127-
if: ${{ inputs.disable-caching != 'true' }}
144+
if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.disable-caching != 'true'
128145
shell: bash
129146
run: |
130147
# Generate cache key from all Gradle files
@@ -139,7 +156,7 @@ runs:
139156
rm -f gradle-md5-sums.txt
140157
141158
- name: Restore Gradle Cache
142-
if: ${{ inputs.disable-caching != 'true' }}
159+
if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.disable-caching != 'true'
143160
uses: SonarSource/gh-action_cache@v1
144161
id: gradle-cache-restore
145162
with:
@@ -150,22 +167,34 @@ runs:
150167
151168
# $GRADLE_USER_HOME is typically set to ~/.gradle/ by gradle/actions/setup-gradle
152169
- name: Configure Gradle Authentication
170+
if: steps.config-gradle-completed.outputs.skip != 'true'
153171
shell: bash
154172
run: |
155173
GRADLE_INIT_DIR="$GRADLE_USER_HOME/init.d"
156174
mkdir -p "$GRADLE_INIT_DIR"
157175
cp "${GITHUB_ACTION_PATH}/resources/repoxAuth.init.gradle.kts" "$GRADLE_INIT_DIR/"
158176
177+
- name: Update project version and set current-version and project-version variables
178+
id: set-version
179+
if: steps.config-gradle-completed.outputs.skip != 'true' && (env.CURRENT_VERSION == '' || env.PROJECT_VERSION == '')
180+
shell: bash
181+
run: ${GITHUB_ACTION_PATH}/set_gradle_project_version.sh
182+
159183
- name: Extract Develocity hostname
160184
id: develocity-hostname
161-
if: ${{ inputs.use-develocity == 'true' }}
185+
if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.use-develocity == 'true'
162186
shell: bash
163187
run: echo "hostname=$(echo '${{ inputs.develocity-url }}' | sed 's|https://||' | sed 's|/$||')" >> $GITHUB_OUTPUT
164188

165189
- name: Deactivate UseContainerSupport on github-ubuntu-* runners
166-
if: runner.os == 'Linux' && runner.environment == 'github-hosted'
190+
if: steps.config-gradle-completed.outputs.skip != 'true' && runner.os == 'Linux' && runner.environment == 'github-hosted'
167191
shell: bash
168192
run: |
169193
echo "::warning title=Deactivating UseContainerSupport::The GitHub-hosted Ubuntu runners have an issue with Java's" \
170194
"UseContainerSupport feature."
171195
echo "JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS:=-XX:-UseContainerSupport}" >> "$GITHUB_ENV"
196+
197+
- name: Set Config Gradle completed
198+
if: steps.config-gradle-completed.outputs.skip != 'true'
199+
shell: bash
200+
run: echo "CONFIG_GRADLE_COMPLETED=$GITHUB_ACTION" >> "$GITHUB_ENV"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
# Config script for SonarSource Maven projects.
3+
#
4+
# Required environment variables (must be explicitly provided):
5+
# - BUILD_NUMBER: Build number for versioning
6+
#
7+
# GitHub Actions auto-provided:
8+
# - GITHUB_OUTPUT: Path to GitHub Actions output file
9+
# - GITHUB_ENV: Path to GitHub Actions environment file
10+
#
11+
# Optional user customization:
12+
# - CURRENT_VERSION and PROJECT_VERSION: If both are set, they will be used as-is and no version update will be performed.
13+
14+
set -euo pipefail
15+
16+
: "${BUILD_NUMBER:?}"
17+
: "${GITHUB_OUTPUT:?}" "${GITHUB_ENV:?}"
18+
19+
# shellcheck source=SCRIPTDIR/../shared/common-functions.sh
20+
source "$(dirname "${BASH_SOURCE[0]}")/../shared/common-functions.sh"
21+
22+
set_gradle_cmd() {
23+
if [[ -f "./gradlew" ]]; then
24+
export GRADLE_CMD="./gradlew"
25+
elif check_tool gradle; then
26+
export GRADLE_CMD="gradle"
27+
else
28+
echo "Neither ./gradlew nor gradle command found!" >&2
29+
exit 1
30+
fi
31+
}
32+
33+
set_project_version() {
34+
local current_version
35+
current_version=$($GRADLE_CMD properties --no-scan --no-daemon --console plain | grep 'version:' | tr -d "[:space:]" | cut -d ":" -f 2)
36+
if [[ -z "$current_version" || "$current_version" == "unspecified" ]]; then
37+
echo "ERROR: Could not get valid version from Gradle properties. Got: '$current_version'" >&2
38+
return 1
39+
fi
40+
41+
# Saving the snapshot version to the output and environment variables
42+
# This is used by the sonar-scanner to set the value of sonar.projectVersion without the build number
43+
echo "CURRENT_VERSION=$current_version"
44+
echo "CURRENT_VERSION=$current_version" >> "$GITHUB_ENV"
45+
export CURRENT_VERSION=$current_version
46+
47+
release_version="${current_version/-SNAPSHOT/}"
48+
if [[ "${release_version}" =~ ^[0-9]+\.[0-9]+$ ]]; then
49+
release_version="${release_version}.0"
50+
fi
51+
release_version="${release_version}.${BUILD_NUMBER}"
52+
echo "Replacing version $current_version with $release_version"
53+
sed -i.bak "s/$current_version/$release_version/g" gradle.properties
54+
echo "project-version=$release_version" >> "$GITHUB_OUTPUT"
55+
echo "PROJECT_VERSION=$release_version" >> "$GITHUB_ENV"
56+
echo "PROJECT_VERSION=$release_version"
57+
export PROJECT_VERSION=$release_version
58+
}
59+
60+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
61+
set_gradle_cmd
62+
set_project_version
63+
fi

config-maven/action.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ inputs:
3737
outputs:
3838
BUILD_NUMBER:
3939
description: The current build number. Also set as environment variable BUILD_NUMBER
40-
value: ${{ steps.get_build_number.outputs.BUILD_NUMBER }}
40+
value: ${{ steps.get-build-number.outputs.BUILD_NUMBER }}
4141
current-version:
4242
description: The project version set in the pom.xml (before replacement). Also set as environment variable CURRENT_VERSION
43-
value: ${{ steps.config.outputs.current-version }}
43+
value: ${{ steps.set-version.outputs.current-version }}
4444
project-version:
4545
description: The project version with build number (after replacement). Also set as environment variable PROJECT_VERSION
46-
value: ${{ steps.config.outputs.project-version }}
46+
value: ${{ steps.set-version.outputs.project-version }}
4747

4848
runs:
4949
using: composite
@@ -73,8 +73,9 @@ runs:
7373
ls -la .actions/*
7474
echo "::endgroup::"
7575
76-
- uses: ./.actions/get-build-number
77-
id: get_build_number
76+
- name: Get build number
77+
id: get-build-number
78+
uses: ./.actions/get-build-number
7879
with:
7980
host-actions-root: ${{ steps.set-path.outputs.host_actions_root }}
8081

@@ -173,8 +174,8 @@ runs:
173174
restore-keys: maven-${{ runner.os }}-${{ github.workflow }}-
174175

175176
- name: Update project version and set current-version and project-version variables
176-
id: config
177-
if: steps.from-env.outputs.skip != 'true'
177+
id: set-version
178+
if: steps.from-env.outputs.skip != 'true' && (env.CURRENT_VERSION == '' || env.PROJECT_VERSION == '')
178179
shell: bash
179180
working-directory: ${{ inputs.working-directory }}
180181
run: $ACTION_PATH_CONFIG_MAVEN/set_maven_project_version.sh

config-maven/set_maven_project_version.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ get_current_version() {
3434
# Update current_version variable with the current project version.
3535
# Then remove the -SNAPSHOT suffix if present, complete with '.0' if needed, and append the build number at the end.
3636
set_project_version() {
37-
if [[ -n "${CURRENT_VERSION:-}" && -n "${PROJECT_VERSION:-}" ]]; then
38-
echo "Using provided CURRENT_VERSION $CURRENT_VERSION and PROJECT_VERSION $PROJECT_VERSION without changes."
39-
echo "current-version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
40-
echo "project-version=$PROJECT_VERSION" >> "$GITHUB_OUTPUT"
41-
return 0
42-
fi
43-
4437
local current_version
4538
if ! current_version=$(get_current_version 2>&1); then
4639
echo -e "::error file=pom.xml,title=Maven project version::Could not get 'project.version' from Maven project\nERROR: $current_version"

spec/build-gradle_spec.sh

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export DEPLOY_PULL_REQUEST="false"
6262
export SKIP_TESTS="false"
6363
export GRADLE_ARGS=""
6464
export GITHUB_OUTPUT=/dev/null
65+
export CURRENT_VERSION="1.2.3-SNAPSHOT"
6566
# Duplicate environment variables removed
6667
GITHUB_EVENT_PATH=$(mktemp)
6768
export GITHUB_EVENT_PATH
@@ -129,59 +130,6 @@ Describe 'set_build_env'
129130
End
130131
End
131132

132-
Describe 'set_project_version'
133-
It 'processes version correctly'
134-
echo "version=1.0-SNAPSHOT" > gradle.properties
135-
When call set_project_version
136-
The output should include "Replacing version 1.2.3-SNAPSHOT with 1.2.3.42"
137-
The variable CURRENT_VERSION should equal "1.2.3-SNAPSHOT"
138-
The variable PROJECT_VERSION should equal "1.2.3.42"
139-
rm -f gradle.properties gradle.properties.bak
140-
End
141-
142-
It 'adds .0 to two-digit version'
143-
echo "version=1.2-SNAPSHOT" > gradle.properties
144-
Mock gradle
145-
echo "version: 1.2-SNAPSHOT"
146-
End
147-
When call set_project_version
148-
The output should include "Replacing version 1.2-SNAPSHOT with 1.2.0.42"
149-
The variable CURRENT_VERSION should equal "1.2-SNAPSHOT"
150-
The variable PROJECT_VERSION should equal "1.2.0.42"
151-
rm -f gradle.properties gradle.properties.bak
152-
End
153-
154-
It 'fails when version is empty'
155-
echo "version=1.0-SNAPSHOT" > gradle.properties
156-
Mock gradle
157-
if [[ "$*" == "properties --no-scan --no-daemon --console plain" ]]; then
158-
echo "version: "
159-
else
160-
echo "gradle $*"
161-
fi
162-
End
163-
When run set_project_version
164-
The status should be failure
165-
The stderr should include "ERROR: Could not get valid version from Gradle properties"
166-
rm -f gradle.properties gradle.properties.bak
167-
End
168-
169-
It 'fails when version is unspecified'
170-
echo "version=1.0-SNAPSHOT" > gradle.properties
171-
Mock gradle
172-
if [[ "$*" == "properties --no-scan --no-daemon --console plain" ]]; then
173-
echo "version: unspecified"
174-
else
175-
echo "gradle $*"
176-
fi
177-
End
178-
When run set_project_version
179-
The status should be failure
180-
The stderr should include "ERROR: Could not get valid version from Gradle properties. Got: 'unspecified'"
181-
rm -f gradle.properties gradle.properties.bak
182-
End
183-
End
184-
185133
Describe 'should_deploy'
186134
It 'does not deploy when deployment is disabled'
187135
export DEPLOY="false"
@@ -537,9 +485,6 @@ Describe 'main function'
537485
Mock set_build_env
538486
echo "env set"
539487
End
540-
Mock set_project_version
541-
echo "version set"
542-
End
543488
Mock gradle_build
544489
echo "build done"
545490
End
@@ -549,8 +494,7 @@ Describe 'main function'
549494
The line 1 should equal "java ok"
550495
The line 2 should equal "gradle ok"
551496
The line 3 should equal "env set"
552-
The line 4 should equal "version set"
553-
The line 5 should equal "build done"
497+
The line 4 should equal "build done"
554498
End
555499
End
556500

0 commit comments

Comments
 (0)