【问题标题】:set inside .gitlab-ci.yml a variable reading the name of artifactId from pom在 .gitlab-ci.yml 中设置一个从 pom 读取 artifactId 名称的变量
【发布时间】:2019-11-22 08:10:36
【问题描述】:

在 .gitlab-ci.yml 中我们定义了一个变量(这只是项目的 artifactId 名称)ARTIFACT_ID: myMicroservice-1

这个变量ARTIFACT_ID 被发送到一个通用的微服务,该微服务具有发布/部署 docker 等的所有脚本。

如何直接从 POM 文件中读取这个变量?

pom:

<artifactId>myMicroservice-1</artifactId>

.gitlab-ci.yml:
variables:
  SKIP_UNIT_TESTS_FLAG: "true"
  ARTIFACT_ID: myMicroserverName
  IS_OSL: "true"
  KUBERNETES_NAMESPACE: test

【问题讨论】:

    标签: maven gitlab-ci


    【解决方案1】:

    这是我们的做法。

    值是根据其 XPath 从 pom.xml 中提取的。
    我们使用来自libxml2-utilsxmllint 工具,但还有其他各种工具。
    然后将值保存为文件中的环境变量,该文件作为工件传递给其他 GitLab 作业。

    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"
    

    【讨论】:

      【解决方案2】:

      这是从 pom 中获取信息的脚本,在本例中为 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";
      

      然后你可以使用 myVariable 来做任何你想要的。

      【讨论】:

        【解决方案3】:

        我尝试了Angel的解决方案,但出现错误:

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

        当我使用下面的方法提取标签值时,我终于成功了:

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

        【讨论】:

        • 我相信不需要awk '{$1=$1;print}'
        猜你喜欢
        • 2021-01-28
        • 2020-08-19
        • 2021-11-13
        • 1970-01-01
        • 2023-01-13
        • 2022-06-23
        • 2023-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多