FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 71b7abab authored by D. Verma's avatar D. Verma Committed by Dmitrii Unterov
Browse files

feat: Adding versioning in maven.gitlab-ci.yml

parent 29611991
No related branches found
No related tags found
1 merge request!117feat: Adding versioning in maven.gitlab-ci.yml
...@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. ...@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [6.6.0] - 2025-02-17
### Added
- Semantic Versioning in maven.gitlab-ci.yml for Java Applications and Libraries
- Version will be bumped up automatically in `MAJOR.MINOR.PATCH` format depending
on the type of commit message
## [6.5.0] - 2025-01-13 ## [6.5.0] - 2025-01-13
### Changed ### Changed
......
...@@ -49,3 +49,10 @@ The following documents describe support for specific languages or repository ...@@ -49,3 +49,10 @@ The following documents describe support for specific languages or repository
types: types:
* [Python projects](./auto-devops/python.md). * [Python projects](./auto-devops/python.md).
# Automatic Version Management in java libraries
The following documents describe support for semantic versioning in java
libraries.
* [Java projects](./auto-devops/maven.md).
\ No newline at end of file
...@@ -142,18 +142,75 @@ maven:verify: ...@@ -142,18 +142,75 @@ maven:verify:
when: never when: never
- !reference [".maven:verify", rules] - !reference [".maven:verify", rules]
# Template deploy job for pipelines which *are* on the default branch.
.maven:deploy: .maven:deploy:
extends: .maven extends: .maven
script: script: |
- if [ ! -f ci_settings.xml ]; then echo "Starting Maven deploy for $CI_JOB_NAME"
echo "CI settings missing\! If deploying to GitLab Maven Repository, please see https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd for instructions."; # Ensure CI settings file exists
[ -f ci_settings.xml ] || {
echo "Error: ci_settings.xml missing. Follow https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd";
exit 1;
}
# Fetch Maven artifact ID and package versions
MAVEN_ARTIFACT_ID=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout) || {
echo "Error: Failed to retrieve Maven artifact ID.";
exit 1;
}
API_URL="https://gitlab.developers.cam.ac.uk/api/v4/projects/$CI_PROJECT_ID/packages"
RESPONSE=$(curl --silent --header "Authorization: Bearer $CI_JOB_TOKEN" "$API_URL?package_name=$MAVEN_ARTIFACT_ID&package_type=maven")
IS_SNAPSHOT=true
FILTER_CONDITION='/SNAPSHOT/'
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
IS_SNAPSHOT=false
FILTER_CONDITION='!/SNAPSHOT/'
fi
LATEST_VERSION=$(echo "$RESPONSE" | jq -r '.[].version' | awk "$FILTER_CONDITION" | sort -V | tail -n 1 || echo "0.1.0")
echo "Latest version: ${LATEST_VERSION}"
IFS='.' read -r MAJOR MINOR PATCH <<< "${LATEST_VERSION//v/}"
MAJOR=${MAJOR:-0}; MINOR=${MINOR:-0}; PATCH=${PATCH:-0}
# Get commit messages
if [ "$IS_SNAPSHOT" = true ]; then
COMMITS=$(git log -1 --pretty=format:"%s")
else
# Analyze commits since the last version
if git rev-parse "$LATEST_VERSION" >/dev/null 2>&1; then
COMMITS=$(git log ${LATEST_VERSION}..HEAD --pretty=format:"%s")
else
echo "Warning: LATEST_VERSION not found in Git history. Analyzing all commits."
COMMITS=$(git log --pretty=format:"%s")
fi fi
- mvn $MAVEN_CLI_OPTS clean deploy fi
# Determine version bumps
MAJOR_BUMP=$(echo "$COMMITS" | grep -q "BREAKING CHANGE" && echo true || echo false)
MINOR_BUMP=$(echo "$COMMITS" | grep -q "^feat:" && echo true || echo false)
PATCH_BUMP=$(echo "$COMMITS" | grep -q "^fix:" && echo true || echo false)
# Increment version
[[ "$MAJOR_BUMP" = true ]] && { MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0; }
[[ "$MINOR_BUMP" = true ]] && { MINOR=$((MINOR + 1)); PATCH=0; }
[[ "$PATCH_BUMP" = true ]] && PATCH=$((PATCH + 1))
VERSION="${MAJOR}.${MINOR}.${PATCH}"
echo -e "Commit messages:\n${COMMITS}"
[[ $IS_SNAPSHOT == true ]] && VERSION="${VERSION}-SNAPSHOT"
echo "Calculated version: $VERSION"
# Deploy artifact
PROFILE=$([[ $IS_SNAPSHOT == true ]] && echo snapshot || echo release)
[[ $IS_SNAPSHOT == false ]] && git tag -a "v$VERSION" -m "Release $VERSION" && git push origin "v$VERSION"
mvn $MAVEN_CLI_OPTS -Drevision="$VERSION" deploy -P $PROFILE
echo "Deploy completed."
rules: rules:
- if: $MAVEN_DEPLOY_DISABLED - if: $MAVEN_DEPLOY_DISABLED
when: never when: never
- !reference [.maven, rules] - !reference [.maven, rules]
cache:
key: "maven-${CI_COMMIT_REF_SLUG}"
paths:
- .m2/repository
# Production Deploy uses the default maven version. # Production Deploy uses the default maven version.
maven:deploy: maven:deploy:
......
# Automatic Version Management in maven.gitlab-ci.yml
This pipeline script includes an intelligent **Automatic Version Management** system that ensures the Maven artifact's version is incremented appropriately based on semantic versioning rules. It analyzes commit messages and calculates the next version number for both **snapshot** and **release** builds.
---
## How It Works
### 1. Current Version Detection
- The script queries the GitLab Maven repository to identify the latest published version.
- Filters versions into **snapshot** or **release** based on the current branch.
### 2. Semantic Versioning Rules
- Versions follow the `major.minor.patch` format.
- **Snapshot builds**: Version ends with `-SNAPSHOT`.
- **Release builds**: A clean semantic version (e.g., `1.2.3`).
### 3. Commit Message Parsing
The script analyzes Git commit messages since the last version to determine the type of change:
- `BREAKING CHANGE`: Increments the **major** version.
- `feat:`: Increments the **minor** version.
- `fix:`: Increments the **patch** version.
#### Example:
- Current version: `1.2.3`
- Commit messages:
- `feat: Add a new feature` → Next version: `1.3.0`
- `fix: Resolve an issue` → Next version: `1.2.4`
- `BREAKING CHANGE: Major overhaul` → Next version: `2.0.0`
### 4. Version Adjustment
- Extracts the `major`, `minor`, and `patch` components from the latest version.
- Adjusts the version components based on the commit analysis.
### 5. Snapshot vs. Release
- If the current branch is the default branch (e.g., `main` or `master`), a release version is created.
- For all other branches, a snapshot version is used (e.g., `1.3.0-SNAPSHOT`).
---
## Benefits
- **Automated**: No manual intervention is required to manage version numbers.
- **Consistent**: Ensures all artifacts follow semantic versioning standards.
- **Branch-Specific Behavior**: Supports both snapshot and release workflows.
This automatic versioning system simplifies Maven artifact management and ensures consistency across your deployment process.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment