diff --git a/README.md b/README.md index 8fdef2c2dd031af958ff12650000a309b1c93c5b..7f25b36d56440ed5f74bf2413e4be83383624eef 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,7 @@ They are bound to the `publish` stage, and use the following variables: | ----------------------------------- | ------------------------------------------------------------ | ----------------- | | `MAVEN_DEPLOY_ENABLED` | Set to `true` to enable release and publish jobs | _none_ (disabled) | | `MAVEN_DEPLOY_FROM_UNPROTECTED_DISABLED`| Set to `true` to limit snapshot publication to protected branches | _none_ (disabled) | +| `MAVEN_DEPLOY_SNAPSHOT_WITH_SLUG_ENABLED`| Set to `true` to inject the Git branch slug in SNAPSHOT versions | _none_ (disabled) | | `MAVEN_DEPLOY_ARGS` | Maven arguments for the `mvn-deploy` job | `deploy -Dmaven.test.skip=true` | | `MAVEN_RELEASE_ARGS` | Maven arguments for the `mvn-release` job | `release:prepare -DtagNameFormat=@{project.version} -Darguments=-Dmaven.test.skip=true` | | `MAVEN_RELEASE_VERSION` | Explicit version to use when triggering a release | _none_ (uses the current snapshot version from `pom.xml`) | @@ -218,6 +219,23 @@ More info: * [Maven Deploy Plugin](https://maven.apache.org/plugins/maven-deploy-plugin/) * [Maven Release Plugin](http://maven.apache.org/maven-release/maven-release-plugin/index.html) +#### Inject the Git branch slug in SNAPSHOT versions + +In order to **segregate your SNAPSHOT versions per branch**, the Maven template allows you to add the Git branch slug into the pom version. +Thus, any other project willing to use your SNAPSHOT packages will be able to do so, explicitly setting its dependency on the desired version (branch). +Only the production branch (`main` or `master` by default) will be preserved from this behavior. + +This feature can be enabled setting `MAVEN_DEPLOY_SNAPSHOT_WITH_SLUG_ENABLED` to `true`. + +Example: for a project with `<version>1.2.1-SNAPSHOT</version>` in its `pom.xml`: + +| Branch | Altered version | +| -------------------------------- | ------------------------------------------------------------ | +| `feature/support-facebook-login` | `1.2.1-feature-support-facebook-login-SNAPSHOT` | +| `fix/timedate-serialization` | `1.2.1-fix-timedate-serialization-SNAPSHOT` | +| `develop` (integration branch) | `1.2.1-develop-SNAPSHOT` | +| `main` (production branch) | `1.2.1-SNAPSHOT` | + #### `semantic-release` integration If you activate the [`semantic-release-info` job from the `semantic-release` template](https://gitlab.com/to-be-continuous/semantic-release/#semantic-release-info-job), the `mvn-release` job will rely on the generated next version info. diff --git a/kicker.json b/kicker.json index c61cfb1f4cc5f7b999d0b3a035780716a7f77d66..f9f4177bb1ce03611103a35751e2d77f395c097f 100644 --- a/kicker.json +++ b/kicker.json @@ -138,6 +138,12 @@ "type": "boolean", "advanced": true }, + { + "name": "MAVEN_DEPLOY_SNAPSHOT_WITH_SLUG_ENABLED", + "description": "Enable to inject the Git branch slug in SNAPSHOT versions", + "type": "boolean", + "advanced": true + }, { "name": "MAVEN_RELEASE_ARGS", "description": "Maven arguments for the release job", diff --git a/templates/gitlab-ci-maven.yml b/templates/gitlab-ci-maven.yml index c26886937132d5d885890bc08d940951d34e0ae6..32b3d3ca9c24cc7af1e95778eebba476dc0ee986 100644 --- a/templates/gitlab-ci-maven.yml +++ b/templates/gitlab-ci-maven.yml @@ -396,6 +396,25 @@ stages: fi } + # only on a branch commit, with deploy and "SNAPSHOT with slug" enabled + function maybe_inject_slug_in_version() { + if [[ "$CI_COMMIT_BRANCH" ]] && [[ "$MAVEN_DEPLOY_ENABLED" == "true" ]] && [[ "$MAVEN_DEPLOY_SNAPSHOT_WITH_SLUG_ENABLED" == "true" ]] + then + # check if on non-prod branch + prod_ref_expr=${PROD_REF#/} + prod_ref_expr=${prod_ref_expr%/} + if [[ ! "$CI_COMMIT_BRANCH" =~ $prod_ref_expr ]] + then + # shellcheck disable=SC2086 + pom_version=$(mvn $MAVEN_CLI_OPTS $mvn_settings_opt $java_proxy_args help:evaluate -Dexpression=project.version -q -DforceStdout | tail -n 1) + altered_pom_version="${pom_version%-SNAPSHOT}-${CI_COMMIT_REF_SLUG}-SNAPSHOT" + log_info "Inject branch slug into SNAPSHOT version: \\e[33;1m${altered_pom_version}\\e[0m..." + # shellcheck disable=SC2086 + mvn $MAVEN_CLI_OPTS $mvn_settings_opt $java_proxy_args versions:set -DgenerateBackupPoms=false -DnewVersion="${altered_pom_version}" + fi + fi + } + unscope_variables # ENDSCRIPT @@ -423,6 +442,7 @@ mvn-build: extends: .mvn-base stage: build script: + - maybe_inject_slug_in_version - mvn ${TRACE+-X} $MAVEN_CLI_OPTS $mvn_settings_opt $java_proxy_args $MAVEN_BUILD_ARGS - output_coverage # code coverage RegEx @@ -436,6 +456,8 @@ mvn-build: junit: - "${MAVEN_PROJECT_DIR}/**/target/*-reports/TEST-*.xml" paths: + # version may have been altered + - pom.xml - "${MAVEN_PROJECT_DIR}/**/target" # Sonar job