FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects

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

Merged D. Verma requested to merge HRWR-39 into master
1 file
+ 49
6
Compare changes
  • Side-by-side
  • Inline
@@ -142,18 +142,61 @@ 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.";
fi
- mvn $MAVEN_CLI_OPTS clean deploy
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")
# Determine version type and latest version
IS_SNAPSHOT=$([[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]] && echo false || echo true)
LATEST_VERSION=$(echo "$RESPONSE" | jq -r '.[].version' | awk -v snapshot=$IS_SNAPSHOT '
snapshot == "true" && /SNAPSHOT/ || snapshot == "false" && !/SNAPSHOT/ {print $0}' | sort -V | tail -n 1 || echo "0.0.0")
echo "Latest version: $LATEST_VERSION"
IFS='.' read -r MAJOR MINOR PATCH <<< "${LATEST_VERSION//[^0-9.]/}"
MAJOR=${MAJOR:-0}; MINOR=${MINOR:-0}; PATCH=${PATCH:-0}
# Analyze commits for version bump
COMMITS=$(git log ${LATEST_VERSION}..HEAD --pretty=format:"%s" || git log -1 --pretty=format:"%s")
echo "$COMMITS" | awk '/BREAKING CHANGE/ {exit 1}' && MAJOR=$((MAJOR + 1)) && MINOR=0 && PATCH=0
echo "$COMMITS" | awk '/^feat:/ {exit 1}' && [[ $MAJOR -eq 0 ]] && MINOR=$((MINOR + 1)) && PATCH=0
echo "$COMMITS" | awk '/^fix:/ {exit 1}' && [[ $MINOR -eq 0 ]] && PATCH=$((PATCH + 1))
VERSION="$MAJOR.$MINOR.$PATCH"
[[ $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:
- exists:
- ci_settings.xml
- 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:
Loading