Skip to content

Commit

Permalink
buildx: ghaNoCache opt for download/build to disable binary cache
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Feb 23, 2024
1 parent f1c7619 commit cbc244c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
11 changes: 11 additions & 0 deletions __tests__/buildx/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
16 changes: 10 additions & 6 deletions src/buildx/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand All @@ -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();
Expand All @@ -91,18 +93,20 @@ 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}`);

const installCache = new Cache({
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();
Expand Down
9 changes: 7 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand All @@ -67,14 +70,16 @@ 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`);
htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
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`);
}
Expand Down

0 comments on commit cbc244c

Please sign in to comment.