如何在gven中将git commit id用作docker image标记?

时间:2016-01-07 14:41:27

标签: git maven docker

我正在尝试为安装和部署阶段中构建和推送的maven Java项目提供docker镜像;我希望用当前的git commit id标记这些图像。

我遇到的问题是,maven-git-commit-plugin似乎无法正确导出${git.commit.id.abbrev}变量以供docker-maven-plugin消费。

我的父母pom.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>    
    <name>myproject</name>

    <groupId>mygroup</groupId>
    <artifactId>myartifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>mymodule</module>
    </modules>

    <properties>

        <!-- dependency versions -->    
           <!-- some stuff here...-->
        <!-- plugin versions -->
        <maven.enforcer.plugin.version>1.4.1</maven.enforcer.plugin.version>
        <maven.shade.plugin.version>2.4.2</maven.shade.plugin.version>
        <scala.maven.plugin.version>3.2.2</scala.maven.plugin.version>
        <com.spotify.docker.plugin.version>0.3.8</com.spotify.docker.plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
           <!-- some stuff here...-->
        </dependencies>
    </dependencyManagement>

    <build>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${com.spotify.docker.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>docker-build</id>
                            <goals>
                                <goal>build</goal>
                            </goals>
                            <phase>install</phase>
                        </execution>
                        <execution>
                            <id>docker-push</id>
                            <goals>
                                <goal>push</goal>
                            </goals>
                            <phase>deploy</phase>
                        </execution>
                    </executions>
                    <configuration>
                        <forceTags>true</forceTags>
                        <baseImage>java</baseImage>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>pl.project13.maven</groupId>
                    <artifactId>git-commit-id-plugin</artifactId>
                    <version>2.2.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>revision</goal>
                            </goals>
                        </execution>
                    </executions>

                    <configuration>
                        <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
                        <verbose>true</verbose>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>

        <plugins>
        </plugins>

    </build>
</project>

我的插件是我的模块pom.xml的一部分:

        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <configuration>
                <imageName>docker-registry.nexus.bazaarvoice.com/${project.parent.artifactId}-${project.artifactId}:${git.commit.id.abbrev}</imageName>
                <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>
                    <resource>
                        <targetPath>config</targetPath>
                        <directory>${project.parent.basedir}/deploy/config/${project.artifactId}</directory>
                        <include>*</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>

我收到以下错误:

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.3.8:build (docker-build) on project service: Exception caught: The template variable 'git.commit.id.abbrev' has no value -> [Help 1]

我尝试切换到buildnumber-plugin,但我遇到了同样的错误(使用${buildNumber}代替${git.commit.id.abbrev})。

我缺少什么?在git commit id插件设置变量之前,docker插件是否正在执行?

2 个答案:

答案 0 :(得分:1)

我有一个类似的问题,因为我直接调用插件(我没有绑定我的pom中的插件阶段,所以我必须明确地调用它):

mvn -f myModule/pom.xml docker:build -DforceTags -DpushImage

使用插件conf:

  <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
        <imageName>registry/myModule</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
            <imageTag>latest</imageTag>
            <imageTag>${git.commit.id.abbrev}</imageTag>
        </imageTags>
        <baseImage>java</baseImage>
        <entryPoint>
            ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/${project.build.finalName}.jar"]
        </entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

由于maven-git-commit-plugin绑定到初始化阶段,我必须调用该阶段,以便git插件填充属性

mvn initialize -f myModule/pom.xml docker:build -DforceTags -DpushImage

答案 1 :(得分:0)

尝试使用 mvn package docker:build 代替 mvn docker:build