From cbc244c2f4311b0116be5fdb5420600d49f4325a Mon Sep 17 00:00:00 2001
From: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Date: Fri, 23 Feb 2024 09:53:23 +0100
Subject: [PATCH] buildx: ghaNoCache opt for download/build to disable binary
 cache

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
---
 __tests__/buildx/install.test.ts | 11 +++++++++++
 src/buildx/install.ts            | 16 ++++++++++------
 src/cache.ts                     |  9 +++++++--
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/__tests__/buildx/install.test.ts b/__tests__/buildx/install.test.ts
index 14c87491..c8269d46 100644
--- a/__tests__/buildx/install.test.ts
+++ b/__tests__/buildx/install.test.ts
@@ -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([
diff --git a/src/buildx/install.ts b/src/buildx/install.ts
index 6b9b397c..a1ed19ca 100644
--- a/src/buildx/install.ts
+++ b/src/buildx/install.ts
@@ -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();
diff --git a/src/cache.ts b/src/cache.ts
index 63e78364..1baa6235 100644
--- a/src/cache.ts
+++ b/src/cache.ts
@@ -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`);
     }