在dockerfile中自动增加项目版本

问题描述 投票:0回答:1

我有一个Java应用程序部署在docker容器内,但是每次我发布新版本时,我都必须手动更新docker文件中应用程序的版本,有什么方法可以通过直接获取版本来自动执行此操作来自 pom?

这是我的dockerfile

FROM openjdk:17
COPY target/projectName-x.x.x.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

显然正确的版本号不是 x.x.x

这是 pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.5</version>
    <relativePath/> 
</parent>
<groupId>com.groupName</groupId>
<artifactId>projectArtifactId</artifactId>
<version>x.x.x-SNAPSHOT</version>
<name>projectName</name>
<properties>
    <java.version>17</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>3.1.5</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <image>
                    <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                </image>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>it.ivannotarstefano.ultimateserver.UltimateServerApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
java docker
1个回答
0
投票

有几种方法你可以尝试,

  1. 如果您使用 gitlab ci/cd 管道,则可以在 .gitlab-ci.yaml 文件中编写以下步骤,

.gitlab-ci.yaml

stages:
   - stage_1
   - stage_2
   - versioning
   - build
 variable:
    APP_VERSION: "1.0"
 versioning:
    stage: "versioning"
    script: 
      - if [! -f version.txt]; then echo "$APP_VERSION" > version.txt fi
      - APP_VERSION=$(echo "$APP_VERSION + 1.0" | bc)
      - echo "$APP_VERSION" > version.txt
  build:
    stage: "build"
    script: 
       - docker build --build-arg APP_VERSION="$APP_VERSION" -t projectName:"$APP_VERSION" .

但是您只需要在上面的 dockerfile 中进行两处更改,

FROM openjdk:17
ARG APP_VERSION
COPY target/projectName-${APP_VERSION}.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
  1. 如果您在本地执行此操作,您可以创建自己的 build.sh 文件并执行以下步骤,

    # check if version.txt file exist, otherwise create one and store initial version
       if [ ! -f version.txt]; then echo "1.0" > version.txt fi
    # declare a variable to store current version
       CURRENT_VERSION=$(<version.txt)
    # create a new version
       UPDATED_VERSION=$(echo "$CURRENT_VERSION + 1.0" | bc)
    # write new version back to file
       echo "$UPDATED_VERSION" > version.txt
    # then build docker image with new version
       docker build --build-arg APP_VERSION="$UPDATED_VERSION" -t projectName:"$UPDATED_VERSION" .
    

希望这能解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.