mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-11-25 09:40:57 -05:00
Merge pull request #850 from gradle/dd/docs
Improve docs on Gradle User Home caching
This commit is contained in:
commit
c87c55823d
2 changed files with 331 additions and 243 deletions
572
README.md
572
README.md
|
@ -2,12 +2,26 @@
|
||||||
|
|
||||||
This GitHub Action can be used to configure Gradle and optionally execute a Gradle build on any platform supported by GitHub Actions.
|
This GitHub Action can be used to configure Gradle and optionally execute a Gradle build on any platform supported by GitHub Actions.
|
||||||
|
|
||||||
|
## Why use the `gradle-build-action`?
|
||||||
|
|
||||||
|
It is possible to directly invoke Gradle in your workflow, and the `actions/setup-java@v3` action provides a simple way to cache Gradle dependencies.
|
||||||
|
|
||||||
|
However, the `gradle-build-action` offers a number of advantages over this approach:
|
||||||
|
|
||||||
|
- Easily [configure your workflow to use a specific version of Gradle](#use-a-specific-gradle-version) using the `gradle-version` parameter. Gradle distributions are automatically downloaded and cached.
|
||||||
|
- More sophisticated and more efficient caching of Gradle User Home between invocations, compared to `setup-java` and most custom configurations using `actions/cache`. [More details below](#caching).
|
||||||
|
- Detailed reporting of cache usage and cache configuration options allow you to [optimize the use of the GitHub actions cache](#optimizing-cache-effectiveness).
|
||||||
|
- [Automatic capture of Build Scan® links](#build-scans) from the build, making these easier to locate for workflow run.
|
||||||
|
|
||||||
|
The `gradle-build-action` is designed to provide these benefits with minimal configuration.
|
||||||
|
These features work both when Gradle is executed via the `gradle-build-action` and for any Gradle execution in subsequent steps.
|
||||||
|
|
||||||
## Use the action to setup Gradle
|
## Use the action to setup Gradle
|
||||||
|
|
||||||
If you have an existing workflow invoking Gradle, you can add an initial "Setup Gradle" Step to benefit from caching,
|
The recommended way to use the `gradle-build-action` is in an initial "Setup Gradle" step, with subsquent steps invoking Gradle directly with a `run` step. This makes the action minimally invasive, and allows a workflow to configure and execute a Gradle execution in any way.
|
||||||
build-scan capture and other features of the gradle-build-action.
|
|
||||||
|
Most of the functionality of the `gradle-build-action` is applied via Gradle init-scripts, and so will apply to all subsequent Gradle executions on the runner, no matter how Gradle is invoked. This means that if you have an existing workflow that executes Gradle with a `run` step, you can add an initial "Setup Gradle" Step to benefit from caching, build-scan capture and other features of the gradle-build-action.
|
||||||
|
|
||||||
All subsequent Gradle invocations will benefit from this initial setup, via `init` scripts added to the Gradle User Home.
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
name: Run Gradle on PRs
|
name: Run Gradle on PRs
|
||||||
|
@ -32,21 +46,7 @@ jobs:
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
```
|
```
|
||||||
|
|
||||||
## Why use the `gradle-build-action`?
|
## Choose a specific Gradle version
|
||||||
|
|
||||||
It is possible to directly invoke Gradle in your workflow, and the `actions/setup-java@v3` action provides a simple way to cache Gradle dependencies.
|
|
||||||
|
|
||||||
However, the `gradle-build-action` offers a number of advantages over this approach:
|
|
||||||
|
|
||||||
- Easily [run the build with different versions of Gradle](#use-a-specific-gradle-version) using the `gradle-version` parameter. Gradle distributions are automatically downloaded and cached.
|
|
||||||
- More sophisticated and more efficient caching of Gradle User Home between invocations, compared to `setup-java` and most custom configurations using `actions/cache`. [More details below](#caching).
|
|
||||||
- Detailed reporting of cache usage and cache configuration options allow you to [optimize the use of the GitHub actions cache](#optimizing-cache-effectiveness).
|
|
||||||
- [Automatic capture of Build Scan® links](#build-scans) from the build, making these easier to locate for workflow run.
|
|
||||||
|
|
||||||
The `gradle-build-action` is designed to provide these benefits with minimal configuration.
|
|
||||||
These features work both when Gradle is executed via the `gradle-build-action` and for any Gradle execution in subsequent steps.
|
|
||||||
|
|
||||||
## Use a specific Gradle version
|
|
||||||
|
|
||||||
The `gradle-build-action` can download and install a specified Gradle version, adding this installed version to the PATH.
|
The `gradle-build-action` can download and install a specified Gradle version, adding this installed version to the PATH.
|
||||||
Downloaded Gradle versions are stored in the GitHub Actions cache, to avoid requiring downloading again later.
|
Downloaded Gradle versions are stored in the GitHub Actions cache, to avoid requiring downloading again later.
|
||||||
|
@ -91,9 +91,319 @@ jobs:
|
||||||
- run: gradle build --dry-run # just test build configuration
|
- run: gradle build --dry-run # just test build configuration
|
||||||
```
|
```
|
||||||
|
|
||||||
## Gradle Execution
|
## Caching build state between Jobs
|
||||||
|
|
||||||
If the action is configured with an `arguments` input, then Gradle will execute a Gradle build with the arguments provided.
|
The `gradle-build-action` will use the GitHub Actions cache to save and restore reusable state that may be speed up a subsequent build invocation. This includes most content that is downloaded from the internet as part of a build, as well as expensive to create content like compiled build scripts, transformed Jar files, etc.
|
||||||
|
|
||||||
|
The state that is cached includes:
|
||||||
|
- Any distributions downloaded to satisfy a `gradle-version` parameter ;
|
||||||
|
- A subset of the Gradle User Home directory, including downloaded dependencies, wrapper distributions, and the local build cache ;
|
||||||
|
|
||||||
|
To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.
|
||||||
|
|
||||||
|
State will be restored from the cache during the first `gradle-build-action` step for any workflow job, and cache entries will be written back to the cache at the end of the job, after all Gradle executions have completed.
|
||||||
|
|
||||||
|
### Disabling caching
|
||||||
|
|
||||||
|
Caching is enabled by default. You can disable caching for the action as follows:
|
||||||
|
```yaml
|
||||||
|
cache-disabled: true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using the cache read-only
|
||||||
|
|
||||||
|
By default, the `gradle-build-action` will only write to the cache from Jobs on the default (`main`/`master`) branch.
|
||||||
|
Jobs on other branches will read entries from the cache but will not write updated entries.
|
||||||
|
See [Optimizing cache effectiveness](#optimizing-cache-effectiveness) for a more detailed explanation.
|
||||||
|
|
||||||
|
In some circumstances it makes sense to change this default, and to configure a workflow Job to read existing cache entries but not to write changes back.
|
||||||
|
|
||||||
|
You can configure read-only caching for the `gradle-build-action` as follows:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cache-read-only: true
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also configure read-only caching only for certain branches:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# Only write to the cache for builds on the 'main' and 'release' branches. (Default is 'main' only.)
|
||||||
|
# Builds on other branches will only read existing entries from the cache.
|
||||||
|
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/release' }}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using the cache write-only
|
||||||
|
|
||||||
|
In certain circumstances it may be desirable to start with a clean Gradle User Home state, but to save that state at the end of a workflow Job:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
cache-write-only: true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Incompatibility with other caching mechanisms
|
||||||
|
|
||||||
|
When using `gradle-build-action` we recommend that you avoid using other mechanisms to save and restore the Gradle User Home.
|
||||||
|
|
||||||
|
Specifically:
|
||||||
|
- Avoid using `actions/cache` configured to cache the Gradle User Home, [as described in this example](https://github.com/actions/cache/blob/main/examples.md#java---gradle).
|
||||||
|
- Avoid using `actions/setup-java` with the `cache: gradle` option, [as described here](https://github.com/actions/setup-java#caching-gradle-dependencies).
|
||||||
|
|
||||||
|
Using either of these mechanisms may interfere with the caching provided by this action. If you choose to use a different mechanism to save and restore the Gradle User Home, you should disable the caching provided by this action, as described above.
|
||||||
|
|
||||||
|
### Cache debugging and analysis
|
||||||
|
|
||||||
|
A report of all cache entries restored and saved is printed to the Job Summary when saving the cache entries.
|
||||||
|
This report can provide valuable insignt into how much cache space is being used.
|
||||||
|
|
||||||
|
It is possible to enable additional debug logging for cache operations. You do via the `GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED` environment variable:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
env:
|
||||||
|
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that this setting will also prevent certain cache operations from running in parallel, further assisting with debugging.
|
||||||
|
|
||||||
|
## How Gradle User Home caching works
|
||||||
|
|
||||||
|
### Properties of the GitHub Actions cache
|
||||||
|
|
||||||
|
The GitHub Actions cache has some properties that present problems for efficient caching of the Gradle User Home.
|
||||||
|
- Immutable entries: once a cache entry is written for a key, it cannot be overwritten or changed.
|
||||||
|
- Branch scope: cache entries written for a Git branch are not visible from actions running against different branches. Entries written for the default branch are visible to all. https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
|
||||||
|
- Restore keys: if no exact match is found, a set of partial keys can be provided that will match by cache key prefix. https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
|
||||||
|
|
||||||
|
Each of these properties has influenced the design and implementation of the caching in `gradle-build-action`, as described below.
|
||||||
|
|
||||||
|
### Which content is cached
|
||||||
|
|
||||||
|
Using experiments and observations, we have attempted to identify which Gradle User Home content is worth saving and restoring between build invocations. We considered both the respective size of the content and the impact this content has on build times. As well as the obvious candidates like downloaded dependencies, we saw that compiled build scripts, transformed Jar files and other content can also have a significant impact.
|
||||||
|
|
||||||
|
In the end, we opted to save and restore as much content as is practical, including:
|
||||||
|
- `caches/<version>/generated-gradle-jars`: These files are generated on first use of a particular Gradle version, and are expensive to recreate
|
||||||
|
- `caches/<version>/kotlin-dsl` and `caches/<version>/scripts`: These are the compiled build scripts. The Kotlin ones in particular can benefit from caching.
|
||||||
|
- `caches/modules-2`: The downloaded dependencies
|
||||||
|
- `caches/transforms-3`: The results of artifact transforms
|
||||||
|
- `caches/jars-9`: Jar files that have been processed/instrumented by Gradle
|
||||||
|
- `caches/build-cache-1`: The local build cache
|
||||||
|
|
||||||
|
In certain cases a particular section of Gradle User Home will be too large to make caching effective. In these cases, particular subdirectories can be excluded from caching. See [Exclude content from Gradle User Home cache](#exclude-content-from-gradle-user-home-cache).
|
||||||
|
|
||||||
|
### Cache keys
|
||||||
|
|
||||||
|
The actual content of the Gradle User Home after a build is the result of many factors, including:
|
||||||
|
- Core Gradle build files (`settngs.gradle[.kts]`, `build.gradle[.kts]`, `gradle.properties`)
|
||||||
|
- Associated Gradle configuration files (`gradle-wrapper.properties`, `dependencies.toml`, etc)
|
||||||
|
- The entire content of `buildSrc` or any included builds that provide plugins.
|
||||||
|
- The entire content of the repository, in the case of the local build cache.
|
||||||
|
- The actual build command that was invoked, including system properties and environment variables.
|
||||||
|
|
||||||
|
For this reason, it's very difficult to create a cache key that will deterministically map to a saved Gradle User Home state. So instead of trying to reliably hash all of these inputs to generate a cache key, the Gradle User Home cache key is based on the currently executing Job and the current commit hash for the repository.
|
||||||
|
|
||||||
|
The Gradle User Home cache key is composed of:
|
||||||
|
- The current operating system (`RUNNER_OS`)
|
||||||
|
- The workflow name and Job ID
|
||||||
|
- A hash of the Job matrix parameters
|
||||||
|
- The git SHA for the latest commit
|
||||||
|
|
||||||
|
Specifically, the cache key is: `${cache-protocol}-gradle|${runner-os}|${workflow-name}-${job-id}[${hash-of-job-matrix}]-${git-sha}`
|
||||||
|
|
||||||
|
As such, the cache key is likely to change on each subsequent run of GitHub actions.
|
||||||
|
This allows the most recent state to always be available in the GitHub actions cache.
|
||||||
|
|
||||||
|
### Finding a matching cache entry
|
||||||
|
|
||||||
|
In most cases, no exact match will exist for the cache key. Instead, the Gradle User Home will be restored for the closest matching cache entry, using a set of "restore keys". The entries will be matched with the following precedence:
|
||||||
|
- An exact match on OS, workflow, job, matrix and Git SHA
|
||||||
|
- The most recent entry saved for the same OS, workflow, job and matrix values
|
||||||
|
- The most recent entry saved for the same OS, workflow and job
|
||||||
|
- The most recent entry saved for the same OS
|
||||||
|
|
||||||
|
Due to branch scoping of cache entries, the above match will be first performed for entries from the same branch, and then for the default ('main') branch.
|
||||||
|
|
||||||
|
After the Job is complete, the current Gradle User Home state will be collected and written as a new cache entry with the complete cache key. Old entries will be expunged from the GitHub Actions cache on a least-recently-used basis.
|
||||||
|
|
||||||
|
Note that while effective, this mechanism is not inherently efficient. It requires the entire Gradle User Home directory to be stored separately for each branch, for every OS+Job+Matrix combination. In addition, a new cache entry to be written on every GitHub Actions run.
|
||||||
|
|
||||||
|
This inefficiency is effectively mitigated by [Deduplication of Gradle User Home cache entries](#deduplication-of-gradle-user-home-cache-entries), and can be further optimized for a workflow using the techniques described in [Optimizing cache effectiveness](#optimizing-cache-effectiveness).
|
||||||
|
|
||||||
|
### Deduplication of Gradle User Home cache entries
|
||||||
|
|
||||||
|
To reduce duplication between cache entries, certain artifacts in Gradle User Home are extracted and cached independently based on their identity. This allows each Gradle User Home cache entry to be relatively small, sharing common elements between them without duplication.
|
||||||
|
|
||||||
|
Artifacts that are cached independently include:
|
||||||
|
- Downloaded dependencies
|
||||||
|
- Downloaded wrapper distributions
|
||||||
|
- Generated Gradle API jars
|
||||||
|
- Downloaded Java Toolchains
|
||||||
|
|
||||||
|
For example, this means that all jobs executing a particular version of the Gradle wrapper will share a single common entry for this wrapper distribution and one for each of the generated Gradle API jars.
|
||||||
|
|
||||||
|
### Stopping the Gradle daemon
|
||||||
|
|
||||||
|
By default, the action will stop all running Gradle daemons in the post-action step, prior to saving the Gradle User Home state.
|
||||||
|
This allows for any Gradle User Home cleanup to occur, and avoid file-locking issues on Windows.
|
||||||
|
|
||||||
|
If caching is disabled or the cache is in read-only mode, the daemon will not be stopped and will continue running after the job is completed.
|
||||||
|
|
||||||
|
## Optimizing cache effectiveness
|
||||||
|
|
||||||
|
Cache storage space for GitHub actions is limited, and writing new cache entries can trigger the deletion of existing entries.
|
||||||
|
Eviction of shared cache entries can reduce cache effectiveness, slowing down your `gradle-build-action` steps.
|
||||||
|
|
||||||
|
There are a number of actions you can take if your cache use is less effective due to entry eviction.
|
||||||
|
|
||||||
|
At the end of a Job, the `gradle-build-action` will write a summary of the Gradle builds executed, together with a detailed report of the cache entries that were read and written during the Job. This report can provide valuable insights that may help to determine the right way to optimize the cache usage for your workflow.
|
||||||
|
|
||||||
|
### Select which jobs should write to the cache
|
||||||
|
|
||||||
|
Consider a workflow that first runs a Job "compile-and-unit-test" to compile the code and run some basic unit tests, which is followed by a matrix of parallel "integration-test" jobs that each run a set of integration tests for the repository. Each "integration test" Job requires all of the dependencies required by "compile-and-unit-test", and possibly one or 2 additional dependencies.
|
||||||
|
|
||||||
|
By default, a new cache entry will be written on completion of each integration test job. If no additional dependencies were downloaded then this cache entry will share the "dependencies" entry with the "compile-and-unit-test" job, but if a single dependency was downloaded then an entire new "dependencies" entry would be written. (The `gradle-build-action` does not _yet_ support a layered cache that could do this more efficiently). If each of these "integration-test" entries with their different "dependencies" entries is too large, then it could result in other important entries being evicted from the GitHub Actions cache.
|
||||||
|
|
||||||
|
There are some techniques that can be used to avoid/mitigate this issue:
|
||||||
|
- Configure the "integration-test" jobs with `cache-read-only: true`, meaning that the Job will use the entry written by the "compile-and-unit-test" job. This will avoid the overhead of cache entries for each of these jobs, at the expense of re-downloading any additional dependencies required by "integration-test".
|
||||||
|
- Add an additional step to the "compile-and-unit-test" job which downloads all dependencies required by the integration-test jobs but does not execute the tests. This will allow the "dependencies" entry for "compile-and-unit-test" to be shared among all cache entries for "integration-test". The resulting "integration-test" entries should be much smaller, reducing the potential for eviction.
|
||||||
|
- Combine the above 2 techniques, so that no cache entry is written by "integration-test" jobs, but all required dependencies are already present from the restored "compile-and-unit-test" entry.
|
||||||
|
|
||||||
|
### Select which branches should write to the cache
|
||||||
|
|
||||||
|
GitHub cache entries are not shared between builds on different branches.
|
||||||
|
This means that each PR branch will have it's own Gradle User Home cache, and will not benefit from cache entries written by other PR branches.
|
||||||
|
An exception to this is that cache entries written in parent and upstream branches are visible to child branches, and cache entries for the default (`master`/`main`) branch can be read by actions invoked for any other branch.
|
||||||
|
|
||||||
|
By default, the `gradle-build-action` will only _write_ to the cache for builds run on the default (`master`/`main`) branch.
|
||||||
|
Jobs run on other branches will only read from the cache. In most cases, this is the desired behaviour,
|
||||||
|
because Jobs run against other branches will benefit from the cache Gradle User Home from `main`,
|
||||||
|
without writing private cache entries that could lead to evicting shared entries.
|
||||||
|
|
||||||
|
If you have other long-lived development branches that would benefit from writing to the cache,
|
||||||
|
you can configure these by overriding the `cache-read-only` action parameter.
|
||||||
|
See [Using the caches read-only](#using-the-caches-read-only) for more details.
|
||||||
|
|
||||||
|
Similarly, you could use `cache-read-only` for certain jobs in the workflow, and instead have these jobs reuse the cache content from upstream jobs.
|
||||||
|
|
||||||
|
### Exclude content from Gradle User Home cache
|
||||||
|
|
||||||
|
As well as any wrapper distributions, the action will attempt to save and restore the `caches` and `notifications` directories from Gradle User Home.
|
||||||
|
|
||||||
|
Each build is different, and some builds produce more Gradle User Home content than others.
|
||||||
|
[Cache debugging ](#cache-debugging-and-analysis) can provide insight into which cache entries are the largest,
|
||||||
|
and the contents to be cached can be fine tuned by including and excluding certain paths within Gradle User Home.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# Cache downloaded JDKs in addition to the default directories.
|
||||||
|
gradle-home-cache-includes: |
|
||||||
|
caches
|
||||||
|
notifications
|
||||||
|
jdks
|
||||||
|
# Exclude the local build-cache and keyrings from the directories cached.
|
||||||
|
gradle-home-cache-excludes: |
|
||||||
|
caches/build-cache-1
|
||||||
|
caches/keyrings
|
||||||
|
```
|
||||||
|
|
||||||
|
You can specify any number of fixed paths or patterns to include or exclude.
|
||||||
|
File pattern support is documented at https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#patterns-to-match-file-paths.
|
||||||
|
|
||||||
|
### Remove unused files from Gradle User Home before saving to cache
|
||||||
|
|
||||||
|
The Gradle User Home directory has a tendency to grow over time. When you switch to a new Gradle wrapper version or upgrade a dependency version
|
||||||
|
the old files are not automatically and immediately removed. While this can make sense in a local environment, in a GitHub Actions environment
|
||||||
|
it can lead to ever-larger Gradle User Home cache entries being saved and restored.
|
||||||
|
|
||||||
|
In order to avoid this situation, the `gradle-build-action` supports the `gradle-home-cache-cleanup` parameter.
|
||||||
|
When enabled, this feature will attempt to delete any files in the Gradle User Home that were not used by Gradle during the GitHub Actions workflow,
|
||||||
|
prior to saving the Gradle User Home to the GitHub Actions cache.
|
||||||
|
|
||||||
|
Gradle Home cache cleanup is considered experimental and is disabled by default. You can enable this feature for the action as follows:
|
||||||
|
```yaml
|
||||||
|
gradle-home-cache-cleanup: true
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build reporting
|
||||||
|
|
||||||
|
The `gradle-build-action` collects information about any Gradle executions that occur in a workflow, and reports these via
|
||||||
|
a Job Summary, visible in the GitHub Actions UI. For each Gradle execution, details about the invocation are listed, together with
|
||||||
|
a link to any Build Scan® published.
|
||||||
|
|
||||||
|
Generation of a Job Summary is enabled by default. If this is not desired, it can be disable as follows:
|
||||||
|
```yaml
|
||||||
|
generate-job-summary: false
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that the action collects information about Gradle invocations via an [Initialization Script](https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script)
|
||||||
|
located at `USER_HOME/.gradle/init.d/build-result-capture.init.gradle`.
|
||||||
|
If you are using init scripts for the [Gradle Enterprise Gradle Plugin](https://plugins.gradle.org/plugin/com.gradle.enterprise) like
|
||||||
|
[`scans-init.gradle` or `gradle-enterprise-init.gradle`](https://docs.gradle.com/enterprise/gradle-plugin/#scans_gradle_com),
|
||||||
|
you'll need to ensure these files are applied prior to `build-result-capture.init.gradle`.
|
||||||
|
Since Gradle applies init scripts in alphabetical order, one way to ensure this is via file naming.
|
||||||
|
|
||||||
|
### Build Scan® link as Step output
|
||||||
|
|
||||||
|
As well as reporting the [Build Scan](https://gradle.com/build-scans/) link in the Job Summary,
|
||||||
|
the `gradle-build-action` action makes this link available as a Step output named `build-scan-url`.
|
||||||
|
|
||||||
|
You can then use that link in subsequent actions of your workflow. For example:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# .github/workflows/gradle-build-pr.yml
|
||||||
|
name: Run Gradle on PRs
|
||||||
|
on: pull_request
|
||||||
|
jobs:
|
||||||
|
gradle:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout project sources
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Setup Gradle
|
||||||
|
uses: gradle/gradle-build-action@v2
|
||||||
|
- name: Run build with Gradle wrapper
|
||||||
|
id: gradle
|
||||||
|
run: ./gradlew build --scan
|
||||||
|
- name: "Add Build Scan URL as PR comment"
|
||||||
|
uses: actions/github-script@v5
|
||||||
|
if: github.event_name == 'pull_request' && failure()
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
script: |
|
||||||
|
github.rest.issues.createComment({
|
||||||
|
issue_number: context.issue.number,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
body: '❌ ${{ github.workflow }} failed: ${{ steps.gradle.outputs.build-scan-url }}'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Saving build outputs
|
||||||
|
|
||||||
|
By default, a GitHub Actions workflow using `gradle-build-action` will record the log output and any Build Scan links for your build,
|
||||||
|
but any output files generated by the build will not be saved.
|
||||||
|
|
||||||
|
To save selected files from your build execution, you can use the core [Upload-Artifact](https://github.com/actions/upload-artifact) action.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
jobs:
|
||||||
|
gradle:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout project sources
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Setup Gradle
|
||||||
|
uses: gradle/gradle-build-action@v2
|
||||||
|
- name: Run build with Gradle wrapper
|
||||||
|
run: ./gradlew build --scan
|
||||||
|
- name: Upload build reports
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: build-reports
|
||||||
|
path: build/reports/
|
||||||
|
```
|
||||||
|
|
||||||
|
## Use the action to invoke Gradle
|
||||||
|
|
||||||
|
If the `gradle-build-action` is configured with an `arguments` input, then Gradle will execute a Gradle build with the arguments provided. NOTE: We recommend using the `gradle-build-action` as a "Setup Gradle" step as described above, with Gradle being invoked via a regular `run` command.
|
||||||
|
|
||||||
If no `arguments` are provided, the action will not execute Gradle, but will still cache Gradle state and configure build-scan capture for all subsequent Gradle executions.
|
If no `arguments` are provided, the action will not execute Gradle, but will still cache Gradle state and configure build-scan capture for all subsequent Gradle executions.
|
||||||
|
|
||||||
|
@ -188,228 +498,6 @@ Use the `gradle-executable` input to execute using a specific Gradle installatio
|
||||||
|
|
||||||
This mechanism can also be used to target a Gradle wrapper script that is located in a non-default location.
|
This mechanism can also be used to target a Gradle wrapper script that is located in a non-default location.
|
||||||
|
|
||||||
## Caching
|
|
||||||
|
|
||||||
By default, this action aims to cache any and all reusable state that may be speed up a subsequent build invocation.
|
|
||||||
|
|
||||||
The state that is cached includes:
|
|
||||||
- Any distributions downloaded to satisfy a `gradle-version` parameter ;
|
|
||||||
- A subset of the Gradle User Home directory, including downloaded dependencies, wrapper distributions, and the local build cache ;
|
|
||||||
|
|
||||||
To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.
|
|
||||||
|
|
||||||
Caching is enabled by default. You can disable caching for the action as follows:
|
|
||||||
```yaml
|
|
||||||
cache-disabled: true
|
|
||||||
```
|
|
||||||
|
|
||||||
### Incompatibility with other caching mechanisms
|
|
||||||
|
|
||||||
When using `gradle-build-action` we recommend that you avoid using other mechanisms to save and restore the Gradle User Home.
|
|
||||||
|
|
||||||
Specifically:
|
|
||||||
- Avoid using `actions/cache` configured to cache the Gradle User Home, [as described in this example](https://github.com/actions/cache/blob/main/examples.md#java---gradle).
|
|
||||||
- Avoid using `actions/setup-java` with the `cache: gradle` option, [as described here](https://github.com/actions/setup-java#caching-gradle-dependencies).
|
|
||||||
|
|
||||||
Using either of these mechanisms may interfere with the caching provided by this action. If you choose to use a different mechanism to save and restore the Gradle User Home, you should disable the caching provided by this action, as described above.
|
|
||||||
|
|
||||||
### Cache keys
|
|
||||||
|
|
||||||
Distributions downloaded to satisfy a `gradle-version` parameter are stored outside of Gradle User Home and cached separately. The cache key is unique to the downloaded distribution and will not change over time.
|
|
||||||
|
|
||||||
The state of the Gradle User Home is highly dependent on the Gradle execution, so the cache key is composed of the current commit hash and the GitHub actions job id.
|
|
||||||
As such, the cache key is likely to change on each subsequent run of GitHub actions.
|
|
||||||
This allows the most recent state to always be available in the GitHub actions cache.
|
|
||||||
|
|
||||||
To reduce duplication between cache entries, certain artifacts are cached independently based on their identity.
|
|
||||||
Artifacts that are cached independently include downloaded dependencies, downloaded wrapper distributions and generated Gradle API jars.
|
|
||||||
For example, this means that all jobs executing a particular version of the Gradle wrapper will share common entries for wrapper distributions and for generated Gradle API jars.
|
|
||||||
|
|
||||||
### Using the caches read-only
|
|
||||||
|
|
||||||
By default, the `gradle-build-action` will only write to the cache from Jobs on the default (`main`/`master`) branch.
|
|
||||||
Jobs on other branches will read entries from the cache but will not write updated entries.
|
|
||||||
See [Optimizing cache effectiveness](#optimizing-cache-effectiveness) for a more detailed explanation.
|
|
||||||
|
|
||||||
In some circumstances it makes sense to change this default, and to configure a workflow Job to read existing cache entries but not to write changes back.
|
|
||||||
|
|
||||||
You can configure read-only caching for the `gradle-build-action` as follows:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# Only write to the cache for builds on the 'main' and 'release' branches. (Default is 'main' only.)
|
|
||||||
# Builds on other branches will only read existing entries from the cache.
|
|
||||||
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/release' }}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Stopping the Gradle daemon
|
|
||||||
|
|
||||||
By default, the action will stop all running Gradle daemons in the post-action step, prior to saving the Gradle User Home state.
|
|
||||||
This allows for any Gradle User Home cleanup to occur, and avoid file-locking issues on Windows.
|
|
||||||
|
|
||||||
If caching is unavailable or the cache is in read-only mode, the daemon will not be stopped and will continue running after the job is completed.
|
|
||||||
|
|
||||||
### Gradle User Home cache tuning
|
|
||||||
|
|
||||||
As well as any wrapper distributions, the action will attempt to save and restore the `caches` and `notifications` directories from Gradle User Home.
|
|
||||||
|
|
||||||
The contents to be cached can be fine tuned by including and excluding certain paths with Gradle User Home.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# Cache downloaded JDKs in addition to the default directories.
|
|
||||||
gradle-home-cache-includes: |
|
|
||||||
caches
|
|
||||||
notifications
|
|
||||||
jdks
|
|
||||||
# Exclude the local build-cache and keyrings from the directories cached.
|
|
||||||
gradle-home-cache-excludes: |
|
|
||||||
caches/build-cache-1
|
|
||||||
caches/keyrings
|
|
||||||
```
|
|
||||||
|
|
||||||
You can specify any number of fixed paths or patterns to include or exclude.
|
|
||||||
File pattern support is documented at https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#patterns-to-match-file-paths.
|
|
||||||
|
|
||||||
### Cache debugging and analysis
|
|
||||||
|
|
||||||
Gradle User Home state will be restored from the cache during the first `gradle-build-action` step for any workflow job.
|
|
||||||
This state will be saved back to the cache at the end of the job, after all Gradle executions have completed.
|
|
||||||
A report of all cache entries restored and saved is printed to the Job Summary when saving the cache entries.
|
|
||||||
This report can provide valuable insignt into how much cache space is being used.
|
|
||||||
|
|
||||||
It is possible to enable additional debug logging for cache operations. You do via the `GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED` environment variable:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
env:
|
|
||||||
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that this setting will also prevent certain cache operations from running in parallel, further assisting with debugging.
|
|
||||||
|
|
||||||
### Optimizing cache effectiveness
|
|
||||||
|
|
||||||
Cache storage space for GitHub actions is limited, and writing new cache entries can trigger the deletion of existing entries.
|
|
||||||
Eviction of shared cache entries can reduce cache effectiveness, slowing down your `gradle-build-action` steps.
|
|
||||||
|
|
||||||
There are a number of actions you can take if your cache use is less effective due to entry eviction.
|
|
||||||
|
|
||||||
#### Select branches that should write to the cache
|
|
||||||
|
|
||||||
GitHub cache entries are not shared between builds on different branches.
|
|
||||||
This means that each PR branch will have it's own Gradle User Home cache, and will not benefit from cache entries written by other PR branches.
|
|
||||||
An exception to this is that cache entries written in parent and upstream branches are visible to child branches, and cache entries for the default (`master`/`main`) branch can be read by actions invoked for any other branch.
|
|
||||||
|
|
||||||
By default, the `gradle-build-action` will only _write_ to the cache for builds run on the default (`master`/`main`) branch.
|
|
||||||
Jobs run on other branches will only read from the cache. In most cases, this is the desired behaviour,
|
|
||||||
because Jobs run against other branches will benefit from the cache Gradle User Home from `main`,
|
|
||||||
without writing private cache entries that could lead to evicting shared entries.
|
|
||||||
|
|
||||||
If you have other long-lived development branches that would benefit from writing to the cache,
|
|
||||||
you can configure these by overriding the `cache-read-only` action parameter.
|
|
||||||
See [Using the caches read-only](#using-the-caches-read-only) for more details.
|
|
||||||
|
|
||||||
Similarly, you could use `cache-read-only` for certain jobs in the workflow, and instead have these jobs reuse the cache content from upstream jobs.
|
|
||||||
|
|
||||||
#### Exclude content from Gradle User Home cache
|
|
||||||
|
|
||||||
Each build is different, and some builds produce more Gradle User Home content than others.
|
|
||||||
[Cache debugging ](#cache-debugging-and-analysis) can provide insight into which cache entries are the largest,
|
|
||||||
and you can selectively [exclude content using `gradle-home-cache-exclude`](#gradle-user-home-cache-tuning).
|
|
||||||
|
|
||||||
#### Removing unused files from Gradle User Home before saving to cache
|
|
||||||
|
|
||||||
The Gradle User Home directory has a tendency to grow over time. When you switch to a new Gradle wrapper version or upgrade a dependency version
|
|
||||||
the old files are not automatically and immediately removed. While this can make sense in a local environment, in a GitHub Actions environment
|
|
||||||
it can lead to ever-larger Gradle User Home cache entries being saved and restored.
|
|
||||||
|
|
||||||
In order to avoid this situation, the `gradle-build-action` supports the `gradle-home-cache-cleanup` parameter.
|
|
||||||
When enabled, this feature will attempt to delete any files in the Gradle User Home that were not used by Gradle during the GitHub Actions workflow,
|
|
||||||
prior to saving the Gradle User Home to the GitHub Actions cache.
|
|
||||||
|
|
||||||
Gradle Home cache cleanup is disabled by default. You can enable this feature for the action as follows:
|
|
||||||
```yaml
|
|
||||||
gradle-home-cache-cleanup: true
|
|
||||||
```
|
|
||||||
|
|
||||||
## Build reporting
|
|
||||||
|
|
||||||
The `gradle-build-action` collects information about any Gradle executions that occur in a workflow, and reports these via
|
|
||||||
a Job Summary, visible in the GitHub Actions UI. For each Gradle execution, details about the invocation are listed, together with
|
|
||||||
a link to any Build Scan® published.
|
|
||||||
|
|
||||||
Generation of a Job Summary is enabled by default. If this is not desired, it can be disable as follows:
|
|
||||||
```yaml
|
|
||||||
generate-job-summary: false
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that the action collects information about Gradle invocations via an [Initialization Script](https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script)
|
|
||||||
located at `USER_HOME/.gradle/init.d/build-result-capture.init.gradle`.
|
|
||||||
If you are using init scripts for the [Gradle Enterprise Gradle Plugin](https://plugins.gradle.org/plugin/com.gradle.enterprise) like
|
|
||||||
[`scans-init.gradle` or `gradle-enterprise-init.gradle`](https://docs.gradle.com/enterprise/gradle-plugin/#scans_gradle_com),
|
|
||||||
you'll need to ensure these files are applied prior to `build-result-capture.init.gradle`.
|
|
||||||
Since Gradle applies init scripts in alphabetical order, one way to ensure this is via file naming.
|
|
||||||
|
|
||||||
### Build Scan® link as Step output
|
|
||||||
|
|
||||||
As well as reporting the [Build Scan](https://gradle.com/build-scans/) link in the Job Summary,
|
|
||||||
the `gradle-build-action` action makes this link available as a Step output named `build-scan-url`.
|
|
||||||
|
|
||||||
You can then use that link in subsequent actions of your workflow. For example:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# .github/workflows/gradle-build-pr.yml
|
|
||||||
name: Run Gradle on PRs
|
|
||||||
on: pull_request
|
|
||||||
jobs:
|
|
||||||
gradle:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout project sources
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: Setup Gradle
|
|
||||||
uses: gradle/gradle-build-action@v2
|
|
||||||
- name: Run build with Gradle wrapper
|
|
||||||
id: gradle
|
|
||||||
run: ./gradlew build --scan
|
|
||||||
- name: "Add Build Scan URL as PR comment"
|
|
||||||
uses: actions/github-script@v5
|
|
||||||
if: github.event_name == 'pull_request' && failure()
|
|
||||||
with:
|
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
||||||
script: |
|
|
||||||
github.rest.issues.createComment({
|
|
||||||
issue_number: context.issue.number,
|
|
||||||
owner: context.repo.owner,
|
|
||||||
repo: context.repo.repo,
|
|
||||||
body: '❌ ${{ github.workflow }} failed: ${{ steps.gradle.outputs.build-scan-url }}'
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Saving build outputs
|
|
||||||
|
|
||||||
By default, a GitHub Actions workflow using `gradle-build-action` will record the log output and any Build Scan links for your build,
|
|
||||||
but any output files generated by the build will not be saved.
|
|
||||||
|
|
||||||
To save selected files from your build execution, you can use the core [Upload-Artifact](https://github.com/actions/upload-artifact) action.
|
|
||||||
For example:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
jobs:
|
|
||||||
gradle:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout project sources
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: Setup Gradle
|
|
||||||
uses: gradle/gradle-build-action@v2
|
|
||||||
- name: Run build with Gradle wrapper
|
|
||||||
run: ./gradlew build --scan
|
|
||||||
- name: Upload build reports
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
|
||||||
name: build-reports
|
|
||||||
path: build/reports/
|
|
||||||
```
|
|
||||||
|
|
||||||
## Support for GitHub Enterprise Server (GHES)
|
## Support for GitHub Enterprise Server (GHES)
|
||||||
|
|
||||||
You can use the `gradle-build-action` on GitHub Enterprise Server, and benefit from the improved integration with Gradle. Depending on the version of GHES you are running, certain features may be limited:
|
You can use the `gradle-build-action` on GitHub Enterprise Server, and benefit from the improved integration with Gradle. Depending on the version of GHES you are running, certain features may be limited:
|
||||||
|
|
|
@ -66,7 +66,7 @@ export class CacheKey {
|
||||||
* - The cache protocol version
|
* - The cache protocol version
|
||||||
* - The name of the cache
|
* - The name of the cache
|
||||||
* - The runner operating system
|
* - The runner operating system
|
||||||
* - The name of the Job being executed
|
* - The name of the workflow and Job being executed
|
||||||
* - The matrix values for the Job being executed (job context)
|
* - The matrix values for the Job being executed (job context)
|
||||||
* - The SHA of the commit being executed
|
* - The SHA of the commit being executed
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue