2021-08-07 15:17:27 -07:00
# Execute Gradle builds in GitHub Actions workflows
2019-09-20 23:06:59 +02:00
2021-12-08 13:33:39 -07:00
This GitHub Action can be used to configure Gradle and optionally execute a Gradle build on any platform supported by GitHub Actions.
2019-09-20 23:06:59 +02:00
2021-12-30 18:12:37 -07:00
## Use the action to setup Gradle
2019-09-20 23:06:59 +02:00
2021-12-30 18:12:37 -07:00
If you have an existing workflow invoking Gradle, you can add an initial "Setup Gradle" Step to benefit from caching,
2021-12-17 13:34:22 -07:00
build-scan capture and other features of the gradle-build-action.
2021-12-30 18:12:37 -07:00
All subsequent Gradle invocations will benefit from this initial setup, via `init` scripts added to the Gradle User Home.
2019-09-21 16:01:53 +02:00
```yaml
name: Run Gradle on PRs
2020-04-14 10:08:39 +03:00
on: pull_request
2019-09-21 16:01:53 +02:00
jobs:
gradle:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
2022-06-04 09:26:11 -06:00
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
2019-09-21 16:01:53 +02:00
with:
2022-01-17 14:28:53 -07:00
distribution: temurin
2019-09-21 16:01:53 +02:00
java-version: 11
2021-12-17 13:34:22 -07:00
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Execute Gradle build
run: ./gradlew build
2019-09-21 16:01:53 +02:00
```
2021-12-17 13:34:22 -07:00
## Why use the `gradle-build-action`?
2021-11-01 12:35:15 -06:00
2022-06-04 09:26:11 -06:00
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.
2021-11-01 12:35:15 -06:00
However, the `gradle-build-action` offers a number of advantages over this approach:
2022-12-08 13:03:46 +00:00
- 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.
2021-11-01 12:35:15 -06:00
- 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.
2021-12-08 13:33:39 -07:00
These features work both when Gradle is executed via the `gradle-build-action` and for any Gradle execution in subsequent steps.
2022-06-04 09:26:11 -06:00
When using `gradle-build-action` we recommend that you _not_ use `actions/cache` or `actions/setup-java@v3` to explicitly cache the Gradle User Home. Doing so may interfere with the caching provided by this action.
2022-01-16 16:18:09 -07:00
2021-12-30 18:12:37 -07:00
## Use a specific Gradle version
2021-12-08 13:33:39 -07:00
2021-12-30 18:12:37 -07:00
The `gradle-build-action` can download and install a specified Gradle version, adding this installed version to the PATH.
2021-12-08 13:33:39 -07:00
Downloaded Gradle versions are stored in the GitHub Actions cache, to avoid requiring downloading again later.
```yaml
- uses: gradle/gradle-build-action@v2
with:
gradle-version: 6.5
```
The `gradle-version` parameter can be set to any valid Gradle version.
Moreover, you can use the following aliases:
| Alias | Selects |
| --- |---|
| `wrapper` | The Gradle wrapper's version (default, useful for matrix builds) |
| `current` | The current [stable release ](https://gradle.org/install/ ) |
| `release-candidate` | The current [release candidate ](https://gradle.org/release-candidate/ ) if any, otherwise fallback to `current` |
| `nightly` | The latest [nightly ](https://gradle.org/nightly/ ), fails if none. |
| `release-nightly` | The latest [release nightly ](https://gradle.org/release-nightly/ ), fails if none. |
This can be handy to automatically verify your build works with the latest release candidate of Gradle:
```yaml
name: Test latest Gradle RC
on:
schedule:
- cron: 0 0 * * * # daily
jobs:
gradle-rc:
runs-on: ubuntu-latest
steps:
2022-06-04 09:26:11 -06:00
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
2021-12-08 13:33:39 -07:00
with:
2022-06-14 12:11:44 -06:00
distribution: temurin
2021-12-08 13:33:39 -07:00
java-version: 11
- uses: gradle/gradle-build-action@v2
with:
gradle-version: release-candidate
- run: gradle build --dry-run # just test build configuration
```
2021-11-01 12:35:15 -06:00
2021-09-28 11:30:55 -06:00
## Gradle Execution
2021-12-08 13:33:39 -07:00
If the action is configured with an `arguments` input, then Gradle will execute a Gradle build with the arguments provided.
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.
2021-12-30 18:12:37 -07:00
```yaml
name: Run Gradle on PRs
on: pull_request
jobs:
gradle:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
2022-06-04 09:26:11 -06:00
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
2021-12-30 18:12:37 -07:00
with:
2022-06-14 12:11:44 -06:00
distribution: temurin
2021-12-30 18:12:37 -07:00
java-version: 11
- name: Setup and execute Gradle 'test' task
uses: gradle/gradle-build-action@v2
with:
arguments: test
```
### Multiple Gradle executions in the same Job
It is possible to configure multiple Gradle executions to run sequentially in the same job.
The initial Action step will perform the Gradle setup.
```yaml
- uses: gradle/gradle-build-action@v2
with:
arguments: assemble
- uses: gradle/gradle-build-action@v2
with:
arguments: check
```
2021-12-08 13:33:39 -07:00
### Gradle command-line arguments
2019-09-21 16:01:53 +02:00
2022-05-15 18:33:34 -06:00
The `arguments` input can be used to pass arbitrary arguments to the `gradle` command line.
2021-10-15 12:24:41 -06:00
Arguments can be supplied in a single line, or as a multi-line input.
2019-09-21 16:01:53 +02:00
Here are some valid examples:
```yaml
arguments: build
arguments: check --scan
arguments: some arbitrary tasks
arguments: build -PgradleProperty=foo
2021-10-15 12:24:41 -06:00
arguments: |
build
--scan
-PgradleProperty=foo
-DsystemProperty=bar
2019-09-21 16:01:53 +02:00
```
2021-09-28 11:30:55 -06:00
If you need to pass environment variables, use the GitHub Actions workflow syntax:
2019-09-21 16:01:53 +02:00
```yaml
2021-09-28 11:30:55 -06:00
- uses: gradle/gradle-build-action@v2
2019-09-21 16:01:53 +02:00
env:
CI: true
2021-09-28 11:30:55 -06:00
with:
arguments: build
2019-09-21 16:01:53 +02:00
```
2021-09-28 11:30:55 -06:00
### Gradle build located in a subdirectory
By default, the action will execute Gradle in the root directory of your project.
Use the `build-root-directory` input to target a Gradle build in a subdirectory.
2019-09-21 16:01:53 +02:00
```yaml
2021-09-28 11:30:55 -06:00
- uses: gradle/gradle-build-action@v2
2019-09-21 16:01:53 +02:00
with:
2021-12-08 13:33:39 -07:00
arguments: build
2019-09-21 16:01:53 +02:00
build-root-directory: some/subdirectory
```
2021-09-28 11:30:55 -06:00
### Using a specific Gradle executable
The action will first look for a Gradle wrapper script in the root directory of your project.
If not found, `gradle` will be executed from the PATH.
Use the `gradle-executable` input to execute using a specific Gradle installation.
2019-09-21 16:01:53 +02:00
```yaml
2021-09-28 11:30:55 -06:00
- uses: gradle/gradle-build-action@v2
2019-09-21 16:01:53 +02:00
with:
2021-12-08 13:33:39 -07:00
arguments: build
2021-09-28 11:30:55 -06:00
gradle-executable: /path/to/installed/gradle
2019-09-21 16:01:53 +02:00
```
2021-09-28 11:30:55 -06:00
This mechanism can also be used to target a Gradle wrapper script that is located in a non-default location.
2021-07-07 12:45:24 -06:00
2020-06-15 16:23:01 +02:00
## Caching
2021-09-28 11:30:55 -06:00
By default, this action aims to cache any and all reusable state that may be speed up a subsequent build invocation.
2020-06-15 16:23:01 +02:00
2021-09-28 11:30:55 -06:00
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 ;
2022-06-03 10:45:51 -06:00
- Any [configuration-cache ](https://docs.gradle.org/nightly/userguide/configuration_cache.html ) data stored in the project `.gradle` directory. (Only supported for Gradle 7 or higher.)
2020-06-15 16:23:01 +02:00
2021-09-28 11:30:55 -06:00
To reduce the space required for caching, this action makes a best effort to reduce duplication in cache entries.
2020-06-15 16:23:01 +02:00
2021-09-28 11:30:55 -06:00
Caching is enabled by default. You can disable caching for the action as follows:
2020-06-15 16:23:01 +02:00
```yaml
2021-09-28 11:30:55 -06:00
cache-disabled: true
2020-06-15 16:23:01 +02:00
```
2021-09-28 11:30:55 -06:00
### Cache keys
2020-06-15 16:23:01 +02:00
2022-04-06 11:30:02 -06:00
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.
2020-06-15 16:23:01 +02:00
2021-09-28 11:30:55 -06:00
The state of the Gradle User Home and configuration-cache are 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.
2020-06-15 16:23:01 +02:00
2021-08-17 16:15:28 -06:00
### Using the caches read-only
2022-06-06 12:34:02 -06:00
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.
2021-08-17 16:15:28 -06:00
2022-06-06 12:34:02 -06:00
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:
2021-08-17 16:15:28 -06:00
```yaml
2022-06-06 12:34:02 -06:00
# Only write to the cache for builds on the 'main' and 'release' branches. (Default is 'main' only.)
2021-10-27 16:05:07 -06:00
# Builds on other branches will only read existing entries from the cache.
2022-06-06 12:34:02 -06:00
cache-read-only: ${{ github.ref != 'refs/heads/main' & & github.ref != 'refs/heads/release' }}
2021-10-27 16:05:07 -06:00
```
2022-06-22 16:52:00 -06:00
### 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.
2021-10-27 16:05:07 -06:00
### 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: |
2021-10-29 07:29:57 -06:00
caches
notifications
jdks
2021-10-27 16:05:07 -06:00
# Exclude the local build-cache from the directories cached.
gradle-home-cache-excludes: |
2021-10-29 07:29:57 -06:00
caches/build-cache-1
2021-08-17 16:15:28 -06:00
```
2021-10-27 16:05:07 -06:00
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.
2021-11-01 08:17:33 -06:00
### 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.
2022-06-20 20:45:24 -06:00
A report of all cache entries restored and saved is printed to the Job Summary when saving the cache entries.
2021-11-01 08:17:33 -06:00
This report can provide valuable insignt into how much cache space is being used.
2021-10-14 10:39:12 -06:00
2021-10-27 16:05:07 -06:00
It is possible to enable additional debug logging for cache operations. You do via the `GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED` environment variable:
2021-10-14 10:39:12 -06:00
```yaml
env:
2021-10-27 16:05:07 -06:00
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
2021-10-14 10:39:12 -06:00
```
2021-10-14 10:41:45 -06:00
Note that this setting will also prevent certain cache operations from running in parallel, further assisting with debugging.
2021-11-01 08:17:33 -06:00
### Optimizing cache effectiveness
2022-06-29 13:50:03 +02:00
Cache storage space for GitHub actions is limited, and writing new cache entries can trigger the deletion of existing entries.
2021-11-01 08:17:33 -06:00
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.
2022-06-06 12:34:02 -06:00
#### Select branches that should write to the cache
2021-11-01 08:17:33 -06:00
2022-07-11 12:27:54 -06:00
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.
2022-06-06 12:34:02 -06:00
2022-07-11 12:27:54 -06:00
By default, the `gradle-build-action` will only _write_ to the cache for builds run on the default (`master`/`main`) branch.
2022-06-06 12:34:02 -06:00
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.
2021-11-01 08:17:33 -06:00
2022-06-06 12:34:02 -06:00
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.
2021-11-01 08:17:33 -06:00
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 ).
2022-08-29 11:47:27 -06:00
#### 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
```
2022-04-04 15:07:16 -06:00
## 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:
2022-04-15 08:39:00 -04:00
```yaml
2022-04-04 15:07:16 -06:00
jobs:
gradle:
runs-on: ubuntu-latest
steps:
- name: Checkout project sources
2022-06-04 09:26:11 -06:00
uses: actions/checkout@v3
2022-04-04 15:07:16 -06:00
- 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/
```
2020-06-15 16:23:01 +02:00
## Build scans
2019-09-21 16:01:53 +02:00
2021-09-28 11:30:55 -06:00
If your build publishes a [build scan ](https://gradle.com/build-scans/ ) the `gradle-build-action` action will:
- Add a notice with the link to the GitHub Actions user interface
2021-12-08 13:33:39 -07:00
- For each step that executes Gradle, adds the link to the published build scan as a Step output named `build-scan-url` .
2019-09-23 12:56:08 +02:00
2021-09-28 11:30:55 -06:00
You can then use that link in subsequent actions of your workflow. For example:
2019-09-23 12:56:08 +02:00
```yaml
2019-09-23 13:00:12 +02:00
# .github/workflows/gradle-build-pr.yml
2019-09-23 12:56:08 +02:00
name: Run Gradle on PRs
2020-04-18 17:10:08 +02:00
on: pull_request
2019-09-23 12:56:08 +02:00
jobs:
gradle:
2021-09-28 11:30:55 -06:00
runs-on: ubuntu-latest
2019-09-23 12:56:08 +02:00
steps:
2022-04-04 15:12:03 -06:00
- name: Checkout project sources
2022-06-04 09:26:11 -06:00
uses: actions/checkout@v3
2022-04-04 15:12:03 -06:00
- 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"
2022-01-16 23:45:15 +01:00
uses: actions/github-script@v5
2020-10-14 15:52:50 +02:00
if: github.event_name == 'pull_request' & & failure()
2019-09-23 12:56:08 +02:00
with:
2020-10-14 15:52:50 +02:00
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
2022-01-16 23:45:15 +01:00
github.rest.issues.createComment({
2020-10-14 15:52:50 +02:00
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '❌ ${{ github.workflow }} failed: ${{ steps.gradle.outputs.build-scan-url }}'
})
2019-09-23 12:56:08 +02:00
```
2022-06-20 20:45:24 -06:00
2022-11-28 00:22:26 +09:00
Note that the build scan capturing utilizes the [Initialization Script ](https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script )
in the `USER_HOME/.gradle/init.d/` directory, with the file named `build-result-capture.init.gradle` .
So, if you are using the 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 ),
make sure that its file names have earlier alphabetical order to the `build-result-capture.init.gradle` ,
since configuring capture requires Gradle Enterprise Gradle Plugin to be applied already.
2022-06-20 20:45:24 -06:00
## Support for GitHub Enterprise Server (GHES)
2022-07-25 17:45:58 -06:00
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:
- Build scan links are captured and displayed in the GitHub Actions UI
- Easily run your build with different versions of Gradle
- Save/restore of Gradle User Home (requires GHES v3.5+ : GitHub Actions cache was introduced in GHES 3.5)
2023-02-06 12:45:48 -07:00
- Support for GitHub Actions Job Summary (requires GHES 3.6+ : GitHub Actions Job Summary support was introduced in GHES 3.6). In earlier versions of GHES the build-results summary and caching report will be written to the workflow log, as part of the post-action step.