From b61cd1c3387d162ab9eec789ea5ff8a4407ac330 Mon Sep 17 00:00:00 2001
From: Aayush <aayush.shah15@gmail.com>
Date: Fri, 20 Sep 2024 19:56:48 -0400
Subject: [PATCH] add a `fallback` input

---
 action.yml     |  3 +++
 src/context.ts |  4 +++-
 src/main.ts    | 10 ++++++++--
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/action.yml b/action.yml
index 2fa9806..afbf3fb 100644
--- a/action.yml
+++ b/action.yml
@@ -7,6 +7,9 @@ branding:
   color: 'blue'
 
 inputs:
+  nofallback:
+    description: "Fail the build if the remote builder is not available; defaults to false"
+    required: false
   add-hosts:
     description: "List of a customs host-to-IP mapping (e.g., docker:10.180.0.1)"
     required: false
diff --git a/src/context.ts b/src/context.ts
index d1e79cf..07c4f16 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -40,6 +40,7 @@ export interface Inputs {
   target: string;
   ulimit: string[];
   'github-token': string;
+  nofallback: boolean;
 }
 
 export async function getInputs(): Promise<Inputs> {
@@ -75,7 +76,8 @@ export async function getInputs(): Promise<Inputs> {
     tags: Util.getInputList('tags'),
     target: core.getInput('target'),
     ulimit: Util.getInputList('ulimit', {ignoreComma: true}),
-    'github-token': core.getInput('github-token')
+    'github-token': core.getInput('github-token'),
+    nofallback: core.getBooleanInput('nofallback')
   };
 }
 
diff --git a/src/main.ts b/src/main.ts
index 08c0a74..104b1cb 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -97,7 +97,9 @@ async function getRemoteBuilderAddr(inputs: context.Inputs): Promise<string | nu
     return null;
   } catch (error) {
     if (error.response && error.response.status === 404) {
-      core.warning('No builder instances were available, falling back to a local build');
+      if (!inputs.nofallback) {
+        core.warning('No builder instances were available, falling back to a local build');
+      }
     } else {
       core.warning(`Error in getBuildkitdAddr: ${error.message}`);
     }
@@ -171,7 +173,11 @@ actionsToolkit.run(
     await core.group(`Starting Blacksmith remote builder`, async () => {
       remoteBuilderAddr = await getRemoteBuilderAddr(inputs);
       if (!remoteBuilderAddr) {
-        core.warning('Failed to obtain Blacksmith remote builder address. Falling back to a local build.');
+        if (inputs.nofallback) {
+          core.setFailed('Failed to obtain Blacksmith builder. Failing the build');
+        } else {
+          core.warning('Failed to obtain Blacksmith remote builder address. Falling back to a local build.');
+        }
       }
     });