带有docker executor:maven的gitlab ci无法检索scmbrach,buildScmBranch



我使用带有docker的Gitlab ci作为执行器来构建我的maven项目。

我使用buildnumber maven插件v1.4来检索git信息,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath />
</parent>
<groupId>com.xxxxxx</groupId>
<artifactId>xxxxxx</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<!-- ********************************************************** -->
<!--            PROPERTIES                                      -->
<!-- ********************************************************** -->
<properties>
<revision>0.1.0-SNAPSHOT</revision>
<xxxxxx.boilerplate.version>0.5.2-SNAPSHOT</xxxxxxxx.boilerplate.version>
<spring.framework.versoin>2.2.1.RELEASE</spring.framework.versoin>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- ********************************************************** -->
<!--            MODULES                                         -->
<!-- ********************************************************** -->
<modules>
<module>client-ui</module>
<module>application</module>
<module>domain</module>
<module>infrastructure</module>
</modules>
<!-- ********************************************************** -->
<!--            REPOSITORIES                                    -->
<!-- ********************************************************** -->
<repositories>
<repository>
<id>java-boilerplate-maven-bucket-release</id>
<name>xxxxxx Java BoilerPlate</name>
<url>s3://xxxxxxxxxx/release</url>
</repository>
<repository>
<id>java-boilerplate-maven-bucket-snapshot</id>
<name>xxxxxx Java BoilerPlate</name>
<url>s3://xxxxxxxxxx/snapshot</url>
</repository>
</repositories>
<!-- ********************************************************** -->
<!--            DEPENDENCIES MANAGEMENT                         -->
<!-- ********************************************************** -->
<dependencyManagement>
<!-- xxxxxxx JAVA BOILERPLATE        -->
<dependencies>
<dependency>
<groupId>com.xxxxxxx</groupId>
<artifactId>infrastructure</artifactId>
<version>${xxxxxx.boilerplate.version}</version>
</dependency>
<dependency>
<groupId>com.xxxxxxxx</groupId>
<artifactId>domain</artifactId>
<version>${xxxxxx.boilerplate.version}</version>
</dependency>
<dependency>
<groupId>com.xxxxxxx</groupId>
<artifactId>security</artifactId>
<version>${xxxxxxx.boilerplate.version}</version>
</dependency>
<!-- SPRING BOOT WEB STARTER            -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.framework.versoin}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data</artifactId>
<version>${spring.framework.versoin}</version>
</dependency>
<!-- SPRING BOOT TEST STARTER           -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.framework.versoin}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- ********************************************************** -->
<!--            DEPENDENCIES                                    -->
<!-- ********************************************************** -->
<dependencies>
</dependencies>
<!-- ********************************************************** -->
<!--            VERSIONING                                      -->
<!-- ********************************************************** -->
<scm>
<developerConnection>scm:git:ssh://git@xxx.Xxxx.XXX.XXX:xxxx/xxxxxx.git</developerConnection>
</scm>
<!-- ********************************************************** -->
<!--            BUILD                                           -->
<!-- ********************************************************** -->
<build>
<!--            FILE NAME                                   -->
<finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
<!--            PLUGINS                                         -->
<plugins>
<!-- delete nodjs build folders on maven clean -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>${basedir}</directory>
<includes>
<include>node/**</include>
<include>client-ui/dist/**</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<!--    mojo buildnumer maven plugin                -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<shortRevisionLength>8</shortRevisionLength>
<scmBranchPropertyName>true</scmBranchPropertyName>
<scmBranchPropertyName>scmBranch</scmBranchPropertyName>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
<!--    maven jar plugin                -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
<Build-Branch>${scmBranch}</Build-Branch>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- maven antrun plugin            -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>echo-properties</id>
<goals>
<goal>run</goal>
</goals>
<phase>validate</phase>
<configuration>
<target>
<echo message="buildnumber-maven-plugin properties:"/>
<echo message="  $${scmBranch}:                  ${scmBranch}" />
<echo message="  $${buildNumber}:                ${buildNumber}" />
<echo message="  $${timestamp}:                  ${timestamp}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!--            EXTENSIONS                                      -->
<extensions>
<!-- Amazon aws repository s3 pulgin -->
<extension>
<groupId>com.allogy.maven.wagon</groupId>
<artifactId>maven-s3-wagon</artifactId>
<version>1.1.0</version>
</extension>
</extensions>
</build>

问题是,当我在本地构建项目时,maven会按预期获得所有git信息,但当在gitlab上执行构建时,它无法检索scm分支名称,我会收到以下警告:

[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ securteck ---
[INFO] Deleting /builds/securteck/securteck (includes = [node/**, client-ui/dist/**], excludes = [])
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.4:create (default) @ securteck ---
[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/builds/securteck/securteck' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /builds/securteck/securteck
[INFO] Storing buildNumber: efd0a966 at timestamp: 1586711200134
[WARNING] Cannot get the branch information from the git repository: 
Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref
[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/builds/securteck/securteck' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /builds/securteck/securteck
[INFO] Storing buildScmBranch: UNKNOWN

我在本地和gitlabrunner上使用相同的maven版本。

这里有我的gitlab-ci.yml内容:

image: maven:latest
# Services goes here
services:
- docker:dind
# Cache goes here
cache:
paths:
- .m2/repository
- frontend-app/node_modules/
# ENV variables goes  here
# Before script goes here
# Stages definition goes here
stages:
- build
# Build stage
build:
stage: build
before_script:
- yum install -y git
- git --version
- mvn --version
script:
- echo "****** start scripts of build stage ******"
- mvn clean install --settings .ci/settings.xml --no-transfer-progress

知道吗?

Gitlab运行程序以分离模式签出repo。我不完全理解这意味着什么,但基本上你无法在本地环境中查询到相同的信息。

所以,对于同样的问题,我最终使用的是一些提供相同信息的环境变量。

现在的缺点是,在您的本地构建时,您没有可用的信息。所以我两者都用。我知道我的产品构建将有来自CI的产品,而另一个将被忽略。

  • 不起作用:${buildNumber}${scmBranch}
  • 请改用:${env.CI_COMMIT_SHA}${env.CI_COMMIT_REF_NAME}
  • 还可以使用与回购无关的信息,如${maven.build.timestamp}${project.version}

最新更新