如何在 GitHub Actions 中优化 Spring Boot Native 镜像编译?

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

我正在尝试使用 GitHub Actions 将一个小型 Spring Boot 应用程序编译为本机映像,但该过程花费了非常长的时间(超过 20 分钟),这使得它几乎无法使用。应用程序本身很小,所以我不明白为什么编译时间这么长。

以下是我当前 GitHub Actions 工作流程的概述:

name: Native Image Compile and Deploy

on:
  workflow_dispatch:
    inputs:
      app:
        description: 'Choose the application to build and deploy'
        required: true
        default: 'app-one'
        options:
          - app-one
          - app-two

jobs:
  native-image-compile:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # Cache Gradle dependencies to speed up build times
      - name: Cache Gradle Dependencies
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            build/
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties', '**/native-image.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle

      # Setup GraalVM using the GitHub Action for GraalVM
      - uses: graalvm/setup-graalvm@v1
        with:
          java-version: '21'
          distribution: 'graalvm'
          components: 'native-image'

      # Compile the application to a native image
      - name: Compile Application to Native Image
        run: |
          APP_NAME=${{ github.event.inputs.app }}
          
          # Use Gradle Daemon and configure Gradle to be faster
          ./gradlew --no-daemon :applications:$APP_NAME:nativeCompile --parallel --max-workers=2

      # Build and tag the Docker image
      - name: Build and Tag Docker Image
        run: |
          APP_NAME=${{ github.event.inputs.app }}
          BRANCH_NAME=${{ github.ref_name }}
          
          docker build -t $APP_NAME:$BRANCH_NAME .
          docker save $APP_NAME:$BRANCH_NAME -o $APP_NAME-$BRANCH_NAME.tar

      # Tag the GitHub Branch with a Timestamp
      - name: Tag the GitHub Branch
        env:
          APP_NAME: ${{ github.event.inputs.app }}
          BRANCH_NAME: ${{ github.ref_name }}
        run: |
          TIMESTAMP=$(date +"%Y%m%d%H%M%S")
          TAG_NAME="$BRANCH_NAME-$APP_NAME-$TIMESTAMP"
          git tag $TAG_NAME
          git push origin $TAG_NAME

我已经尝试过缓存 Gradle 依赖项并启用并行编译,但并没有产生显着的差异。

我的问题是:

  1. 我可以应用任何特定策略或配置来减少本机映像编译时间吗?
  2. 是否有其他方法可以优化 GitHub Actions 工作流程以将 Spring Boot 应用程序编译为本机映像?
  3. GraalVM 或 GitHub Actions 是否存在可能导致编译时间过长的已知限制或问题?

任何建议或见解将不胜感激!

spring spring-boot github github-actions native
1个回答
0
投票

尝试对您的工作流程进行此更改,以改进 gradle 缓存。查看有关设置 gradle 的更多信息此处

jobs:
  native-image-compile:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: graalvm/setup-graalvm@v1
        with:
          java-version: '21'
          distribution: 'graalvm'
          components: 'native-image'

      - name: Setup Gradle and Gradle caching
        uses: gradle/actions/setup-gradle@v4

      - name: Compile Application to Native Image
        run: |
          APP_NAME=${{ inputs.app }}
          ./gradlew --no-daemon :applications:$APP_NAME:nativeCompile --parallel --max-workers=2
© www.soinside.com 2019 - 2024. All rights reserved.