Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache cleanup fails with "Unable to locate executable file: gradle" when Gradle is not pre-installed on runner #33

Closed
ashirman opened this issue Sep 7, 2022 · 10 comments · Fixed by #272
Labels
bug Something isn't working in:cache-cleanup
Milestone

Comments

@ashirman
Copy link

ashirman commented Sep 7, 2022

Our gradle action config looks as the following

    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2.3.0
      with:
        gradle-version: wrapper
        gradle-home-cache-cleanup: true
        cache-read-only: false

so assumption is that in post-action gradle will clean up cache in home directory thanks to gradle-home-cache-cleanup: true. Actually in Post Setup Gradle I see the following message

Post job cleanup.
In final post-action step, saving state and writing summary
Stopping all Gradle daemons before saving Gradle User Home state
Stopping Gradle daemons for /home/runner/.gradle/wrapper/dists/gradle-7.4.[2](https://github.com/pleo-io/triton/runs/8232662390?check_suite_focus=true#step:43:2)-bin/48ivgl02cpt2ed[3](https://github.com/pleo-io/triton/runs/8232662390?check_suite_focus=true#step:43:3)fh9dbalvx8/gradle-7.[4](https://github.com/pleo-io/triton/runs/8232662390?check_suite_focus=true#step:43:4).2
/home/runner/.gradle/wrapper/dists/gradle-7.4.2-bin/48ivgl02cpt2ed3fh9dbalvx8/gradle-7.4.2/bin/gradle --stop
Stopping Daemon(s)
1 Daemon stopped
Forcing cache cleanup.
Warning: Unhandled error in Gradle post-action - job will continue: Error: Unable to locate executable file: gradle. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
Error: Unable to locate executable file: gradle. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
    at Object.<anonymous> (/home/runner/work/_actions/gradle/gradle-build-action/v2.3.0/dist/webpack:/gradle-build-action/node_modules/@actions/io/lib/io.js:213:1)
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/_actions/gradle/gradle-build-action/v2.3.0/dist/webpack:/gradle-build-action/node_modules/@actions/io/lib/io.js:24:1)

based on sources https://github.com/gradle/gradle-build-action/blob/main/src/cache-cleaner.ts I see as if dummy gradle project does actual clean up. But from where this project should get gradle to execute this

        await exec.exec(`gradle -g ${this.gradleUserHome} --no-daemon --build-cache --no-scan --quiet noop`, [], {
            cwd: cleanupProjectDir
        })

?

Would you please clarify how to properly configure gradle-home-cache-cleanup so it would get gradle from somewhere. Thanks!

@ashirman
Copy link
Author

ashirman commented Sep 9, 2022

ok, for these who are having the same issue the workaround is the following

    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2.3.0
      with:
        gradle-version: 7.5
        gradle-home-cache-cleanup: true
        cache-read-only: false

so gradle-version must be explicitly declared to be able to make gradle available in PATH. It allows to clean up cache in post step.

Having said that the issue is that gradle-home-cache-cleanup feature does not work as expected if gradle-version: wrapper is used. Not sure if that by design or this is a bug actually.

@andersfischernielsen
Copy link

This seems like a bug to me. Isn't the whole point of using the Wrapper version that you don't have to worry about per-project Gradle versions? 🤔

@bigdaz
Copy link
Member

bigdaz commented Sep 12, 2022

Issue

Thanks for the report. I tested this feature on ubuntu-latest, windows-latest and macos-latest runners, all of which have a recent version of Gradle pre-installed. This pre-installed Gradle version is used for the cache-cleanup process.

However it appears that ubuntu-22.04 does not come preinstalled with Gradle on the PATH, so the cache-cleanup feature will indeed fail. This is a bug in the action: cache-cleanup should work even if the runner does not have a recent version of Gradle pre-installed.

Workaround

You can force the action to download and install a recent gradle version by specifying a gradle-version parameter: the action will install the requested version and add it to the PATH. This isn't normally required if you use the Gradle wrapper, but allows you to ensure that Gradle is available on the runner for cache-cleanup to work.

Note that you can still use the Gradle wrapper to execute your project. Here's a working example:

    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2
      with:
        gradle-version: current     ### This version will be installed and added to PATH, so used for cache-cleanup
        gradle-home-cache-cleanup: true
    
    - name: Execute build with Gradle wrapper
      run: ./gradlew build

@andersfischernielsen
Copy link

Thanks for the quick response @bigdaz!

That's exactly the conclusion we came to too 😄 The problem is that we use a central workflow for all our Gradle projects, so the Gradle version differs from project to project. We can work around this by getting the current gradlew version and using that explicitly, but given that we use gradle-build-action extensively across Pleo and would like to have as few workarounds as possible, I thought I'd fork the action and submit a PR handling the missing gradle in $PATH (by falling back to executing ./gradlew if wrapper is specified as the version).

WDYT?

@andersfischernielsen
Copy link

@bigdaz

Or, alternatively: Would we just be able to use ./gradlew as a parameter to this action's gradle-executable parameter in order to ensure cleanup?

@andersfischernielsen
Copy link

andersfischernielsen commented Sep 12, 2022

We have a PR up in gradle/gradle-build-action#427. I'm unsure about the state handling here - we can probably clean that up with some guidance on the expected architecture 😄

@bigdaz bigdaz changed the title "Unable to locate executable file: gradle." on attempts to use gradle-home-cache-cleanup: true Cache cleanup fails with "Unable to locate executable file: gradle" when Gradle is not pre-installed on runner Sep 13, 2022
@nuhkoca
Copy link

nuhkoca commented Sep 16, 2022

Do we have an update on this?

@bigdaz
Copy link
Member

bigdaz commented Sep 19, 2022

Do we have an update on this?

Not really. The current PR is a bit problematic (assumes Gradle project is root of repository), and I'm not sure when we'll have time to address this.

There is a simple workaround:

  • Use separate "Setup Gradle" and "Run Gradle" steps in your workflow (this is what we recommend anyway)
  • Add gradle-version: current to the "Setup Gradle" step. (This will ensure that Gradle is available on the PATH)
  • Use run: ./gradlew my-build-task in the "Run Gradle" step to build your project with the Gradle wrapper
    - name: Setup Gradle
      uses: gradle/gradle-build-action@v2
      with:
        gradle-version: current
        gradle-home-cache-cleanup: true
    
    - name: Execute build with Gradle wrapper
      run: ./gradlew build

@KENNYSOFT
Copy link
Contributor

KENNYSOFT commented Nov 27, 2022

I've tried to make another PR, but it seems hard since:

  • The first action step could not have information about projectRootDirectory,
  • So in some cases, the executable or wrapper script to execute could only be found at subsequent action step, and it seems no way to load 'the executable or wrapper script to execute' in the post script of the first action step.

Here is a workaround if you are using only a wrapper and do not want to install standalone Gradle at all (only tested in self-hosted runner provisioned by actions-runner-controller):

      - name: Set /usr/bin/gradle alias to gradlew # https://github.com/gradle/actions/issues/33
        run: sudo ln -s "$GITHUB_WORKSPACE/gradlew" /usr/bin/gradle

Thanks to @andersfischernielsen for the base idea!

@yogurtearl
Copy link

Another workaround:

(you will need to swap /home/.gradle for whatever your $GRADLE_USER_HOME is. )

      - name: Enable actions/cache for Gradle
        uses: gradle/gradle-build-action@a4cf152f482c7ca97ef56ead29bf08bcd953284c # v2.7.0
        with:
          gradle-home-cache-cleanup: true
      - name: Gradle Build
        shell: bash
        run: |
          ./gradlew build
          echo "$(find /home/.gradle/wrapper/dists/ -name gradle | xargs ls -tr | tail -n 1 | xargs dirname)" >> $GITHUB_PATH

@bigdaz bigdaz transferred this issue from gradle/gradle-build-action Feb 9, 2024
bigdaz added a commit that referenced this issue Jun 28, 2024
To cleanup Gradle User Home, a Gradle build must be executed.
Newer Gradle versions are able to cleanup the home directories of older versions,
but not vice-versa.

With this change, the latest version of Gradle is automatically provisioned
in order to run Gradle User Home cleanup. This ensures a consistent version of
Gradle is used for cleanup, and fixes #33 where Gradle is not pre-installed on
a custom runner.
bigdaz added a commit that referenced this issue Jun 28, 2024
@bigdaz bigdaz added this to the 3.5.0 milestone Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in:cache-cleanup
Projects
None yet
6 participants