Search code examples
mavenjenkinsdockerartifact-deployer

I want to get the artifact version deployed by Jenkins


I run maven deploy as a step (using maven build step) and the artifact is deployed with a timestamp.

I now want to create a docker image which has the deployed artifact and the docker image is tagged with the artifact timestamp. It is a very common scenario where the tag of docker image has to be same as the artifact is contains.

I have already read a couple of posts

  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

Where [3] gives me the list of snapshot-versions from the server in an xml, which has to be parsed.

  • Since I'm pushing the artifact in the jenkins job, is it possible to know the full artifact name in the build instead of getting it from the server.

  • Is there an API/any other way, which can give the name of the latest artifact instead of artifact XML


Solution

  • The -SNAPSHOT part of all files (attached on an Maven deployment 'task') will be replaced by the timestamped version at deploy:deploy phase.

    1) create Docker image file

    Extend the artifact POM with docker-maven-plugin (provided by spotify at 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>
    

    The Docker image name will be defined at <imageName /> and use the artifact file name (${project.build.finalName}).

    imageName: Built image will be given this name.

    More information about the build goal: mvn com.spotify:docker-maven-plugin:help -Ddetail=true -Dgoal=build or https://github.com/spotify/docker-maven-plugin

    2) attach Docker image file on Maven deploy task

    Attach - if docker-maven-plugin doesn't do it for you - the Docker image file with the build-helper-maven-plugin (http://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>
    

    After these steps the artifact files itself and the Docker image artifact are deployed to Maven repository with identical version strings.