8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

Gradle 构建从管道运行和从本地运行时生成不同的 jar

Mohammad Fatoni 2月前

15 0

我正在使用 github 工作流为 java/spring 应用程序生成 jar 文件,然后从中构建 docker 镜像。问题是当我使用 docker 在本地运行该镜像时,它不会启动 grpc

我正在使用 github 工作流为 java/spring 应用程序生成 jar 文件,然后从中构建 docker 镜像。

问题是,当我使用 docker 在本地运行该图像时,它不会启动 grpc 服务器。

我已经在本地(即 macos)上完成了运行测试:

bash gradlew clean bootJar
docker build --build-arg JAR_FILE=build/libs/user-service-0.0.20.jar -t user-service-local:0.0.20 .

然后我开始了我的图像并且一切运行良好。

当我通过 github 工作流文件执行相同操作并尝试在本地运行推送的 docker 镜像时,它不会启动 grpc 服务器。

我已经验证了两个图像(在本地和 github 上的 vs 上生成)并且它们的 sha 是相同的。所以这让我认为问题出在 gradle .

以下这些信息应该可以帮助缩小范围。


Github 工作流程

build:
    if: github.event.pull_request.merged == true
    needs: determine-version
    runs-on: ubuntu-latest
    name: Deploying ${{ needs.determine-version.outputs.new_version }}
    env:
      ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }}
      ECR_REGISTRY: ${{ vars.ECR_REGISTRY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          java-version: '8'
          distribution: 'temurin'
          architecture: x64

      - name: Build with Gradle
        run: ./gradlew clean build --refresh-dependencies

      - name: Identify JAR file
        id: identify-jar
        run: |
          jar_file=$(basename $(ls build/libs/*.jar | grep -v '\-plain\.jar'))
          echo "Identified JAR file: $jar_file"
          echo "::set-output name=jar_file::$jar_file"

      - name: Debug JAR file
        run: echo "JAR file ${{ steps.identify-jar.outputs.jar_file }}"

      - name: Verify JAR file existence
        run: |
          if [[ ! -f build/libs/${{ steps.identify-jar.outputs.jar_file }} ]]; then
            echo "JAR file not found!"
            exit 1
          fi
          echo "JAR file found: build/libs/${{ steps.identify-jar.outputs.jar_file }}"

      - name: Update version in build.gradle
        run: |
          old_version=$(grep "^version = '" build.gradle | cut -d"'" -f2)
          echo "Old version: $old_version"
          sed -i "s/version = '.*'/version = '${{ needs.determine-version.outputs.new_version }}'/g" build.gradle
          new_version=$(grep "^version = '" build.gradle | cut -d"'" -f2)
          echo "Updated version in build.gradle: $new_version"

      - name: Commit and tag new version
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT }}
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git remote set-url origin https://x-access-token:${{ secrets.GH_PAT }}@github.com/${{ github.repository }}.git
          git add build.gradle
          git commit -m "Increment version to ${{ needs.determine-version.outputs.new_version }}"
          git tag -a "${{ needs.determine-version.outputs.new_version }}" -m "Release ${{ needs.determine-version.outputs.new_version }}"
          git push origin --tags
          git push origin main

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-west-2

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
        env:
          AWS_REGION: eu-west-2

      - name: Build Docker image
        run: |
          docker build --build-arg JAR_FILE=build/libs/${{ steps.identify-jar.outputs.jar_file }} -t ${{ vars.ECR_REPOSITORY }}:latest -t ${{ vars.ECR_REPOSITORY }}:${{ needs.determine-version.outputs.new_version }} .

      - name: Tag Docker image
        run: |
          docker tag ${{ vars.ECR_REPOSITORY }}:${{ needs.determine-version.outputs.new_version }} ${{ vars.ECR_REGISTRY }}/${{ vars.ECR_REPOSITORY }}:${{ needs.determine-version.outputs.new_version }}
          docker tag ${{ vars.ECR_REPOSITORY }}:latest ${{ vars.ECR_REGISTRY }}/${{ vars.ECR_REPOSITORY }}:latest

      - name: Push Docker image
        run: |
          docker push ${{ vars.ECR_REGISTRY }}/${{ vars.ECR_REPOSITORY }}:${{ needs.determine-version.outputs.new_version }}
          docker push ${{ vars.ECR_REGISTRY }}/${{ vars.ECR_REPOSITORY }}:latest

Gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

构建.gradle

plugins {
    id 'org.springframework.boot' version '2.6.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'com.google.protobuf' version '0.8.14'
}

group = 'com.uberkaretki.user'
version = '0.0.20'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    mavenLocal()

    repositories {
        maven {
            name 'GitHub'
            url "https://maven.pkg.github.com/uberkaretki/common"

            credentials {
                username = 'xxx'
                password = 'yyy' // Just packages:read
            }
        }
    }

}

dependencies {
    implementation "com.uberkaretki:karetki-common:0.1.2"

    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok'

}

protobuf {
    generatedFilesBaseDir = "$projectDir/src/main/java/generated"
    protoc {
        artifact = 'com.google.protobuf:protoc:3.10.1'
    }

    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0'
        }
    }

    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

我已经在本地(即 macos)上完成了运行测试:

bash gradlew clean bootJar
docker build --build-arg JAR_FILE=build/libs/user-service-0.0.20.jar -t user-service-local:0.0.20 .

然后我开始了我的图像并且一切运行良好。

帖子版权声明 1、本帖标题:Gradle 构建从管道运行和从本地运行时生成不同的 jar
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Mohammad Fatoni在本站《java》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: