Skip to content

Commit

Permalink
Force authentication if GOOGLE_GHA_CREDS_PATH is set
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Nov 10, 2022
1 parent 5c6749c commit eed1043
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/setup-gcloud.ts
Expand Up @@ -17,6 +17,7 @@
import * as core from '@actions/core';
import * as toolCache from '@actions/tool-cache';
import {
authenticateGcloudSDK,
getLatestGcloudSDKVersion,
installComponent,
installGcloudSDK,
Expand Down Expand Up @@ -60,6 +61,17 @@ export async function run(): Promise<void> {
await installComponent(components.split(',').map((comp) => comp.trim()));
}

// Authenticate - this comes from google-github-actions/auth
const credFile = process.env.GOOGLE_GHA_CREDS_PATH;
if (credFile) {
await authenticateGcloudSDK(credFile);
core.info('Successfully authenticated');
} else {
core.warning(
'No authentication found for gcloud, authenticate with `google-github-actions/auth`.',
);
}

// Set the project ID, if given.
const projectId = core.getInput('project_id');
if (projectId) {
Expand Down
9 changes: 9 additions & 0 deletions tests/setup-gcloud.test.ts
Expand Up @@ -46,6 +46,7 @@ describe('#run', function () {
exportVariable: sinon.stub(core, 'exportVariable'),
setFailed: sinon.stub(core, 'setFailed'),
warning: sinon.stub(core, 'warning'),
authenticateGcloudSDK: sinon.stub(setupGcloud, 'authenticateGcloudSDK'),
installGcloudSDK: sinon.stub(setupGcloud, 'installGcloudSDK'),
isInstalled: sinon.stub(setupGcloud, 'isInstalled').returns(false),
setProject: sinon.stub(setupGcloud, 'setProject'),
Expand Down Expand Up @@ -114,6 +115,14 @@ describe('#run', function () {
expect(this.stubs.installComponent.callCount).to.eq(1);
});

it('authenticates if GOOGLE_GHA_CREDS is set', async function () {
this.stubs.env
.value({ GOOGLE_GHA_CREDS_PATH: 'foo/bar/path.json' })
.returns('{}');
await run();
expect(this.stubs.authenticateGcloudSDK.callCount).to.eq(1);
});

it('sets the project ID if provided', async function () {
this.stubs.getInput.withArgs('project_id').returns('test');
await run();
Expand Down

0 comments on commit eed1043

Please sign in to comment.