2021-08-07 22:17:27 +00:00
# Execute Gradle builds in GitHub Actions workflows
2019-09-20 21:06:59 +00:00
2021-08-07 22:17:27 +00:00
This GitHub Action can be used to execute a Gradle build on any platform supported by GitHub Actions.
2019-09-20 21:06:59 +00:00
2019-09-21 14:01:53 +00:00
## Usage
2019-09-20 21:06:59 +00:00
2021-08-07 22:17:27 +00:00
The following workflow will run `./gradlew build` using the wrapper from the repository on ubuntu, macos and windows. The only prerequisite is to have Java installed: you define the version of Java you need to run the build using the `actions/setup-java` action.
2019-09-23 10:10:22 +00:00
2019-09-21 14:01:53 +00:00
```yaml
2019-09-23 11:00:12 +00:00
# .github/workflows/gradle-build-pr.yml
2019-09-21 14:01:53 +00:00
name: Run Gradle on PRs
2020-04-14 07:08:39 +00:00
on: pull_request
2019-09-21 14:01:53 +00:00
jobs:
gradle:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
2020-06-14 10:57:10 +00:00
- uses: actions/checkout@v2
2019-09-21 14:01:53 +00:00
- uses: actions/setup-java@v1
with:
java-version: 11
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
with:
arguments: build
```
## Gradle arguments
The `arguments` input can used to pass arbitrary arguments to the `gradle` command line.
Here are some valid examples:
```yaml
arguments: build
arguments: check --scan
arguments: some arbitrary tasks
arguments: build -PgradleProperty=foo
arguments: build -DsystemProperty=bar
....
```
See `gradle --help` for more information.
2020-02-14 05:46:51 +00:00
If you need to pass environment variables, simply use the GitHub Actions workflow syntax:
2019-09-21 14:01:53 +00:00
```yaml
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
env:
CI: true
```
## Run a build from a different directory
```yaml
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
with:
build-root-directory: some/subdirectory
```
## Use a specific `gradle` executable
```yaml
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
with:
gradle-executable: path/to/gradle
```
2021-07-07 18:45:24 +00:00
## Use a Gradle wrapper from a different directory
```yaml
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2021-07-07 18:45:24 +00:00
with:
gradle-executable: path/to/gradlew
```
NOTE: The `wrapper-directory` input has been deprecated. Use `gradle-executable` instead.
2019-09-21 14:04:03 +00:00
## Setup and use a declared Gradle version
2019-09-21 14:01:53 +00:00
```yaml
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
with:
2020-06-14 10:57:10 +00:00
gradle-version: 6.5
2019-09-21 14:01:53 +00:00
```
`gradle-version` can be set to any valid Gradle version.
Moreover, you can use the following aliases:
| Alias | Selects |
| --- |---|
2021-06-24 22:59:03 +00:00
| `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. |
2019-09-21 14:01:53 +00:00
2019-09-23 10:10:22 +00:00
This can be handy to, for example, automatically test your build with the next Gradle version once a release candidate is out:
2019-09-21 14:01:53 +00:00
```yaml
2019-09-23 11:00:12 +00:00
# .github/workflows/test-gradle-rc.yml
2019-09-21 14:01:53 +00:00
name: Test latest Gradle RC
on:
schedule:
- cron: 0 0 * * * # daily
jobs:
gradle-rc:
runs-on: ubuntu-latest
steps:
2020-06-14 10:57:10 +00:00
- uses: actions/checkout@v2
2019-09-21 14:01:53 +00:00
- uses: actions/setup-java@v1
with:
java-version: 11
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-21 14:01:53 +00:00
with:
2021-06-24 22:59:03 +00:00
gradle-version: release-candidate
2019-09-21 14:01:53 +00:00
arguments: build --dry-run # just test build configuration
```
2020-06-15 14:23:01 +00:00
## Caching
This action provides 3 levels of caching to help speed up your GitHub Actions:
2021-07-08 18:22:48 +00:00
- `distributions` caches any downloaded Gradle zips, including any downloaded [wrapper ](https://docs.gradle.org/current/userguide/gradle_wrapper.html ) versions, saving time downloading Gradle distributions ;
2020-06-15 14:23:01 +00:00
- `dependencies` caches the [dependencies ](https://docs.gradle.org/current/userguide/dependency_resolution.html#sub:cache_copy ), saving time downloading dependencies ;
- `configuration` caches the [build configuration ](https://docs.gradle.org/nightly/userguide/configuration_cache.html ), saving time configuring the build.
2021-07-08 18:22:48 +00:00
Only the first one, caching downloaded distributions, is enabled by default.
2020-06-15 14:23:01 +00:00
Future versions of this action will enable all caching by default.
You can control which level is enabled as follows:
```yaml
2021-07-08 18:22:48 +00:00
distributions-cache-enabled: true
2020-06-15 14:23:01 +00:00
dependencies-cache-enabled: true
configuration-cache-enabled: true
```
2021-07-08 18:22:48 +00:00
NOTE: The `wrapper-cache-enabled` flag has been deprecated, replaced by `distributions-cache-enabled` which enables caching for all downloaded distributions, including Gradle wrapper downloads.
The distributions cache is simple and can't be configured further.
2020-06-15 14:23:01 +00:00
The dependencies and configuration cache will compute a cache key in a best effort manner.
Keep reading to learn how to better control how they work.
2021-08-07 22:33:18 +00:00
Note that enabling configuration cache without thee dependencies cache is not permitted, since a hit in the configuration cache assumes that dependencies are already present in the local dependencies cache.
2020-06-15 14:23:01 +00:00
### Configuring the dependencies and configuration caches
Both the dependencies and configuration caches use the same default configuration:
They use the following inputs to calculate the cache key:
2020-06-15 14:50:15 +00:00
```text
**/*.gradle
**/*.gradle.kts
**/gradle.properties
gradle/**
2020-06-15 14:23:01 +00:00
```
2020-06-15 14:50:15 +00:00
This is a good enough approximation.
2020-06-15 14:23:01 +00:00
They restore cached state even if there isn't an exact match.
If the defaults don't suit your needs you can override them with the following inputs:
```yaml
dependencies-cache-key: |
** /gradle.properties
2020-09-22 17:41:29 +00:00
gradle/dependency-locks/**
2020-06-15 14:23:01 +00:00
dependencies-cache-exact: true
configuration-cache-key: |
** /gradle.properties
2020-09-22 17:41:29 +00:00
gradle/dependency-locks/**
2020-06-15 14:23:01 +00:00
configuration-cache-exact: true
```
Coming up with a good cache key isn't trivial and depends on your build.
The above example isn't realistic.
Stick to the defaults unless you know what you are doing.
If you happen to use Gradle [dependency locking ](https://docs.gradle.org/current/userguide/dependency_locking.html ) you can make the dependencies cache more precise with the following configuration:
```yaml
dependencies-cache-enabled: true
2020-09-22 17:41:29 +00:00
dependencies-cache-key: gradle/dependency-locks/**
2020-06-15 14:23:01 +00:00
dependencies-cache-exact: true
```
2021-08-17 22:15:28 +00:00
### Using the caches read-only
Cache storage space is limited for GitHub actions, and writing new cache entries can trigger the deletion of exising entries.
In some circumstances, it makes sense for a Gradle invocation to use any existing cache entries but not to write and changes back.
For example, you may want to write cache entries for builds on your `main` branch, but not for any PR build invocations.
Use the following configuration to avoid writing cache entries for the action invocation:
```yaml
cache-read-only: true
```
2020-06-15 14:23:01 +00:00
## Build scans
2019-09-21 14:01:53 +00:00
2021-08-07 22:17:27 +00:00
If your build publishes a [build scan ](https://gradle.com/build-scans/ ) the `gradle-build-action` action will emit the link to the published build scan as an output named `build-scan-url` .
2019-09-21 14:01:53 +00:00
You can then use that link in subsequent actions of your workflow.
2019-09-23 10:56:08 +00:00
For example:
```yaml
2019-09-23 11:00:12 +00:00
# .github/workflows/gradle-build-pr.yml
2019-09-23 10:56:08 +00:00
name: Run Gradle on PRs
2020-04-18 15:10:08 +00:00
on: pull_request
2019-09-23 10:56:08 +00:00
jobs:
gradle:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
2020-06-14 10:57:10 +00:00
- uses: actions/checkout@v2
2019-09-23 10:56:08 +00:00
- uses: actions/setup-java@v1
with:
java-version: 11
2021-08-07 22:17:27 +00:00
- uses: gradle/gradle-build-action@v1
2019-09-23 10:56:08 +00:00
with:
arguments: build
id: gradle
2020-10-14 13:52:50 +00:00
- name: "Comment build scan url"
uses: actions/github-script@v3
if: github.event_name == 'pull_request' & & failure()
2019-09-23 10:56:08 +00:00
with:
2020-10-14 13:52:50 +00:00
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.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 }}'
})
2019-09-23 10:56:08 +00:00
```