Skip to content

Commit

Permalink
add working-directory parameter (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger authored Dec 9, 2023
1 parent 325282a commit 5934e34
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 29 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Simluate rye-project
run: cp -r ./__tests__/fixtures/rye-project/* .
- name: Setup with cache
uses: ./
with:
version: '0.11.0'
enable-cache: true
cache-key: ${{ github.run_id }}-${{ github.run_attempt }}
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
- run: rye sync
working-directory: __tests__/fixtures/rye-project
test-restore-cache:
runs-on: ubuntu-latest
needs: test-setup-cache
steps:
- uses: actions/checkout@v4
- name: Simluate rye-project
run: cp -r ./__tests__/fixtures/rye-project/* .
- name: Restore with cache
id: restore
uses: ./
with:
version: '0.11.0'
enable-cache: true
cache-key: ${{ github.run_id }}-${{ github.run_attempt }}
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
- name: Cache was hit
run: |
if [ "$CACHE_HIT" != "true" ]; then
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ You can also specify a specific version of rye
### Validate checksum
You can also specify a checksum to validate the downloaded file.
Checksums of versions 0.12.0 and later are automatically verified by this action.
The sha265 hashes can be found on the [releases page](https://github.com/mitsuhiko/rye/releases)
of the rye repo.
Expand Down Expand Up @@ -60,6 +61,19 @@ You can optionally define a custom cache key prefix.
cache-prefix: 'optional-prefix'
```

#### Working directory

If your rye project is not at the root of the repository you can specify the working directory
relative to the repository root. This is useful for monorepos.

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

## 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
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
enable-cache:
description: 'Enable caching of .venv'
default: 'false'
working-directory:
description: 'The location of the rye project relative to the root of the repository'
required: false
cache-prefix:
description: 'Prefix for the cache key'
required: false
Expand All @@ -20,7 +23,7 @@ outputs:
cache-hit:
description: "A boolean value to indicate a cache entry was found"
runs:
using: 'node16'
using: 'node20'
main: 'dist/setup/index.js'
post: 'dist/save-cache/index.js'
post-if: success()
Expand Down
19 changes: 12 additions & 7 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.

15 changes: 9 additions & 6 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.

15 changes: 9 additions & 6 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {getLinuxInfo} from './utils'

export const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key'
export const CACHE_MATCHED_KEY = 'cache-matched-key'
const CACHE_DEPENDENCY_PATH = '**/requirements**.lock'
const cachePath = `${process.env['GITHUB_WORKSPACE']}/.venv`
const CACHE_DEPENDENCY_PATH = 'requirements**.lock'
const workingDir = `/${core.getInput('working-directory')}` || ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
const cacheDependencyPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/${CACHE_DEPENDENCY_PATH}`

export async function restoreCache(
cachePrefix: string,
Expand All @@ -16,7 +18,7 @@ export async function restoreCache(
const {primaryKey, restoreKey} = await computeKeys(cachePrefix, version)
if (primaryKey.endsWith('-')) {
throw new Error(
`No file in ${process.cwd()} matched to [${CACHE_DEPENDENCY_PATH}], make sure you have checked out the target repository`
`No file in ${process.cwd()} matched to [${cacheDependencyPath}], make sure you have checked out the target repository`
)
}

Expand All @@ -39,12 +41,13 @@ async function computeKeys(
cachePrefix: string,
version: string
): Promise<{primaryKey: string; restoreKey: string}> {
const hash = await glob.hashFiles(CACHE_DEPENDENCY_PATH)
core.debug(`Computing cache key for ${cacheDependencyPath}`)
const hash = await glob.hashFiles(cacheDependencyPath)
let primaryKey = ''
let restoreKey = ''
const osInfo = await getLinuxInfo()
primaryKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-venv-${hash}`
restoreKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-venv`
primaryKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-${workingDir}-${hash}`
restoreKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}-${workingDir}`
return {primaryKey, restoreKey}
}

Expand Down
4 changes: 3 additions & 1 deletion src/save-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as cache from '@actions/cache'
import * as core from '@actions/core'
import {CACHE_MATCHED_KEY, STATE_CACHE_PRIMARY_KEY} from './restore-cache'

const cachePath = `${process.env['GITHUB_WORKSPACE']}/.venv`
const workingDir = `/${core.getInput('working-directory')}` || ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`

export async function run(): Promise<void> {
try {
Expand Down Expand Up @@ -34,6 +35,7 @@ async function saveCache(): Promise<void> {
let cacheId = 0

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

0 comments on commit 5934e34

Please sign in to comment.