Unverified Commit 0b56831a authored by Bruno Borges's avatar Bruno Borges Committed by GitHub
Browse files

Add dependency cache path overrides (#1175)

* Add dependency cache path overrides

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c

* Clarify supported dependency cache managers

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c

* Fix custom cache path CI checks

Align the custom cache save and restore key inputs and use the workflow hash to avoid a previously populated cache entry. Rebuild the distribution bundles.

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c

---------

Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c
parent 9f431413
Loading
Loading
Loading
Loading
+50 −1
Changes for .github/workflows/e2e-cache.yml: 50 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -335,7 +335,9 @@ jobs:
          distribution: 'adopt'
          java-version: '11'
          cache: maven
          cache-dependency-path: __tests__/cache/maven2/pom.xml
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            README.md
      - name: Confirm that ~/.m2/repository directory has not been made
        run: bash __tests__/check-dir.sh "$HOME/.m2/repository" absent
  sbt1-save:
@@ -446,3 +448,50 @@ jobs:
      - name: Confirm that ~/.cache/coursier directory has not been made
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier" absent
  custom-maven-path-save:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with a custom Maven cache path
        uses: ./
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: maven
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            .github/workflows/e2e-cache.yml
          cache-path: |
            ${{ runner.temp }}/setup-java-custom-maven-repository
            !${{ runner.temp }}/setup-java-custom-maven-repository/**/*.lastUpdated
      - name: Populate the custom Maven repository
        run: |
          mvn -Dmaven.repo.local="$RUNNER_TEMP/setup-java-custom-maven-repository" verify -f __tests__/cache/maven2/pom.xml
          touch "$RUNNER_TEMP/setup-java-custom-maven-repository/setup-java-cache-path-marker"
          bash __tests__/check-dir.sh "$RUNNER_TEMP/setup-java-custom-maven-repository"
  custom-maven-path-restore:
    runs-on: ubuntu-latest
    needs: custom-maven-path-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with a custom Maven cache path
        uses: ./
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: maven
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            .github/workflows/e2e-cache.yml
          cache-path: |
            ${{ runner.temp }}/setup-java-custom-maven-repository
            !${{ runner.temp }}/setup-java-custom-maven-repository/**/*.lastUpdated
          cache-read-only: true
      - name: Confirm that the custom Maven repository has been restored
        run: test -f "$RUNNER_TEMP/setup-java-custom-maven-repository/setup-java-cache-path-marker"
+30
Changes for README.md: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -72,6 +72,8 @@ For more details, see the full release notes on the [releases page](https://git

  - `cache-dependency-path`: The path to a dependency file: pom.xml, build.gradle, build.sbt, etc. This option can be used with the `cache` option. If this option is omitted, the action searches for the dependency file in the entire repository. This option supports wildcards and a list of file names for caching multiple dependencies.

  - `cache-path`: The dependency cache path to use instead of the default path for the package manager selected by `cache`. This option supports a list of paths and exclusion patterns. The build tool must be configured to use the same location.

  - `cache-read-only`: Restore dependency caches without saving changes in the post action. Defaults to `false`. Use this for pull requests, merge queues, short-lived branches, and fan-out jobs that should consume caches populated by a default-branch or seed job.

  #### Maven options
@@ -188,6 +190,34 @@ The action has a built-in functionality for caching and restoring dependencies.

When the option `cache-dependency-path` is specified, the hash is based on the matching file. This option supports wildcards and a list of file names, and is especially useful for monorepos.

Use `cache-path` to replace the selected package manager's default dependency
cache paths. Each non-empty line is passed to `actions/cache`, including
supported exclusion patterns. `setup-java` does not configure the build tool,
so the build must use the same paths:

```yaml
- uses: actions/setup-java@v6
  with:
    distribution: 'temurin'
    java-version: '25'
    cache: 'maven'
    cache-path: |
      /custom/maven/repository
      !/custom/maven/repository/**/*.lastUpdated
- run: mvn -Dmaven.repo.local=/custom/maven/repository verify
```

`cache-path` does not change the cache key. The key continues to use the runner
OS, architecture, selected package manager, and dependency-file hash described
above. Jobs intended to share a cache key must therefore use the same
`cache-path` values so that they restore and save the same filesystem
locations.

The Maven and Gradle wrapper caches remain at their documented default paths
and are managed independently of `cache-path`. For advanced keying, fallback
keys, or cache topologies that do not map to one package manager's dependency
paths, use [`actions/cache`](https://github.com/actions/cache) directly.

The workflow output `cache-hit` is set to indicate if an exact match was found for the key [as actions/cache does](https://github.com/actions/cache/tree/main#outputs).

The workflow output `cache-primary-key` exposes the primary cache key computed by the action for the configured build tool. It is useful for composing with [`actions/cache`](https://github.com/actions/cache) or [`actions/cache/restore`](https://github.com/actions/cache/tree/main/restore) in later steps or dependent jobs that need to reuse the exact same key. It is empty when caching is not enabled or when caching is skipped (for example, when the cache service is unavailable).
+68 −4
Changes for __tests__/cache.test.ts: 68 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -224,10 +224,10 @@ describe('dependency cache', () => {
          join(workspace, '.mvn', 'wrapper', 'maven-wrapper.properties')
        );

        await restore('maven', '');
        await restore('maven', '', ['/custom/maven/repository']);
        // Main dependency cache no longer carries the wrapper dists path.
        expect(spyCacheRestore).toHaveBeenCalledWith(
          [join(os.homedir(), '.m2', 'repository')],
          ['/custom/maven/repository'],
          expect.any(String)
        );
        expect(spyCacheRestore).toHaveBeenCalledWith(
@@ -394,10 +394,10 @@ describe('dependency cache', () => {
      it('restores the gradle wrapper distribution cache independently of the main cache', async () => {
        createFile(join(workspace, 'build.gradle'));

        await restore('gradle', '');
        await restore('gradle', '', ['/custom/gradle/caches']);
        // Main dependency cache no longer carries the wrapper path.
        expect(spyCacheRestore).toHaveBeenCalledWith(
          [join(os.homedir(), '.gradle', 'caches')],
          ['/custom/gradle/caches'],
          expect.any(String)
        );
        // Wrapper distribution is restored on its own, keyed only on the
@@ -576,6 +576,34 @@ describe('dependency cache', () => {
        expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found');
      });
    });
    describe('cache-path', () => {
      it.each([
        ['maven', ['/custom/maven/repository']],
        ['gradle', ['/custom/gradle/caches']],
        [
          'sbt',
          [
            '/custom/ivy/cache',
            '/custom/coursier/cache',
            '!/custom/ivy/cache/*.lock'
          ]
        ]
      ])(
        'restores and persists custom paths for %s',
        async (packageManager, cachePaths) => {
          await restore(packageManager, '', cachePaths);

          expect(spyCacheRestore).toHaveBeenCalledWith(
            cachePaths,
            expect.any(String)
          );
          expect(spySaveState).toHaveBeenCalledWith(
            'cache-paths',
            JSON.stringify(cachePaths)
          );
        }
      );
    });
  });
  describe('save', () => {
    let spyCacheSave: any;
@@ -625,6 +653,42 @@ describe('dependency cache', () => {
      );
    });

    it.each([
      ['maven', ['/custom/maven/repository']],
      ['gradle', ['/custom/gradle/caches']],
      [
        'sbt',
        [
          '/custom/ivy/cache',
          '/custom/coursier/cache',
          '!/custom/ivy/cache/*.lock'
        ]
      ]
    ])(
      'saves the persisted custom paths for %s',
      async (packageManager, cachePaths) => {
        (core.getState as jest.Mock<any>).mockImplementation((name: any) => {
          switch (name) {
            case 'cache-primary-key':
              return 'setup-java-cache-primary-key';
            case 'cache-matched-key':
              return 'setup-java-cache-matched-key';
            case 'cache-paths':
              return JSON.stringify(cachePaths);
            default:
              return '';
          }
        });

        await save(packageManager);

        expect(spyCacheSave).toHaveBeenCalledWith(
          cachePaths,
          'setup-java-cache-primary-key'
        );
      }
    );

    describe('for maven', () => {
      it('uploads cache even if no pom.xml found', async () => {
        createStateForMissingBuildFile();
+8 −1
Changes for __tests__/setup-java.test.ts: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -301,6 +301,10 @@ describe('setup action orchestration', () => {
    inputs.set('cache', 'maven');
    inputs.set('cache-dependency-path', '**/pom.xml');
    multilineInputs.set('java-version', ['21']);
    multilineInputs.set('cache-path', [
      '/custom/maven/repository',
      '!/custom/maven/repository/excluded'
    ]);
    const setupJava = jest.fn(async () => ({
      version: '21.0.4+7',
      path: '/opt/java/21'
@@ -312,7 +316,10 @@ describe('setup action orchestration', () => {
    expect(problemMatcher.configureProblemMatcher).toHaveBeenCalledWith(
      expect.stringMatching(/\.github[/\\]java\.json$/)
    );
    expect(cache.restore).toHaveBeenCalledWith('maven', '**/pom.xml');
    expect(cache.restore).toHaveBeenCalledWith('maven', '**/pom.xml', [
      '/custom/maven/repository',
      '!/custom/maven/repository/excluded'
    ]);
    expect(
      (toolchains.configureToolchains as jest.Mock).mock.invocationCallOrder[0]
    ).toBeLessThan(
+3
Changes for action.yml: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -87,6 +87,9 @@ inputs:
  cache-dependency-path:
    description: 'The path to a dependency file: pom.xml, build.gradle, build.sbt, etc. This option can be used with the `cache` option. If this option is omitted, the action searches for the dependency file in the entire repository. This option supports wildcards and a list of file names for caching multiple dependencies.'
    required: false
  cache-path:
    description: 'The path to cache instead of the default dependency cache path for the selected package manager. This option can be used with the `cache` option and supports a list of paths and exclusion patterns.'
    required: false
  cache-read-only:
    description: 'Restore dependency caches without saving cache changes in the post action.'
    required: false
Loading