我想获取 Jenkins 部署的神器版本

I want to get the artifact version deployed by Jenkins

我 运行 将 maven 部署作为一个步骤(使用 maven 构建步骤)并使用时间戳部署工件。

我现在想创建一个 docker 图像,其中包含已部署的工件,并且 docker 图像标记有工件时间戳。这是一个非常常见的场景,其中 docker 图像的标签必须与包含的工件相同。

我已经阅读了几篇文章

  1. Jenkins maven deploy jar to nexus - artifact naming
  2. Jenkins - How can i pass parameters from the Upstream to Downstream
  3. Sonatype Nexus REST Api fetch latest build version

其中 [3] 在 xml 中为我提供了来自服务器的快照版本列表,必须对其进行解析。

在基于 Maven 的 Jenkins Jobs 中,环境变量 POM_GROUPIDPOM_ARTIFACTIDPOM_VERSION 被导出。

通过${ENV,var="POM_VERSION"}(或类似的)

获取此变量

根据以上信息构建您想要的标签名称。

参见:https://blog.codecentric.de/en/2014/07/accessing-maven-project-properties-jenkins-build-jobs/

Jenkins exposes general maven project properties as environment variables. Of corse this only works in maven build jobs, but not in freestyle jobs that execute maven goals.

[...]

The following table shows a full list of how maven project properties are mapped to Jenkins environment variables:

maven project property - Jenkins environment variable

project.displayName - POM_DISPLAYNAME

project.version - POM_VERSION

project.groupId - POM_GROUPID

project.artifactId - POM_ARTIFACTID

project.packaging - POM_PACKAGING

project.relativePath - POM_RELATIVEPATH

所有文件的 -SNAPSHOT 部分(附加在 Maven 部署 'task' 上)将在 deploy:deploy 阶段被带时间戳的版本替换。

1) 创建Docker 图像文件

使用 docker-maven-plugin 扩展工件 POM(由 spotify 在 https://github.com/spotify/docker-maven-plugin 提供)。

[...]

You can also bind the build goal to the package phase, so the container will be built when you run just mvn package.

[...]

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>0.2.11</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>build</goal>
      </goals>
      <configuration>
        <imageName>${project.build.finalName}</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

Docker 图像名称将在 <imageName /> 处定义并使用工件文件名 (${project.build.finalName})。

imageName: Built image will be given this name.

有关 build 目标的更多信息:mvn com.spotify:docker-maven-plugin:help -Ddetail=true -Dgoal=buildhttps://github.com/spotify/docker-maven-plugin

2) 在 Maven 部署任务上附加 Docker 图像文件

附加 - 如果 docker-maven-plugin 不适合您 - 带有 build-helper-maven-plugin (http://www.mojohaus.org/build-helper-maven-plugin/usage.html).

的 Docker 图像文件
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>${project.build.finalName}</file>
              <type>...</type>
              <classifier>docker</classifier>
            </artifact>
            ...
          </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>

在这些步骤之后,工件文件本身和 Docker 图像工件被部署到具有相同版本字符串的 Maven 存储库。