2015-06-29 25 views
1

我将maven deploy作为步骤运行(使用maven构建步骤),并使用时间戳部署工件。我想获取由Jenkins部署的工件版本

我现在想创建一个具有已部署工件的泊坞窗图像,并且使用工件时间戳标记了泊坞窗图像。这是一种非常常见的情况,其中docker映像的标签必须与artifact包含的相同。

我已经看过一些帖子

  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,它必须被解析。

  • 因为我推在詹金斯工作的神器,是有可能知道在构建全 工件名称,而不是从服务器得到它的。

  • 是否有一个API /任何其他方式,它可以给最新 工件的名称,而不是神器XML

+0

在ONE POM中可以将工件的部署和Docker镜像结合起来吗?工件和Docker镜像部署在相同的Maven任务中 - 两个-SNAPSHOT别名被替换为相同的时间戳版本字符串。如果它是您的工作解决方案,我会创建一个反映该问题的新答案。 – barthel

+0

@barthel我做了一些关于在一个pom中发布神器和码头图像的研究,并且发现它很有趣。但他们(2 poms)顺序改变时间戳。您是否能够帮助我更好地了解如何在工件部署中添加docker build + deploy。我发现[这](https://github.com/spotify/docker-maven-plugin)是一个很好的参考,以防您需要,但我相信您会知道它已经:) –

回答

0

的所有文件(连在一个Maven的部署“任务”)的-SNAPSHOT部分将由时间戳版本在deploy:deploy阶段所取代。

1)创建泊坞图像文件

扩展与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> 

的泊坞图像名称将在<imageName />被定义和使用的工件的文件名(${project.build.finalName})。

imageName:建立的图像将被赋予这个名字。有关build目标

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

2)附加泊坞映像文件Maven的部署任务

连接 - 如果docker-maven-plugin不为你做 - 泊坞窗图像文件与build-helper-maven-pluginhttp://www.mojohaus.org/build-helper-maven-plugin/usage.html)。

<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仓库中。

+0

Thanks @barthel!它看起来会解决我的问题。我会试试看,如果我面对任何问题,回来的问题:) –

1

在基于Maven的詹金斯作业环境变量POM_GROUPIDPOM_ARTIFACTIDPOM_VERSION是出口。

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

得到这个变量像你想与上面的信息构建的标签名。

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

詹金斯公开一般Maven项目属性,环境变量。这些只适用于maven构建作业,但不适用于执行maven目标的自由式作业。

[...]

下表显示的项目属性如何行家映射到詹金斯环境变量的完整列表:

Maven项目属性 - 詹金斯环境变量

project.displayName - POM_DISPLAYNAME

project.version - POM_VERSION

project.groupId - POM_GRO UPID

project.artifactId - POM_ARTIFACTID

project.packaging - POM_PACKAGING

项目。relativePath - POM_RELATIVEPATH

+0

让我们假设* *我神器的名字**是** com.company.mani **。 **版**是** 1.0.1-SNAPSHOT **。这些工件发布为** com.company.mani-1.0.1-20150629.232753-78 **。pom。 POM_VERSION中提到的值是1.0.1-SNAPSHOT。但我想获取带有时间戳的上传的工件,即** com.company.mani-1.0.1-20150629.232753-78 **我将如何获取? –

+0

我的解决方案在澄清之后不适用于您。环境变量在作业开始时只读取和导出一次。获取时间戳版本信息更加复杂。该字符串将在'deploy:deploy'目标中创建。 – barthel