Skip to content

Commit cbc244c

Browse files
committed
buildx: ghaNoCache opt for download/build to disable binary cache
Signed-off-by: CrazyMax <[email protected]>
1 parent f1c7619 commit cbc244c

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

__tests__/buildx/install.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ describe('download', () => {
6767
expect(fs.existsSync(toolPath)).toBe(true);
6868
});
6969

70+
// prettier-ignore
71+
test.each([
72+
['v0.11.2'],
73+
['v0.12.0'],
74+
])(
75+
'acquires %p of buildx without cache', async (version) => {
76+
const install = new Install({standalone: false});
77+
const toolPath = await install.download(version, true);
78+
expect(fs.existsSync(toolPath)).toBe(true);
79+
});
80+
7081
// TODO: add tests for arm
7182
// prettier-ignore
7283
test.each([

src/buildx/install.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ export class Install {
4747

4848
/*
4949
* Download buildx binary from GitHub release
50-
* @param version semver version or latest
50+
* @param v: version semver version or latest
51+
* @param ghaNoCache: disable binary caching in GitHub Actions cache backend
5152
* @returns path to the buildx binary
5253
*/
53-
public async download(v: string): Promise<string> {
54+
public async download(v: string, ghaNoCache?: boolean): Promise<string> {
5455
const version: DownloadVersion = await Install.getDownloadVersion(v);
5556
core.debug(`Install.download version: ${version.version}`);
5657

@@ -69,7 +70,8 @@ export class Install {
6970
htcName: version.key != 'official' ? `buildx-dl-bin-${version.key}` : 'buildx-dl-bin',
7071
htcVersion: vspec,
7172
baseCacheDir: path.join(Buildx.configDir, '.bin'),
72-
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'
73+
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx',
74+
ghaNoCache: ghaNoCache
7375
});
7476

7577
const cacheFoundPath = await installCache.find();
@@ -91,18 +93,20 @@ export class Install {
9193

9294
/*
9395
* Build buildx binary from source
94-
* @param gitContext git repo context
96+
* @param gitContext: git repo context
97+
* @param ghaNoCache: disable binary caching in GitHub Actions cache backend
9598
* @returns path to the buildx binary
9699
*/
97-
public async build(gitContext: string): Promise<string> {
100+
public async build(gitContext: string, ghaNoCache?: boolean): Promise<string> {
98101
const vspec = await this.vspec(gitContext);
99102
core.debug(`Install.build vspec: ${vspec}`);
100103

101104
const installCache = new Cache({
102105
htcName: 'buildx-build-bin',
103106
htcVersion: vspec,
104107
baseCacheDir: path.join(Buildx.configDir, '.bin'),
105-
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'
108+
cacheFile: os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx',
109+
ghaNoCache: ghaNoCache
106110
});
107111

108112
const cacheFoundPath = await installCache.find();

src/cache.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ export interface CacheOpts {
2727
htcVersion: string;
2828
baseCacheDir: string;
2929
cacheFile: string;
30+
ghaNoCache?: boolean;
3031
}
3132

3233
export class Cache {
3334
private readonly opts: CacheOpts;
3435
private readonly ghaCacheKey: string;
36+
private readonly ghaNoCache?: boolean;
3537
private readonly cacheDir: string;
3638
private readonly cachePath: string;
3739

3840
constructor(opts: CacheOpts) {
3941
this.opts = opts;
4042
this.ghaCacheKey = util.format('%s-%s-%s', this.opts.htcName, this.opts.htcVersion, this.platform());
43+
this.ghaNoCache = this.opts.ghaNoCache;
4144
this.cacheDir = path.join(this.opts.baseCacheDir, this.opts.htcVersion, this.platform());
4245
this.cachePath = path.join(this.cacheDir, this.opts.cacheFile);
4346
if (!fs.existsSync(this.cacheDir)) {
@@ -52,7 +55,7 @@ export class Cache {
5255
const htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
5356
core.debug(`Cache.save cached to hosted tool cache ${htcPath}`);
5457

55-
if (cache.isFeatureAvailable()) {
58+
if (!this.ghaNoCache && cache.isFeatureAvailable()) {
5659
core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
5760
await cache.saveCache([this.cacheDir], this.ghaCacheKey);
5861
}
@@ -67,14 +70,16 @@ export class Cache {
6770
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
6871
}
6972

70-
if (cache.isFeatureAvailable()) {
73+
if (!this.ghaNoCache && cache.isFeatureAvailable()) {
7174
core.debug(`GitHub Actions cache feature available`);
7275
if (await cache.restoreCache([this.cacheDir], this.ghaCacheKey)) {
7376
core.info(`Restored ${this.ghaCacheKey} from GitHub Actions cache`);
7477
htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
7578
core.info(`Restored to hosted tool cache ${htcPath}`);
7679
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
7780
}
81+
} else if (this.ghaNoCache) {
82+
core.info(`GitHub Actions cache disabled`);
7883
} else {
7984
core.info(`GitHub Actions cache feature not available`);
8085
}

0 commit comments

Comments
 (0)