3

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

Community
  • 1
  • 1
Kumar Mani
  • 151
  • 2
  • 11
  • It's possible to combine the deployment of the artifact and the Docker image in ONE pom? Are the artifact and the Docker image deployed at the same maven task both -SNAPSHOT aliases are replaced by the same timestamped version string. If it is a working solution for you I'll create a new answer reflecting that. – barthel Jun 30 '15 at 11:29
  • @barthel I did some research about publishing both artifact and docker image in one pom and found it very interesting. But calling them (2 poms) sequentially changes the timestamp. Would you be able to help me understand more on how to add docker build+deploy with the artifact deploy. I found [this](https://github.com/spotify/docker-maven-plugin) a good reference, in case you need, but I'm sure you would be aware of it already :) – Kumar Mani Jun 30 '15 at 14:01

2 Answers2

2

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

Community
  • 1
  • 1
barthel
  • 865
  • 9
  • 22
  • Lets assume the **name** of my artifact is **com.company.mani**. The **version** is **1.0.1-SNAPSHOT**. The artifacts are posted as **com.company.mani-1.0.1-20150629.232753-78**.pom. The value mentioned in the POM_VERSION is 1.0.1-SNAPSHOT. But I want to get the uploaded artifact with timestamp i.e. **com.company.mani-1.0.1-20150629.232753-78** How would I get that? – Kumar Mani Jun 30 '15 at 10:00
  • My solution doesn't work - after clarification - for you. The environment variables are read and exports only once at job start. Get the timestamed version information is more complicated. This string will be created at the `deploy:deploy` goal. – barthel Jun 30 '15 at 11:26
1

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.

Community
  • 1
  • 1
barthel
  • 865
  • 9
  • 22
  • Thanks @barthel ! It looks like it will solve my problem. I'll give it a try and come back with issues if I face any :) – Kumar Mani Jul 01 '15 at 02:30