buildx: ghaNoCache opt for download/build to disable binary cache

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-02-23 09:53:23 +01:00
parent f1c76199be
commit cbc244c2f4
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
3 changed files with 28 additions and 8 deletions

View File

@ -67,6 +67,17 @@ describe('download', () => {
expect(fs.existsSync(toolPath)).toBe(true);
});
// prettier-ignore
test.each([
['v0.11.2'],
['v0.12.0'],
])(
'acquires %p of buildx without cache', async (version) => {
const install = new Install({standalone: false});
const toolPath = await install.download(version, true);
expect(fs.existsSync(toolPath)).toBe(true);
});
// TODO: add tests for arm
// prettier-ignore
test.each([

View File

@ -47,10 +47,11 @@ export class Install {
/*
* Download buildx binary from GitHub release
* @param version semver version or latest
* @param v: version semver version or latest
* @param ghaNoCache: disable binary caching in GitHub Actions cache backend
* @returns path to the buildx binary
*/
public async download(v: string): Promise<string> {
public async download(v: string, ghaNoCache?: boolean): Promise<string> {
const version: DownloadVersion = await Install.getDownloadVersion(v);
core.debug(`Install.download version: ${version.version}`);
@ -69,7 +70,8 @@ export class Install {
htcName: version.key != 'official' ? `buildx-dl-bin-${version.key}` : 'buildx-dl-bin',
htcVersion: vspec,
baseCacheDir: path.join(Buildx.configDir, '.bin'),
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx',
ghaNoCache: ghaNoCache
});
const cacheFoundPath = await installCache.find();
@ -91,10 +93,11 @@ export class Install {
/*
* Build buildx binary from source
* @param gitContext git repo context
* @param gitContext: git repo context
* @param ghaNoCache: disable binary caching in GitHub Actions cache backend
* @returns path to the buildx binary
*/
public async build(gitContext: string): Promise<string> {
public async build(gitContext: string, ghaNoCache?: boolean): Promise<string> {
const vspec = await this.vspec(gitContext);
core.debug(`Install.build vspec: ${vspec}`);
@ -102,7 +105,8 @@ export class Install {
htcName: 'buildx-build-bin',
htcVersion: vspec,
baseCacheDir: path.join(Buildx.configDir, '.bin'),
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx',
ghaNoCache: ghaNoCache
});
const cacheFoundPath = await installCache.find();

View File

@ -27,17 +27,20 @@ export interface CacheOpts {
htcVersion: string;
baseCacheDir: string;
cacheFile: string;
ghaNoCache?: boolean;
}
export class Cache {
private readonly opts: CacheOpts;
private readonly ghaCacheKey: string;
private readonly ghaNoCache?: boolean;
private readonly cacheDir: string;
private readonly cachePath: string;
constructor(opts: CacheOpts) {
this.opts = opts;
this.ghaCacheKey = util.format('%s-%s-%s', this.opts.htcName, this.opts.htcVersion, this.platform());
this.ghaNoCache = this.opts.ghaNoCache;
this.cacheDir = path.join(this.opts.baseCacheDir, this.opts.htcVersion, this.platform());
this.cachePath = path.join(this.cacheDir, this.opts.cacheFile);
if (!fs.existsSync(this.cacheDir)) {
@ -52,7 +55,7 @@ export class Cache {
const htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
core.debug(`Cache.save cached to hosted tool cache ${htcPath}`);
if (cache.isFeatureAvailable()) {
if (!this.ghaNoCache && cache.isFeatureAvailable()) {
core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
await cache.saveCache([this.cacheDir], this.ghaCacheKey);
}
@ -67,7 +70,7 @@ export class Cache {
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
}
if (cache.isFeatureAvailable()) {
if (!this.ghaNoCache && cache.isFeatureAvailable()) {
core.debug(`GitHub Actions cache feature available`);
if (await cache.restoreCache([this.cacheDir], this.ghaCacheKey)) {
core.info(`Restored ${this.ghaCacheKey} from GitHub Actions cache`);
@ -75,6 +78,8 @@ export class Cache {
core.info(`Restored to hosted tool cache ${htcPath}`);
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
}
} else if (this.ghaNoCache) {
core.info(`GitHub Actions cache disabled`);
} else {
core.info(`GitHub Actions cache feature not available`);
}