Skip to content

Commit

Permalink
Merge pull request #182 from wtto00/cache-key
Browse files Browse the repository at this point in the history
feat: custom key for cache
  • Loading branch information
amyu authored Oct 2, 2023
2 parents ff814bd + 2c5e58b commit 2fdb21d
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 50 deletions.
38 changes: 35 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
os: [ubuntu-latest, windows-latest, macos-latest]
sdk: [32, 33]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}-${{ matrix.sdk }}-test
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
os: [ubuntu-latest, windows-latest, macos-latest]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}-test_not_use_cache
cancel-in-progress: true
Expand Down Expand Up @@ -91,7 +91,7 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v4
Expand All @@ -116,3 +116,35 @@ jobs:

- run: |
sdkmanager --install "system-images;android-31;default;x86_64"
test_custom_cache_key:
name: run test custom cache key
runs-on: ${{ matrix.os }}
timeout-minutes: 20
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v4

- name: remove android sdk from ubuntu-latest
shell: bash
run: |
echo 'ANDROID_HOME=' >> $GITHUB_ENV
echo 'ANDROID_SDK_ROOT=' >> $GITHUB_ENV
rm -rf ~/android
- name: Setup JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: temurin

- name: Setup Android SDK
uses: ./
with:
cache-key: 'default-version'

- run: |
sdkmanager --install emulator
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# setup-android

This action provides the following functionality for GitHub Actions users:
This action provides the following functionality for GitHub Actions users:

- Optionally downloading and caching distribution of the requested sdk version or build tools version or ndk,cmake version, and adding it to the PATH
- Runs on Mac, Linux and Windows powered by SelfHostedRunner

# Motivation

This Action is provided for SelfHostedRunner.
GithubHostedRunner does not need this Action as it already has the SDK set up.

Expand All @@ -14,6 +15,7 @@ GithubHostedRunner does not need this Action as it already has the SDK set up.
See [action.yml](action.yml)

**Basic:**

```yaml
steps:
- uses: actions/checkout@v4
Expand All @@ -22,14 +24,15 @@ steps:
with:
java-version: 17
distribution: temurin

- name: Setup Android SDK
uses: amyu/setup-android@v3

- run: ./gradlew build --stacktrace
```
**Additional:**
```yaml
steps:
- uses: actions/checkout@v4
Expand All @@ -38,26 +41,30 @@ steps:
with:
java-version: 17
distribution: temurin

- name: Setup Android SDK
uses: amyu/setup-android@v3
with:
# default: false
# Whether to use the cache
# Whether to use the cache
cache-disabled: true


# default: `${sdkVersion}-${buildToolsVersion}-${ndkVersion}-${cmakeVersion}-v3.1`
# Custom key for cache. It is invalid when `cache-disabled: true`
cache-key: 'custom-cache-key'

# default: '33'
# sdk version
# see https://developer.android.com/studio/releases/platforms
# It will always be installed.
sdk-version: '33'

# default: '33.0.2'
# build tools version
# see https://developer.android.com/studio/releases/build-tools
# It will always be installed.
build-tools-version: '33.0.2'

# default: ''
# cmake version
# see https://developer.android.com/studio/projects/install-ndk
Expand All @@ -71,7 +78,7 @@ steps:
ndk-version: '23.1.7779620'

# default: true
# Whether to generate or not the job summary
# Whether to generate or not the job summary
generate-job-summary: false

- run: ./gradlew build --stacktrace
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
required: false
description: 'disabled cache'
default: 'false'
cache-key:
required: false
description: 'cache key'
default: ''
generate-job-summary:
required: false
description: 'display job summary'
Expand Down
25 changes: 15 additions & 10 deletions dist/cleanup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 18 additions & 13 deletions dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "setup-android",
"version": "2",
"version": "3",
"description": "setup-android for self hosted runner",
"main": "dist/setup/index.js",
"scripts": {
"postinstall": "patch-package",
"build": "tsc",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"package": "ncc build -o dist/setup src/setup-android.ts && ncc build -o dist/cleanup src/cleanup-android.ts",
"all": "npm run postinstall && npm run build && npm run format && npm run lint && npm run package"
Expand Down
22 changes: 14 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ function generateRestoreKey(
sdkVersion: string,
buildToolsVersion: string,
ndkVersion: string,
cmakeVersion: string
cmakeVersion: string,
cacheKey: string
): string {
if (cacheKey) return cacheKey
return `${sdkVersion}-${buildToolsVersion}-${ndkVersion}-${cmakeVersion}-v3.1`
}

export async function restoreCache(
sdkVersion: string,
buildToolsVersion: string,
ndkVersion: string,
cmakeVersion: string
cmakeVersion: string,
cacheKey: string
): Promise<CacheEntry | undefined> {
const restoreKey = generateRestoreKey(
sdkVersion,
buildToolsVersion,
ndkVersion,
cmakeVersion
cmakeVersion,
cacheKey
)

const restoredEntry = await cache.restoreCache([ANDROID_HOME_DIR], restoreKey)
if (restoredEntry) {
core.info(`Found in cache`)
core.info(`Found in cache: ${restoredEntry}`)
} else {
core.info(`Not Found cache`)
core.info(`Not Found cache: ${restoredEntry}`)
}
core.saveState(RESTORED_ENTRY_STATE_KEY, restoredEntry)
return Promise.resolve(restoredEntry)
Expand All @@ -41,16 +45,18 @@ export async function saveCache(
sdkVersion: string,
buildToolsVersion: string,
ndkVersion: string,
cmakeVersion: string
cmakeVersion: string,
cacheKey: string
): Promise<CacheEntry | undefined> {
const restoreKey = generateRestoreKey(
sdkVersion,
buildToolsVersion,
ndkVersion,
cmakeVersion
cmakeVersion,
cacheKey
)

core.info(`caching ...`)
core.info(`caching "${restoreKey}" ...`)
try {
const savedEntry = await cache.saveCache([ANDROID_HOME_DIR], restoreKey)
return Promise.resolve(savedEntry)
Expand Down
Loading

0 comments on commit 2fdb21d

Please sign in to comment.