Maven+GitHub Action构建Spring Boot Docker镜像失败求助
Spring Boot项目容器化CI流水线故障排查
我正尝试用Maven将Spring Boot项目容器化,已经通过GitHub Action搭建了CI流水线,但运行时出错,找不到问题所在。以下是我的配置文件:
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 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>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ads.cre</groupId> <artifactId>cre-audit-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cre-audit-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20201115</version> </dependency> <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple --> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- Swagger API--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>1.18.22</version> </dependency> <!--Unit and Integration test --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.5.6-Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>com.github.librepdf</groupId> <artifactId>openpdf</artifactId> <version>1.3.8</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> <dependency> <groupId>xuggle</groupId> <artifactId>xuggle-xuggler</artifactId> <version>5.4</version> </dependency> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.6</version> </dependency> </dependencies> <repositories> <repository> <id>xuggle repo</id> <url>https://files.liferay.com/mirrors/xuggle.googlecode.com/svn/trunk/repo/share/java/</url> </repository> </repositories> <profiles> <profile> <id>docker</id> <build> <plugins> <plugin> <groupId>org.jolokia</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <images> <image> <name>ads/${project.artifactId}</name> <run> <ports> <port>8600:8080</port> </ports> </run> <build> <from>openjdk:8u92-jre-alpine</from> <ports> <port>8080</port> </ports> <assembly> <basedir>/</basedir> <inline> <files> <file> <source>${project.artifactId}-${project.version}.jar</source> <outputDirectory>/opt</outputDirectory> </file> </files> </inline> </assembly> <entryPoint> <shell>java -jar -Dstage=prod -Djava.net.preferIPv4Stack=true /opt/${project.artifactId}-${project.version}.jar </shell> </entryPoint> </build> </image> </images> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <execution> <id>pre-integration-test</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>post-integration-test</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
GitHub Action配置
name: Docker Image Build on: workflow_dispatch: push: branches: ['CRE[0-9]+-[0-9]+-[0-9]+-[0-9]+'] jobs: build: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v3 - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Set up JDK 8 uses: actions/setup-java@v3 with: java-version: '8' distribution: 'temurin' server-id: github Value of the distributionManagement/repository/id field of the pom.xml settings-path: ${{ github.workspace }} location for the settings.xml file - name: Set up Maven uses: stCarolas/setup-maven@v4 with: maven-version: '3.5.3' - name: Build with Maven shell: bash run: | mvn -v mvn -DskipTests -Dstage=prod -B package -X -Pdocker --file pom.xml -s $GITHUB_WORKSPACE/settings.xml env: GITHUB_TOKEN: ${{secrets.GITHUB_PACKAGES}} - name: Publish to GitHub Packages Apache Maven shell: bash run: | docker image tag ads/cre-audit-service:latest ghcr.io/cre-audit/cre-audit-service:qa-latest docker image tag ads/cre-audit-service:latest ghcr.io/cre-audit/cre-audit-service:qa-git-commit-${GITHUB_SHA::8} docker image tag ads/cre-audit-service:latest ghcr.io/cre-audit/cre-audit-service:qa-${GITHUB_REF*/} docker push -a ghcr.io/cre-audit/cre-audit-service
故障排查方向
1. GitHub Action语法错误
on块缩进错误:workflow_dispatch:和push:需要与on同层级缩进,否则会触发语法解析失败- 步骤缩进不一致:
Set up JDK 8和Set up Maven步骤的-符号比其他步骤多了一个空格,导致Action无法识别 - 参数注释格式错误:JDK配置中的注释不能直接写在参数值后,需单独一行并添加
#前缀
2. Maven配置问题
- 缺失settings.xml:Maven命令指定了
-s $GITHUB_WORKSPACE/settings.xml,但流水线中没有生成或添加该文件的步骤,会导致Maven报错 - 插件版本未指定:
docker-maven-plugin未声明版本,可能拉取到与当前Maven/Spring Boot不兼容的版本,建议明确指定稳定版本 - 依赖冲突:手动指定的
spring-test:2.5与Spring Boot Parent管理的spring-test:5.2.9.RELEASE版本冲突,移除手动指定的版本号即可
3. Docker权限与镜像问题
- 缺少GHCR登录步骤:推送镜像到GitHub容器仓库前,需要先执行登录命令,否则会因权限不足失败:
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin - 镜像构建验证:查看Maven日志确认
docker-maven-plugin是否成功构建出ads/cre-audit-service:latest镜像,若构建失败需检查插件配置
4.依赖仓库可用性
xuggle-xuggler依赖对应的仓库地址可能已失效,导致Maven无法拉取该依赖,建议替换为可用的镜像源或移除该依赖
内容的提问来源于stack exchange,提问作者Anbazhagan Natarajan
相关产品推荐
相关产品推荐

