如何使用GitHub操作缓存Java安装

8zzbczxx  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(187)

我创建了一个GitHub操作脚本,在第二次运行时使用该高速缓存步骤。但是,缓存的版本是17,但使用java -version检查版本时显示版本1.8

openjdk version "1.8.0_352"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (Temurin)(build 25.352-b08, mixed mode)

我如何缓存这个特定版本java?2提前感谢。
我完整的剧本。

name: dotnet-pipeline
on: [push]

jobs:
  build:
    name: Build-run-analysis
    runs-on: windows-2022
  
    env:
      JAVA_HOME: 'C:\hostedtoolcache\windows\Java_Microsoft_jdk\17.0.3\x64'
      JAVA_HOME_17_X64: 'C:\hostedtoolcache\windows\Java_Microsoft_jdk\17.0.3\x64'
     
    steps:  
    
      - name: Cache JDK
        id: cache-jdk
        uses: actions/cache@v3
        with:
          path: ${{ env.JAVA_HOME }}
          key: ${{ runner.os }}-jdk-17
          restore-keys: |
            ${{ runner.os }}-jdk-
  
      # Install Java development kit using microsoft distro
      - name: Install Java
        if: steps.cache-jdk.outputs.cache-hit != 'true'
        uses: actions/setup-java@v3
        with:
          distribution: 'microsoft'
          java-version: '17'
          check-latest: false # enforce to use files from cache
        
      - name: Set PATH
        run: |
          echo "${{ env.JAVA_HOME }}" | Out-File -FilePath $ENV:GITHUB_PATH -Encoding utf8 -Append      

      - name: check version
        run: java -version
kpbwa7wx

kpbwa7wx1#

这与您之前关于dotnet缓存的问题非常相似:

唯一要做的就是缓存正确的安装路径,并该高速缓存命中时将其设置为GITHUB_PATH
JAVA_HOME_17_X64将为:

C:\hostedtoolcache\windows\Java_Microsoft_jdk\17.0.3\x64

并且,其bin子目录将添加到GITHUB_PATH

${{ env.JAVA_HOME_17_X64 }}\bin

以下是完整的工作流程:

name: cache_java

on:
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-2022

    env:
      JAVA_HOME_17_X64: 'C:\hostedtoolcache\windows\Java_Microsoft_jdk\17.0.3\x64'

    steps:
      - name: Cache JDK
        id: cache-jdk
        uses: actions/cache@v3
        with:
          path: ${{ env.JAVA_HOME_17_X64 }}
          key: ${{ runner.os }}-jdk-17
          restore-keys: ${{ runner.os }}-jdk-

      - name: Install Java
        if: ${{ steps.cache-jdk.outputs.cache-hit != 'true' }}
        uses: actions/setup-java@v3
        with:
          distribution: 'microsoft'
          java-version: '17'

      - name: Check default Java version
        run: java -version

      - name: Set installed/cached Java path [${{ env.JAVA_HOME_17_X64 }}]
        run: echo "${{ env.JAVA_HOME_17_X64 }}\bin" | Out-File -FilePath $ENV:GITHUB_PATH -Encoding utf8 -Append

      - name: Check installed/cached Java version
        run: java -version

输出:

相关问题