diff --git a/action.yml b/action.yml index ca5694c81..632ebaf66 100644 --- a/action.yml +++ b/action.yml @@ -86,6 +86,18 @@ inputs: default: false required: false + skip_auth_check: + description: |- + Skip the check for an authenticated `gcloud` and suppress the warning + emitted when none is found. This is useful when the action is only used + to install components or to put `gcloud` on the PATH (for example, to + drive a local emulator) and no Google Cloud credentials are needed. + + ⚠️ If you enable this, the action will not tell you when `gcloud` is + unauthenticated, even if a later step needs credentials. + default: false + required: false + outputs: version: description: |- diff --git a/src/main.ts b/src/main.ts index 0e72e12b4..c80cae1dc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,6 +56,7 @@ export async function run(): Promise { let version = presence(core.getInput('version')); const components = core.getInput('install_components'); const projectId = core.getInput('project_id'); + const skipAuthCheck = parseBoolean(core.getInput('skip_auth_check')); const cache = parseBoolean(core.getInput('cache')); if (skipInstall) { @@ -105,6 +106,8 @@ export async function run(): Promise { if (credFile) { await authenticateGcloudSDK(credFile); core.info('Successfully authenticated'); + } else if (skipAuthCheck) { + core.debug(`Skipping the authentication check ("skip_auth_check" was true)`); } else { let authed; try { diff --git a/tests/setup-gcloud.test.ts b/tests/setup-gcloud.test.ts index f1a3c6431..a11f1cf6a 100644 --- a/tests/setup-gcloud.test.ts +++ b/tests/setup-gcloud.test.ts @@ -214,6 +214,41 @@ test('#run', { concurrency: true }, async (suite) => { assert.deepStrictEqual(mocks.setProject.mock.callCount(), 0); }); + + await suite.test('warns when there is no authentication', async (t) => { + const mocks = defaultMocks(t.mock, {}); + + // The suite restores process.env by assigning a copy back, which does not + // remove variables set by previous tests. + delete process.env.GOOGLE_GHA_CREDS_PATH; + + await run(); + + assert.deepStrictEqual(mocks.isAuthenticated.mock.callCount(), 1); + assert.match(mocks.logWarning.mock.calls?.at(0)?.arguments?.at(0), /not authenticated/); + }); + + await suite.test('skips the authentication check when skip_auth_check is true', async (t) => { + const mocks = defaultMocks(t.mock, { + skip_auth_check: 'true', + }); + await run(); + + assert.deepStrictEqual(mocks.isAuthenticated.mock.callCount(), 0); + assert.deepStrictEqual(mocks.logWarning.mock.callCount(), 0); + }); + + await suite.test('authenticates even when skip_auth_check is true', async (t) => { + const mocks = defaultMocks(t.mock, { + skip_auth_check: 'true', + }); + + process.env.GOOGLE_GHA_CREDS_PATH = '/foo/bar/path.json'; + + await run(); + + assert.deepStrictEqual(mocks.authenticateGcloudSDK.mock.callCount(), 1); + }); }); /**