|
| 1 | +import { info, warning } from '@actions/core'; |
| 2 | +import { createHash } from 'crypto'; |
| 3 | +import { existsSync, readFileSync } from 'fs'; |
| 4 | +import { Cargo } from '../rust/cargo'; |
| 5 | +import { restoreFromCache, saveToCache } from './cache-impl'; |
| 6 | + |
| 7 | +export interface BuildCacheStrategy { |
| 8 | + restore(): Promise<void>; |
| 9 | + save(): Promise<void>; |
| 10 | +} |
| 11 | + |
| 12 | +export function buildCacheStrategy( |
| 13 | + projectDir: string, |
| 14 | + strategy: string, |
| 15 | + toolchains: string[], |
| 16 | + fallbackBranch: string, |
| 17 | +): BuildCacheStrategy | undefined { |
| 18 | + switch (strategy) { |
| 19 | + case 'github': |
| 20 | + return new GithubBuildCacheStrategy( |
| 21 | + projectDir, |
| 22 | + toolchains, |
| 23 | + fallbackBranch, |
| 24 | + ); |
| 25 | + case 'none': |
| 26 | + return undefined; |
| 27 | + default: |
| 28 | + warning(`Unknown build cache strategy: ${strategy}`); |
| 29 | + return undefined; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export class GithubBuildCacheStrategy implements BuildCacheStrategy { |
| 34 | + private cacheKey: string; |
| 35 | + private fallbackKey: string; |
| 36 | + private projectDir: string; |
| 37 | + private fallbackBranch: string; |
| 38 | + |
| 39 | + private restoredFrom: string | undefined; |
| 40 | + |
| 41 | + constructor( |
| 42 | + projectDir: string, |
| 43 | + toolchains: string[], |
| 44 | + fallbackBranch: string, |
| 45 | + ) { |
| 46 | + this.projectDir = projectDir; |
| 47 | + this.fallbackBranch = fallbackBranch; |
| 48 | + this.cacheKey = GithubBuildCacheStrategy.buildCacheKey( |
| 49 | + projectDir, |
| 50 | + toolchains, |
| 51 | + fallbackBranch, |
| 52 | + ); |
| 53 | + this.fallbackKey = GithubBuildCacheStrategy.buildFallbackCacheKey( |
| 54 | + projectDir, |
| 55 | + toolchains, |
| 56 | + fallbackBranch, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + async restore(): Promise<void> { |
| 61 | + this.restoredFrom = await GithubBuildCacheStrategy.restoreBuildCache( |
| 62 | + this.projectDir, |
| 63 | + this.cacheKey, |
| 64 | + this.fallbackKey, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + async save(): Promise<void> { |
| 69 | + const currentBranch = process.env.GITHUB_REF_NAME || 'not-in-gh-action'; |
| 70 | + |
| 71 | + // Always save if this is the fallback branch |
| 72 | + // Skip save if no dependencies changed |
| 73 | + if ( |
| 74 | + currentBranch !== this.fallbackBranch && |
| 75 | + this.restoredFrom === this.cacheKey |
| 76 | + ) { |
| 77 | + info(`Build cache dependencies unchanged, skipping save.`); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + await GithubBuildCacheStrategy.saveBuildCache( |
| 82 | + this.projectDir, |
| 83 | + this.cacheKey, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + static async restoreBuildCache( |
| 88 | + projectDir: string, |
| 89 | + cacheKey: string, |
| 90 | + fallbackKey: string, |
| 91 | + ): Promise<string | undefined> { |
| 92 | + const targetDir = Cargo.targetDir(projectDir); |
| 93 | + |
| 94 | + const restoredKey = await restoreFromCache([targetDir], cacheKey, [ |
| 95 | + fallbackKey, |
| 96 | + ]); |
| 97 | + |
| 98 | + if (restoredKey) { |
| 99 | + console.info(`Restored build cache from key: ${restoredKey}`); |
| 100 | + } else { |
| 101 | + console.info( |
| 102 | + `No build cache found for keys: ${cacheKey}, ${fallbackKey}`, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return restoredKey; |
| 107 | + } |
| 108 | + |
| 109 | + static async saveBuildCache(projectDir: string, cacheKey: string) { |
| 110 | + const targetDir = Cargo.targetDir(projectDir); |
| 111 | + |
| 112 | + if (!existsSync(targetDir)) { |
| 113 | + warning( |
| 114 | + `Target directory does not exist: ${targetDir}, skipping cache save.`, |
| 115 | + ); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + await saveToCache([targetDir], cacheKey); |
| 120 | + info(`Saved build cache with key: ${cacheKey}`); |
| 121 | + } |
| 122 | + |
| 123 | + static buildCacheKey( |
| 124 | + projectDir: string, |
| 125 | + toolchains: string[], |
| 126 | + fallbackBranch: string, |
| 127 | + ): string { |
| 128 | + const lockFile = Cargo.cargoLock(projectDir); |
| 129 | + let lockHashOrBranch = process.env.GITHUB_REF_NAME || 'not-in-gh-action'; |
| 130 | + |
| 131 | + if (fallbackBranch === lockHashOrBranch) { |
| 132 | + // Use branch name |
| 133 | + } else if (existsSync(lockFile)) { |
| 134 | + const lockContent = readFileSync(lockFile, 'utf8'); |
| 135 | + const hash = createHash('sha256').update(lockContent).digest('hex'); |
| 136 | + lockHashOrBranch = hash.slice(0, 20); |
| 137 | + } |
| 138 | + |
| 139 | + const toolchainHash = createHash('sha256') |
| 140 | + .update(toolchains.sort().join(',')) |
| 141 | + .digest('hex') |
| 142 | + .slice(0, 8); |
| 143 | + |
| 144 | + let normalizedProjectDir = projectDir |
| 145 | + .trim() |
| 146 | + .replace(/^[./\\]+/, '') |
| 147 | + .replace(/[./\\]+$/, '') |
| 148 | + .replace(/^[/\\]+/, ''); |
| 149 | + |
| 150 | + if (!normalizedProjectDir) normalizedProjectDir = 'root'; |
| 151 | + normalizedProjectDir = normalizedProjectDir.replace(/[\\/]+/g, '-'); |
| 152 | + |
| 153 | + const platform = process.platform; |
| 154 | + const arch = process.arch; |
| 155 | + |
| 156 | + return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${normalizedProjectDir}-${lockHashOrBranch}`; |
| 157 | + } |
| 158 | + |
| 159 | + static buildFallbackCacheKey( |
| 160 | + projectDir: string, |
| 161 | + toolchains: string[], |
| 162 | + fallbackBranch: string, |
| 163 | + ): string { |
| 164 | + const platform = process.platform; |
| 165 | + const arch = process.arch; |
| 166 | + const toolchainHash = createHash('sha256') |
| 167 | + .update(toolchains.sort().join(',')) |
| 168 | + .digest('hex') |
| 169 | + .slice(0, 8); |
| 170 | + |
| 171 | + return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${projectDir}-${fallbackBranch}`; |
| 172 | + } |
| 173 | +} |
0 commit comments