diff --git a/CHANGELOG.md b/CHANGELOG.md index bfe5ab339e37cf33732f96d9596f0229f1281357..75478b197b6d5ef6db642fdb72358815f6411617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 ### Changed diff --git a/README.md b/README.md index fe7b5175e4678153386bf706866ac917726acbc8..515c238143cd286984595b0f39b1b363b591468a 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,10 @@ The following documents describe support for specific languages or repository types: * [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 diff --git a/auto-devops/maven.gitlab-ci.yml b/auto-devops/maven.gitlab-ci.yml index a8c669afd1257cd8b28d41a41e39bfa22130d8c7..a9a5590e7ed7ad269f98cb4eb0950696ad7c7bbb 100644 --- a/auto-devops/maven.gitlab-ci.yml +++ b/auto-devops/maven.gitlab-ci.yml @@ -142,18 +142,75 @@ maven:verify: when: never - !reference [".maven:verify", rules] -# Template deploy job for pipelines which *are* on the default branch. .maven:deploy: extends: .maven - script: - - if [ ! -f ci_settings.xml ]; then - 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."; + script: | + echo "Starting Maven deploy for $CI_JOB_NAME" + # 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 - - 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: - if: $MAVEN_DEPLOY_DISABLED when: never - !reference [.maven, rules] + cache: + key: "maven-${CI_COMMIT_REF_SLUG}" + paths: + - .m2/repository + # Production Deploy uses the default maven version. maven:deploy: diff --git a/auto-devops/maven.md b/auto-devops/maven.md new file mode 100644 index 0000000000000000000000000000000000000000..46bb4af6b92640093bcb6565bdcc89f84dbe6527 --- /dev/null +++ b/auto-devops/maven.md @@ -0,0 +1,48 @@ +# 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.