Multiple tags for a Docker image built by Google Jib and Maven?
Asked Answered
T

2

9

Using the jib-maven-plugin I'd like to build an image with mulitple tags. While mvn jib:build basically works fine, it "only" tags the image with latest.

My Maven pom.xml defines the following:

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.8.0</version>
    <configuration>
        <to>
            <image>my-private-registry.org/my-image</image>
        </to>
    </configuration>
</plugin>

The docs show that one can use jib.to.tags in the XML to define a list of tags. However, I'd like to run the build step in a Jenkins pipeline, i.e. I cannot hard-code the tags in the pom.xml.

Is it possible to pass the list of tags when running the mvn command, e.g. by setting some -D parameters?

Technical answered 9/4, 2021 at 8:10 Comment(0)
S
21

Use -Djib.to.tags=a,b,c.

$ mvn package jib:build -Djib.to.tags=a,b,c
...
[INFO] --- jib-maven-plugin:2.8.0:dockerBuild (default-cli) @ hello-spring-boot ---
[INFO] Tagging image with generated image reference hello-spring-boot:0.1.0. If you'd like to specify a different tag, you can set the <to><image> parameter in your pom.xml, or use the -Dimage=<MY IMAGE> commandline flag.
[INFO] 
[INFO] Containerizing application to Docker daemon as hello-spring-boot:0.1.0, hello-spring-boot:a, hello-spring-boot:b, hello-spring-boot:c...
[WARNING] Base image 'gcr.io/distroless/java:8' does not use a specific image digest - build may not be reproducible
[INFO] Using base image with digest: sha256:34c3598d83f0dba27820323044ebe79e63ad4f137b405676da75a3905a408adf
[INFO] 
[INFO] Container entrypoint set to [java, -Djava.security.egd=file:/dev/./urandom, -cp, /app/resources:/app/classes:/app/libs/*, hello.Application]
[INFO] 
[INFO] Built image to Docker daemon as hello-spring-boot:0.1.0, hello-spring-boot:a, hello-spring-boot:b, hello-spring-boot:c
[INFO] 
[INFO] A new version of Jib (3.0.0) is available (currently using 2.8.0). Update your build configuration to use the latest features and fixes!
[INFO] https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/CHANGELOG.md
[INFO] Please see https://github.com/GoogleContainerTools/jib/blob/master/docs/privacy.md for info on disabling this update check.
[INFO] 
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Schweiker answered 9/4, 2021 at 14:46 Comment(3)
I have this command: ./mvnw package -Pprod verify -DskipTests jib:build -Dimage=registry.gitlab.com/me/myproject:master when using -Djib.to.tags=master,v1.14 would it push directly both tags?Moselle
@Moselle as you are using the -Dimage param and this is anyway not optional, you don´t have to put the master tag in -Djib.to.tags. With the -Djib.to.tags param you can add additional tags to push to.Potshot
I was able to get it using pom configuration, so the command doesn't change. Thanks for the hint though :)Moselle
M
3

I was having the same question and I solved using this snippet in my pom.xml file:

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>${jib-maven-plugin.version}</version>
    <configuration>
        <from>
            <image>${jib-maven-plugin.image}</image>
        </from>
        <to>
            <image>registry.gitlab.com/mygroup/myproject</image>
            <tags>
                <tag>latest</tag>
                <tag>${project.version}</tag>
            </tags>
        </to>
    </configuration>
</plugin>

simply calling mvn clean package -Pprod verify jib:build will push both latest and the project's version defined in pom

Moselle answered 8/8, 2023 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.