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

Add SBT in cache managers. #302

Merged
merged 13 commits into from Apr 20, 2022
26 changes: 20 additions & 6 deletions README.md
Expand Up @@ -25,7 +25,7 @@ Inputs `java-version` and `distribution` are mandatory. See [Supported distribut
**Eclipse Temurin**
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'temurin' # See 'Supported distributions' for available options
Expand All @@ -36,7 +36,7 @@ steps:
**Zulu OpenJDK**
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'zulu' # See 'Supported distributions' for available options
Expand Down Expand Up @@ -69,13 +69,14 @@ Currently, the following distributions are supported:
The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle and maven. The format of the used cache key is `setup-java-${{ platform }}-${{ packageManager }}-${{ fileHash }}`, where the hash is based on the following files:
- gradle: `**/*.gradle*`, `**/gradle-wrapper.properties`
- maven: `**/pom.xml`
- sbt: `**/build.sbt`

The cache input is optional, and caching is turned off by default.

#### Caching gradle dependencies
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'temurin'
Expand All @@ -87,7 +88,7 @@ steps:
#### Caching maven dependencies
```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'temurin'
Expand All @@ -97,6 +98,19 @@ steps:
run: mvn -B package --file pom.xml
```

#### Caching sbt dependencies
```yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: '11'
cache: 'sbt'
- name: Build with SBT
run: sbt package
```

### Check latest
In the basic examples above, the `check-latest` flag defaults to `false`. When set to `false`, the action tries to first resolve a version of Java from the local tool cache on the runner. If unable to find a specific version in the cache, the action will download a version of Java. Use the default or set `check-latest` to `false` if you prefer a faster more consistent setup experience that prioritizes trying to use the cached versions at the expense of newer versions sometimes being available for download.

Expand All @@ -107,7 +121,7 @@ For Java distributions that are not cached on Hosted images, `check-latest` alwa

```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'adopt'
Expand All @@ -126,7 +140,7 @@ jobs:
java: [ '8', '11', '13', '15' ]
name: Java ${{ matrix.Java }} sample
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup java
uses: actions/setup-java@v2
with:
Expand Down
41 changes: 41 additions & 0 deletions __tests__/cache.test.ts
Expand Up @@ -118,6 +118,23 @@ describe('dependency cache', () => {
expect(spyInfo).toBeCalledWith('gradle cache is not found');
});
});
describe('for sbt', () => {
it('throws error if no build.sbt found', async () => {
await expect(restore('sbt')).rejects.toThrowError(
`No file in ${projectRoot(
workspace
)} matched to [**/build.sbt], make sure you have checked out the target repository`
);
});
it('downloads cache', async () => {
createFile(join(workspace, 'build.sbt'));

await restore('sbt');
expect(spyCacheRestore).toBeCalled();
expect(spyWarning).not.toBeCalled();
expect(spyInfo).toBeCalledWith('sbt cache is not found');
});
});
});
describe('save', () => {
let spyCacheSave: jest.SpyInstance<
Expand Down Expand Up @@ -194,6 +211,30 @@ describe('dependency cache', () => {
expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
});
});
describe('for sbt', () => {
it('uploads cache even if no build.sbt found', async () => {
createStateForMissingBuildFile();
await save('sbt');
expect(spyCacheSave).toBeCalled();
expect(spyWarning).not.toBeCalled();
});
it('does not upload cache if no restore run before', async () => {
createFile(join(workspace, 'build.sbt'));

await save('sbt');
expect(spyCacheSave).not.toBeCalled();
expect(spyWarning).toBeCalledWith('Error retrieving key from state.');
});
it('uploads cache', async () => {
createFile(join(workspace, 'build.sbt'));
createStateForSuccessfulRestore();

await save('sbt');
expect(spyCacheSave).toBeCalled();
expect(spyWarning).not.toBeCalled();
expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
});
});
});
});

Expand Down
1 change: 1 addition & 0 deletions __tests__/cache/sbt/build.sbt
@@ -0,0 +1 @@
ThisBuild / scalaVersion := "3.1.1"
8 changes: 7 additions & 1 deletion src/cache.ts
Expand Up @@ -13,7 +13,7 @@ const CACHE_MATCHED_KEY = 'cache-matched-key';
const CACHE_KEY_PREFIX = 'setup-java';

interface PackageManager {
id: 'maven' | 'gradle';
id: 'maven' | 'gradle' | 'sbt';
/**
* Paths of the file that specify the files to cache.
*/
Expand All @@ -32,6 +32,12 @@ const supportedPackageManager: PackageManager[] = [
path: [join(os.homedir(), '.gradle', 'caches'), join(os.homedir(), '.gradle', 'wrapper')],
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
pattern: ['**/*.gradle*', '**/gradle-wrapper.properties']
},
{
id: 'sbt',
path: [join(os.homedir(), '.ivy2', 'cache'), join(os.homedir(), '.sbt')],
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#scala---sbt
pattern: ['**/build.sbt']
fmeriaux marked this conversation as resolved.
Show resolved Hide resolved
}
];

Expand Down