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
  • 49,970
  • 8
  • 20
  • 38
Angel Lopez
  • 39
  • 1
  • 4

3 Answers3

2

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
  • 8,043
  • 4
  • 53
  • 69
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
  • 39
  • 1
  • 4
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}')
Avi Bis
  • 303
  • 1
  • 3
  • 13