1

Inside .gitlab-ci.yml we define a variable (which is just the artifactId name for the project) ARTIFACT_ID: myMicroservice-1

This variable ARTIFACT_ID is sent to a general microservice which has all the scripts to publish/deploy docker, etc.

How can I read this variable direct from POM file?

pom:

<artifactId>myMicroservice-1</artifactId>

.gitlab-ci.yml:
variables:
  SKIP_UNIT_TESTS_FLAG: "true"
  ARTIFACT_ID: myMicroserverName
  IS_OSL: "true"
  KUBERNETES_NAMESPACE: test
Theo
  • 57,719
  • 8
  • 24
  • 41
Angel Lopez
  • 35
  • 1
  • 5

4 Answers4

3

Here is how we do it.

Value is extracted from the pom.xml based on its XPath.
We use xmllint tool from libxml2-utils, but there are various other tools for that.
Then value is saved as an environment variable in a file, which is passed to further GitLab jobs as artifact.

stages:
  - prepare
  - build

variables:
  VARIABLES_FILE: ./variables.txt  # "." is required for sh based images
  POM_FILE: pom.xml

get-version:
  stage: prepare
  image: ubuntu
  script:
    - apt-get update
    - apt-get install -y libxml2-utils
    - APP_VERSION=`xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' $POM_FILE`
    - echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
  artifacts:
    paths:
      - $VARIABLES_FILE

build:
  stage: build
  image: docker:latest
  script:
    - source $VARIABLES_FILE
    - echo "Here use $APP_VERSION as you like"
Ivan
  • 9,089
  • 4
  • 61
  • 74
2
jobname:
  stage: stage
  before_script:
    - export "MAVEN_ID=$(mvn help:evaluate -Dexpression=project.id -q -DforceStdout)"
    - >
      IFS=: read -r MAVEN_GROUPID MAVEN_ARTIFACTID MAVEN_PACKAGING MAVEN_VERSION <<< ${MAVEN_ID}
  script:
    - >
      echo -e "groupId: ${MAVEN_GROUPID}\nartifactId: ${MAVEN_ARTIFACTID}\nversion: ${MAVEN_VERSION}\npackaging: ${MAVEN_PACKAGING}"
  • mvn help:evaluate -Dexpression=project.id -q -DforceStdout prints artifact identification information in the following format: com.group.id:artifactid:packaging:version
  • MAVEN_ID variable is parsed using IFS based on the colon (:) as a separator to get common maven variables like artifactId, groupId, version and packaging (explanation)
  • later these variables can be used in the code, for example for echoing values
  • IFS is a bash feature, thus corresponding GitLab runner should have bash installed
ololoid
  • 63
  • 5
1

This is the script to grab information from pom, in this case, artifactId:

    - export myARTIFACT_ID=$(mvn exec:exec -q -Dexec.executable=echo -Dexec.args='${project.artifactId}')
    - if [[ "$myARTIFACT_ID" == *finishWithWhatEverName]]; then export myVariable="false"; else export myVariable="true";

Then you can use myVariable to whatever you want.

Angel Lopez
  • 35
  • 1
  • 5
1

I tried Angel's solution, but got an error:

/bin/bash: line 87: mvn: command not found

Finally I succeeded when I used the following to extract the tag value:

- export ARTIFACT_ID=$(cat pom.xml | grep "<artifactId>" | head -1 | cut -d">" -f2 | cut -d"<" -f1 | awk '{$1=$1;print}')
Compile Commit
  • 375
  • 1
  • 5
  • 17