mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-23 11:36:10 +08:00
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:
parent
f1c76199be
commit
cbc244c2f4
@ -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([
|
||||
|
@ -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();
|
||||
|
@ -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`);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user