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

feat: custom key for cache #182

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: custom key for cache
  • Loading branch information
wtto00 committed Oct 2, 2023
commit 2c5e58b94955a6504bafdc06d9a9b32dedbff4b0
38 changes: 35 additions & 3 deletions .github/workflows/test.yml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is formatted by prettier

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks reformat!

Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
@@ -116,3 +116,35 @@ jobs:

- run: |
sdkmanager --install "system-images;android-31;default;x86_64"

test_custom_cache_key:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

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.

@@ -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
@@ -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
@@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# 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
@@ -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
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -20,6 +20,10 @@ inputs:
required: false
description: 'disabled cache'
default: 'false'
cache-key:
required: false
description: 'cache key'
default: ''
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

generate-job-summary:
required: false
description: 'display job summary'
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",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍 👍 👍 👍 👍

"description": "setup-android for self hosted runner",
"main": "dist/setup/index.js",
"scripts": {
"postinstall": "patch-package",
"build": "tsc",
"format": "prettier --write '**/*.ts'",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding quotes, cannot be executed on Windows.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍 👍 👍

"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"
22 changes: 14 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
Loading