Kumar Mani
Kumar Mani

Reputation: 161

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.

Upvotes: 3

Views: 8683

Answers (2)

barthel
barthel

Reputation: 950

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.

Upvotes: 1

barthel
barthel

Reputation: 950

In a Maven based Jenkins Jobs the environment variables POM_GROUPID, POM_ARTIFACTID and POM_VERSION are exported.

Get this Variable via ${ENV,var="POM_VERSION"} (or similar)

Build your tag name like you want with the information above.

See: 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

Upvotes: 2

Related Questions