Skip to content

Commit

Permalink
wip localcache
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Feb 7, 2024
1 parent 3701194 commit b168d80
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 21 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,40 @@ jobs:
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}

test-setup-cache-local:
runs-on: oracle-aarch64
steps:
- uses: actions/checkout@v4
- run: mkdir -p /tmp/rye-cache
- 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-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-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 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
61 changes: 53 additions & 8 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.

21 changes: 20 additions & 1 deletion 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
21 changes: 20 additions & 1 deletion 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 @@ -63,3 +69,16 @@ function handleMatchResult(
}
core.setOutput('cache-hit', matchedKey === primaryKey)
}

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

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> {
Expand Down Expand Up @@ -31,22 +35,41 @@ async function saveCache(): Promise<void> {
)
return
}
core.info(`Saving cache path: ${cachePath}`)
const cacheId = cacheLocalStoragePath
? await saveCacheLocal(primaryKey)
: await saveCacheGitHub(primaryKey)

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

async function saveCacheLocal(primaryKey: string): Promise<number> {
const cacheId = -1
if (!(await exists(cacheLocalStoragePath))) {
core.error(`Local cache path not found: ${cacheLocalStoragePath}`)
return cacheId
}
const targetPath = `${cacheLocalStoragePath}/${primaryKey}`
await mkdirP(targetPath)
await cp(cachePath, targetPath, {
copySourceDirectory: false,
recursive: true
})
return 1
}

async function saveCacheGitHub(primaryKey: string): Promise<number> {
let cacheId = -1
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}`)
return cacheId
}

run()

0 comments on commit b168d80

Please sign in to comment.