I made simple microservices structure in spring boot (three modules) and I wanted to conigure gitlab ci/cd for autoincrement version number in pom of changed microservice. So I created step in my .gitlab-ci.yml:
.increment-version-module:
stage: increment version
image: ssmolinski9/docker-adoptopenjdk-11-maven-node
except:
variables:
- $GITLAB_USER_LOGIN == "login"
- $CI_COMMIT_BRANCH == "master"
before_script:
- git config --global user.email "mymail"
- git config --global push.default matching
- git config --global user.name "myname"
- git config --global user.password "$CI_INCREMENTION_PWD"
script:
- NUMBER=$(grep \<\/version $MODULE/pom.xml | head -n 1 | cut -d '>' -f2 | cut -d '<' -f1)
- NUMBER_1=$(echo $NUMBER | cut -d '.' -f1)
- NUMBER_2=$(echo $NUMBER | cut -d '.' -f2)
- NUMBER_3=$(echo $NUMBER | cut -d '.' -f3)
- NUMBER_3=$((NUMBER_3+1))
- NUMBER_new="$NUMBER_1"."$NUMBER_2"."$NUMBER_3"
- sed -i 's/\<version\>$NUMBER\<\/version\>/\<version\>$NUMBER_new\<\/version\>/g' $MODULE/pom.xml
- echo $NUMBER_new
- mvn $MAVEN_CLI_OPTS -pl $MODULE versions:set -B -DnewVersion=$NUMBER_new -f pom.xml
- mvn $MAVEN_CLI_OPTS -pl $MODULE versions:commit -B -DprocessAllModules -f pom.xml
- git commit -a -m ''$MODULE' POM Version Increment '$NUMBER_new''
- git pull https://myname:[email protected]/url/to/my/repo/api.git HEAD:$CI_COMMIT_REF_NAME --prune --rebase
- git push https://myname:[email protected]/url/to/my/repo/api.git HEAD:$CI_COMMIT_REF_NAME -u -f
Then I created three jobs for every module I have, eg:
increment-discovery-service:
extends:
- .discovery-service
- .increment-version-module
resource_group: incrementing
I though it work, my pipeline is in SUCCESS state but... When I pulled my changes only one (last) of three commits still exists. Everything inside pipeline log is correct (new version, create commit, push), but I think -f option in git push is messing up.
Any ideas?
I think I got it. I have to pull from $CI_COMMIT_REF_NAME instead of HEAD:$CI_COMMIT_REF_NAME. I changed it and everything seems to work correct. I'll present full solution if someone need it in the future.
My modules structure:
--discovery-service
---pom.xml
---.gitlab-ci.yml
--dummy-service
---pom.xml
---.gitlab-ci.yml
--gateway-service
---pom.xml
---.gitlab-ci.yml
-.gitlab-ci.yml
-.pom.xml
So I have pom.xml and .gitlab-ci.yml in each microservice's module and one for root.
Each .gitlab-ci.yml inside microservice looks like:
.discovery-service:
variables:
MODULE: "discovery-service"
only:
changes:
- "discovery-service/**/*"
In main .gitlab-ci.yml we have to include microservices' configurations:
include:
- local: discovery-service/.gitlab-ci.yml
- local: dummy-service/.gitlab-ci.yml
- local: gateway-service/.gitlab-ci.yml
.increment-version-module I listed in the first post. Only change pull from $CI_COMMIT_REF_NAME without HEAD. Last thing you have to make job for every module:
increment-discovery-service:
extends:
- .discovery-service
- .increment-version-module
resource_group: incrementing
increment-dummy-service:
extends:
- .dummy-service
- .increment-version-module
resource_group: incrementing
increment-gateway-service:
extends:
- .gateway-service
- .increment-version-module
resource_group: incrementing