Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force authentication if GOOGLE_GHA_CREDS_PATH is set #587

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Force authentication if GOOGLE_GHA_CREDS_PATH is set
  • Loading branch information
sethvargo committed Nov 10, 2022
commit eed10432aca2cf73381ac4d536d70ad32570719c
12 changes: 12 additions & 0 deletions src/setup-gcloud.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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