Skip to content

Commit

Permalink
Only save cache when run succeeded
Browse files Browse the repository at this point in the history
Commit 9dddc34 disabled this with famous last words "there should be no errors because of this". This is wrong as a run can have some misconfigured or failing steps that create a partial or wrong .venv. This is then uploaded to the cache and each subsequent run finds that corrupted .venv in the cache and does not attempt create the full/correct version.
The initial reason for always running the post run step was to clean up the .rye home folder. I later learned that the only thing we need to clean up is the config.toml file. Upon first install of the .rye folder (for self-hosted runners. ephemeral runners always install) a backup of the clean config.toml file is created with the name config.toml.bak. If this file exists on subsequent it will be used to overwrite a possible changed config.toml. This only works for version 0.19.0 and later.
  • Loading branch information
eifinger committed Jul 4, 2024
1 parent d5ce775 commit 062a31d
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 63 deletions.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ runs:
using: 'node20'
main: 'dist/setup/index.js'
post: 'dist/save-cache/index.js'
post-if: success()
branding:
icon: 'package'
color: 'blue'
11 changes: 1 addition & 10 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.

40 changes: 28 additions & 12 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.

3 changes: 1 addition & 2 deletions dist/update-default-version/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/update-default-version/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": "3.0.1",
"version": "4.0.0",
"private": true,
"description": "TypeScript template action",
"main": "dist/index.js",
Expand Down
14 changes: 0 additions & 14 deletions src/save-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import {
STATE_CACHE_KEY,
venvPath
} from './restore-cache'
import {
RYE_CONFIG_TOML,
RYE_CONFIG_TOML_BACKUP,
STATE_TOOL_CACHED_PATH
} from './utils'

const enableCache = core.getInput('enable-cache') === 'true'
const cacheLocalStoragePath =
Expand All @@ -26,15 +21,6 @@ export async function run(): Promise<void> {
const err = error as Error
core.setFailed(err.message)
}
const cachedPath = core.getState(STATE_TOOL_CACHED_PATH)
await io.rmRF(`${cachedPath}/${RYE_CONFIG_TOML}`)
if (fs.existsSync(`${cachedPath}/${RYE_CONFIG_TOML_BACKUP}`)) {
await io.mv(
`${cachedPath}/${RYE_CONFIG_TOML_BACKUP}`,
`${cachedPath}/${RYE_CONFIG_TOML}`
)
core.info(`Restored ${cachedPath}/${RYE_CONFIG_TOML}`)
}
}

async function saveCache(): Promise<void> {
Expand Down
58 changes: 38 additions & 20 deletions src/setup-rye.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import {
toolsCacheName
} from './utils'
import {downloadLatest} from './download/download-latest'
import {
RYE_CONFIG_TOML,
RYE_CONFIG_TOML_BACKUP,
STATE_TOOL_CACHED_PATH
} from './utils'
import {RYE_CONFIG_TOML, RYE_CONFIG_TOML_BACKUP} from './utils'

async function run(): Promise<void> {
const platform = IS_MAC ? 'macos' : 'linux'
Expand All @@ -43,23 +39,11 @@ async function run(): Promise<void> {
githubToken
)

if (VERSIONS_WHICH_MODIFY_PROFILE.includes(setupResult.version)) {
core.warning(
`Rye version ${setupResult.version} adds a wrong path to the file ~/.profile. Consider using version ${EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT} or later instead.`
)
}
core.setOutput('rye-version', setupResult.version)
setVersion(setupResult.version)
addRyeToPath(setupResult.installedPath)
addMatchers()
core.saveState(STATE_TOOL_CACHED_PATH, setupResult.installedPath)
await io.rmRF(`${setupResult.installedPath}/${RYE_CONFIG_TOML_BACKUP}`)
if (fs.existsSync(`${setupResult.installedPath}/${RYE_CONFIG_TOML}`)) {
await io.cp(
`${setupResult.installedPath}/${RYE_CONFIG_TOML}`,
`${setupResult.installedPath}/${RYE_CONFIG_TOML_BACKUP}`
)
core.info(`Backed up ${setupResult.installedPath}/${RYE_CONFIG_TOML}`)
}
await ensureCleanConfig(setupResult.installedPath)

if (enableCache) {
await restoreCache(cachePrefix, setupResult.version)
}
Expand Down Expand Up @@ -87,6 +71,7 @@ async function setupRye(
installedPath = tryGetFromCache(arch, versionInput)
if (installedPath) {
core.info(`Found Rye in tools-cache for ${versionInput}`)
await createConfigBackup(installedPath)
return {version, installedPath}
}
downloadPath = await downloadVersion(
Expand All @@ -99,6 +84,7 @@ async function setupRye(
}

installedPath = await installRye(downloadPath, arch, version)
await createConfigBackup(installedPath)
return {version, installedPath}
}

Expand Down Expand Up @@ -131,6 +117,38 @@ async function installRye(
return cachedPath
}

async function createConfigBackup(installedPath: string): Promise<void> {
if (fs.existsSync(`${installedPath}/${RYE_CONFIG_TOML}`)) {
await io.cp(
`${installedPath}/${RYE_CONFIG_TOML}`,
`${installedPath}/${RYE_CONFIG_TOML_BACKUP}`
)
core.info(`Backed up ${installedPath}/${RYE_CONFIG_TOML}`)
}
}

async function ensureCleanConfig(installedPath: string): Promise<void> {
if (fs.existsSync(`${installedPath}/${RYE_CONFIG_TOML_BACKUP}`)) {
await io.rmRF(`${installedPath}/${RYE_CONFIG_TOML}`)
await io.cp(
`${installedPath}/${RYE_CONFIG_TOML_BACKUP}`,
`${installedPath}/${RYE_CONFIG_TOML}`
)
core.info(
`Restored clean ${RYE_CONFIG_TOML} from ${installedPath}/${RYE_CONFIG_TOML_BACKUP}`
)
}
}

function setVersion(version: string): void {
if (VERSIONS_WHICH_MODIFY_PROFILE.includes(version)) {
core.warning(
`Rye version ${version} adds a wrong path to the file ~/.profile. Consider using version ${EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT} or later instead.`
)
}
core.setOutput('rye-version', version)
}

function addRyeToPath(cachedPath: string): void {
core.addPath(`${cachedPath}/shims`)
core.info(`Added ${cachedPath}/shims to the path`)
Expand Down
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const OWNER = 'astral-sh'
export const toolsCacheName = 'setup-rye-2024-03-04' // Custom name for cache busting
export const RYE_CONFIG_TOML_BACKUP = 'config.toml.bak'
export const RYE_CONFIG_TOML = 'config.toml'
export const STATE_TOOL_CACHED_PATH = 'tool-cached-path'

export const EARLIEST_VERSION_WITH_NO_MODIFY_PATHSUPPORT = '0.25.0'
export const VERSIONS_WHICH_MODIFY_PROFILE = [
Expand Down

0 comments on commit 062a31d

Please sign in to comment.