首页 文章

我想获得Jenkins部署的工件版本

提问于
浏览
1

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

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

我已经阅读了几篇文章

其中[3]给出了xml中服务器的快照版本列表,必须对其进行解析 .

  • 由于我在jenkins作业中推送工件,是否有可能知道构建中的完整工件名称而不是从服务器获取它 .

  • 是否有API /任何其他方式,可以提供最新工件的名称而不是工件XML

2 回答

  • 0

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

    1) create Docker image file

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

    [...]您还可以将构建目标绑定到包阶段,因此只运行mvn包时将构建容器 . [...]

    <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: Build 的图像将被赋予此名称 .

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

    2) attach Docker image file on Maven deploy task

    附加 - 如果 docker-maven-plugin 不适合您 - 带有 build-helper-maven-pluginhttp://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存储库 .

  • 1

    在基于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将常规maven项目属性公开为环境变量 . Corse只适用于maven构建作业,但不适用于执行maven目标的自由式作业 . [...]下表显示了maven项目属性如何映射到Jenkins环境变量的完整列表:maven项目属性 - Jenkins环境变量project.displayName - POM_DISPLAYNAME project.version - POM_VERSION project.groupId - POM_GROUPID project.artifactId - POM_ARTIFACTID project.packaging - POM_PACKAGING project.relativePath - POM_RELATIVEPATH

相关问题