Skip to content

Commit

Permalink
add cache-local-storage-path
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Feb 8, 2024
1 parent 3701194 commit e6dcdaa
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 38 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

jobs:
test-setup-cache:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -39,3 +42,39 @@ jobs:
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}

test-setup-cache-local:
runs-on: oracle-aarch64
steps:
- uses: actions/checkout@v4
- name: Setup with cache
uses: ./
with:
version: '0.11.0'
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
cache-local-storage-path: /tmp/rye-cache
- run: rye sync
working-directory: __tests__/fixtures/rye-project
test-restore-cache-local:
runs-on: oracle-aarch64
needs: test-setup-cache-local
steps:
- uses: actions/checkout@v4
- name: Restore with cache
id: restore
uses: ./
with:
version: '0.11.0'
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
cache-local-storage-path: /tmp/rye-cache
- name: Cache was hit
run: |
if [ "$CACHE_HIT" != "true" ]; then
exit 1
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

jobs:
build:
runs-on: ${{ matrix.os }}
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ relative to the repository root. This is useful for monorepos.
working-directory: 'path/to/rye/project'
```

#### Local storage path

If you want to save the cache to a local path you can specify the path with the `cache-local-storage-path` input.
This can be useful if you are on a self hosted runner and want to save time and network traffic.

```yaml
- name: Enable caching and define a custom cache path
uses: eifinger/setup-rye@v1
with:
enable-cache: true
cache-local-storage-path: '/path/to/cache'
```

## How it works

This action downloads rye from the releases of the [rye repo](https://github.com/mitsuhiko/rye) and uses the [GitHub Actions Toolkit](https://github.com/actions/toolkit) to cache it as a tool to speed up consecutive runs especially on self-hosted runners.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
cache-prefix:
description: 'Prefix for the cache key'
required: false
cache-local-storage-path:
description: 'Local path to store the cache. Will be used instead of the GitHub Actions cache'
required: false
outputs:
rye-version:
description: "The installed rye version. Useful when using latest."
Expand Down
54 changes: 38 additions & 16 deletions dist/save-cache/index.js

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

2 changes: 1 addition & 1 deletion dist/save-cache/index.js.map

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions dist/setup/index.js

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

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setup-rye",
"version": "1.4.0",
"version": "1.11.0",
"private": true,
"description": "TypeScript template action",
"main": "dist/index.js",
Expand Down
23 changes: 21 additions & 2 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as cache from '@actions/cache'
import * as glob from '@actions/glob'
import * as core from '@actions/core'
import {cp} from '@actions/io/'
import {exists} from '@actions/io/lib/io-util'

import {getLinuxInfo} from './utils'

Expand All @@ -9,6 +11,8 @@ export const CACHE_MATCHED_KEY = 'cache-matched-key'
const CACHE_DEPENDENCY_PATH = 'requirements**.lock'
const workingDir = `/${core.getInput('working-directory')}` || ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
const cacheDependencyPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/${CACHE_DEPENDENCY_PATH}`

export async function restoreCache(
Expand All @@ -24,7 +28,9 @@ export async function restoreCache(

let matchedKey: string | undefined
try {
matchedKey = await cache.restoreCache([cachePath], primaryKey, [restoreKey])
matchedKey = cacheLocalStoragePath
? await restoreCacheLocal(primaryKey)
: await cache.restoreCache([cachePath], primaryKey, [restoreKey])
} catch (err) {
const message = (err as Error).message
core.warning(message)
Expand Down Expand Up @@ -59,7 +65,20 @@ function handleMatchResult(
core.saveState(CACHE_MATCHED_KEY, matchedKey)
core.info(`Cache restored from key: ${matchedKey}`)
} else {
core.info(`cache is not found`)
core.info(`Cache is not found`)
}
core.setOutput('cache-hit', matchedKey === primaryKey)
}

async function restoreCacheLocal(primaryKey: string): Promise<string> {
const storedCache = `${cacheLocalStoragePath}/${primaryKey}`
if (!(await exists(cacheLocalStoragePath))) {
core.info(`Local cache is not found: ${storedCache}`)
return ''
}
await cp(storedCache, cachePath, {
copySourceDirectory: false,
recursive: true
})
return primaryKey
}
32 changes: 17 additions & 15 deletions src/save-cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import {mkdirP, cp} from '@actions/io/'
import {CACHE_MATCHED_KEY, STATE_CACHE_PRIMARY_KEY} from './restore-cache'

const enableCache = core.getInput('enable-cache') === 'true'
const workingDir = `/${core.getInput('working-directory')}` || ''
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`

export async function run(): Promise<void> {
try {
const enableCache = core.getInput('enable-cache') === 'true'
if (enableCache) {
await saveCache()
}
Expand All @@ -31,22 +34,21 @@ async function saveCache(): Promise<void> {
)
return
}
core.info(`Saving cache path: ${cachePath}`)
cacheLocalStoragePath
? await saveCacheLocal(primaryKey)
: await cache.saveCache([cachePath], primaryKey)

let cacheId = 0

try {
core.info(`Saving cache path: ${cachePath}`)
cacheId = await cache.saveCache([cachePath], primaryKey)
} catch (err) {
const message = (err as Error).message
core.info(`[warning]${message}`)
return
}

if (cacheId === -1) {
return
}
core.info(`Cache saved with the key: ${primaryKey}`)
}

async function saveCacheLocal(primaryKey: string): Promise<void> {
const targetPath = `${cacheLocalStoragePath}/${primaryKey}`
await mkdirP(targetPath)
await cp(cachePath, targetPath, {
copySourceDirectory: false,
recursive: true
})
}

run()

0 comments on commit e6dcdaa

Please sign in to comment.