allegro / axion-release-plugin

Gradle release & version management plugin.
https://axion-release-plugin.readthedocs.io/
Apache License 2.0
550 stars 149 forks source link

On GitLab, on a feature branch push to remote, how to create a "snapshot" version with the branch name #606

Open javaworkinggirl opened 1 year ago

javaworkinggirl commented 1 year ago

Hi. I am using gradle kotlin DSL v8.0.2 and GitLab and I have a feature branch named "feature/foo-7". When I push code on that feature branch to remote, how can I create a "snapshot" version with the branch name in the version. So how can I have the result of the version being:

<version>0.1.13-feature-foo-7</version>

OR

<version>0.1.13-feature-foo-7-SNAPSHOT</version>

I tried the following code and I am not getting the branch name in the version:

<version>0.1.13-SNAPSHOT</version>

Here's a snippet of the build.gradle.kts:

configure<pl.allegro.tech.build.axion.release.domain.VersionConfig> {
    scmVersion {
        useHighestVersion.set(true)
        versionIncrementer("incrementPatch")
        versionCreator("versionWithBranch")
    }
}

And here's a snippet of the .gitlab-ci.yml:

publish-snapshot:
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
      - .gradle/caches
  image: gradle:jdk17
  stage: publish-snapshot
  needs: [ "lint-compile-test" ]
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  tags:
    - dev
  before_script:
    - export GRADLE_USER_HOME=`pwd`/.gradle
    - git remote set-url origin ${CI_SERVER_URL}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.git
    - git config --global fetch.prune true
    - git config --global fetch.tags true
  script:
    - gradle fte-1605-alpha:publishAlphaPublicationToXXXCIRepository
    - gradle fte-1605-alpha-api:publishAlpha-apiPublicationToXXXCIRepository

What are your insights? Thank you.

bgalek commented 1 year ago

@javaworkinggirl Hi! I don't know gitlab much, maybe you could setup a gitlab example repo and give me access to check it out?

lgawin commented 3 months ago

The issue is that GitLab by default checkouts the code in detached mode.

@javaworkinggirl I was able to make versionCreator("versionWithBranch") work correctly with these additions to gitlab configuration:

some_job:
  ...
  variables:
    GIT_STRATEGY: clone
  script:
    - git checkout -b $CI_COMMIT_BRANCH
   ...

or simply add

-Prelease.overriddenBranchName=${CI_COMMIT_BRANCH}

to gradle publishing commands