Skip to content

Commit 04a6fc4

Browse files
authored
bug: set project via a flag if specified (#278)
Because /auth exports the project ID as an environment variable, that environment variable takes precedence over the core property. This previously worked because setup-gcloud didn't export said environment variable. To fix this, I updated the gcloud command to use the --project flag if a project ID was given. This has the added side-effect of being able to remove all the code to configure gcloud, since it was never really necessary.
1 parent 8b52d43 commit 04a6fc4

File tree

8 files changed

+56
-35
lines changed

8 files changed

+56
-35
lines changed

.github/workflows/deploy-cloudrun-credentials-it.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
run: npm run e2e-tests
3939
env:
4040
URL: ${{ steps.deploy.outputs.url }}
41+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
4142

4243
b64_json:
4344
name: with base64 creds
@@ -70,6 +71,7 @@ jobs:
7071
run: npm run e2e-tests
7172
env:
7273
URL: ${{ steps.deploy.outputs.url }}
74+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
7375

7476
json:
7577
name: with json creds
@@ -102,6 +104,7 @@ jobs:
102104
run: npm run e2e-tests
103105
env:
104106
URL: ${{ steps.deploy.outputs.url }}
107+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
105108

106109
wif:
107110
name: with wif creds
@@ -146,6 +149,7 @@ jobs:
146149
run: npm run e2e-tests
147150
env:
148151
URL: ${{ steps.deploy.outputs.url }}
152+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
149153

150154
cleanup:
151155
name: Clean Up

.github/workflows/deploy-cloudrun-it.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
run: npm run e2e-tests
4343
env:
4444
URL: ${{ steps.deploy_1.outputs.url }}
45+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
4546
SERVICE: ${{ steps.service.outputs.service }}
4647
ENV: TEST_ENV=TEST_VAR
4748

@@ -58,6 +59,7 @@ jobs:
5859
run: npm run e2e-tests
5960
env:
6061
URL: ${{ steps.deploy_2.outputs.url }}
62+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
6163
SERVICE: ${{ steps.service.outputs.service }}
6264
ENV: TEST_ENV=TEST_VAR,TEST_ENV2=TEST_VAR2
6365
COUNT: 2
@@ -97,6 +99,7 @@ jobs:
9799
run: npm run e2e-tests
98100
env:
99101
URL: ${{ steps.deploy_1.outputs.url }}
102+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
100103
SERVICE: ${{ steps.service.outputs.service }}
101104
SECRET_ENV: MY_SECRET=secret_value:latest
102105

@@ -113,6 +116,7 @@ jobs:
113116
run: npm run e2e-tests
114117
env:
115118
URL: ${{ steps.deploy_2.outputs.url }}
119+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
116120
SERVICE: ${{ steps.service.outputs.service }}
117121
SECRET_ENV: MY_SECRET=secret_value:latest
118122
SECRET_VOLUMES: /api/secrets/my-secret=secret_value:latest
@@ -154,6 +158,7 @@ jobs:
154158
run: npm run e2e-tests
155159
env:
156160
URL: ${{ steps.deploy.outputs.url }}
161+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
157162

158163
metadata:
159164
name: with full YAML metadata
@@ -192,6 +197,7 @@ jobs:
192197
run: npm run e2e-tests
193198
env:
194199
URL: ${{ steps.deploy_1.outputs.url }}
200+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
195201
SERVICE: ${{ steps.service.outputs.service }}
196202
PARAMS: "{\"cpu\": 2,\"memory\": \"1Gi\", \"containerConcurrency\": 20}"
197203
ANNOTATIONS: "{\"run.googleapis.com/cloudsql-instances\": \"test-project:us-central1:my-test-instance\"}"
@@ -209,6 +215,7 @@ jobs:
209215
run: npm run e2e-tests
210216
env:
211217
URL: ${{ steps.deploy_2.outputs.url }}
218+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
212219
SERVICE: ${{ steps.service.outputs.service }}
213220
PARAMS: "{\"cpu\": 2,\"memory\": \"1Gi\", \"containerConcurrency\": 20}"
214221
ANNOTATIONS: "{\"run.googleapis.com/cloudsql-instances\": \"test-project:us-central1:my-test-instance\"}"
@@ -251,6 +258,7 @@ jobs:
251258
run: npm run e2e-tests
252259
env:
253260
URL: ${{ steps.deploy_1.outputs.url }}
261+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
254262
SERVICE: ${{ steps.service.outputs.service }}
255263
REVISION: ${{ steps.service.outputs.service }}-001
256264

@@ -269,6 +277,7 @@ jobs:
269277
run: npm run e2e-tests
270278
env:
271279
URL: ${{ steps.deploy_2.outputs.url }}
280+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
272281
SERVICE: ${{ steps.service.outputs.service }}
273282
REVISION: ${{ steps.service.outputs.service }}-002
274283
COUNT: 2

.github/workflows/deploy-cloudrun-traffic-it.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
run: npm run e2e-tests
4242
env:
4343
URL: ${{ steps.deploy_1.outputs.url }}
44+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
4445
SERVICE: ${{ steps.service.outputs.service }}
4546

4647
- name: Update Service with env vars
@@ -57,6 +58,7 @@ jobs:
5758
run: npm run e2e-tests
5859
env:
5960
URL: ${{ steps.deploy_2.outputs.url }}
61+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
6062
SERVICE: ${{ steps.service.outputs.service }}
6163
ENV: TEST_ENV=TEST_VAR
6264
PARAMS: "{\"cpu\": 2,\"memory\": \"1Gi\", \"containerConcurrency\": 20}"
@@ -96,6 +98,7 @@ jobs:
9698
run: npm run e2e-tests
9799
env:
98100
URL: ${{ steps.deploy_1.outputs.url }}
101+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
99102
SERVICE: ${{ steps.service.outputs.service }}
100103
REVISION: ${{ steps.service.outputs.service }}-002
101104

@@ -135,6 +138,7 @@ jobs:
135138
run: npm run e2e-tests
136139
env:
137140
URL: ${{ steps.deploy_1.outputs.url }}
141+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
138142
SERVICE: ${{ steps.service.outputs.service }}
139143
TAG: test-tag
140144

@@ -159,6 +163,7 @@ jobs:
159163
run: npm run e2e-tests
160164
env:
161165
URL: ${{ steps.traffic_1.outputs.url }}
166+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
162167
SERVICE: ${{ steps.service.outputs.service }}
163168
TAG: test-tag
164169
TRAFFIC: 100
@@ -175,6 +180,7 @@ jobs:
175180
run: npm run e2e-tests
176181
env:
177182
URL: ${{ steps.traffic_2.outputs.url }}
183+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
178184
SERVICE: ${{ steps.service.outputs.service }}
179185
TAG: test-tag
180186
TRAFFIC: 80
@@ -193,6 +199,7 @@ jobs:
193199
run: npm run e2e-tests
194200
env:
195201
URL: ${{ steps.deploy_3.outputs.url }}
202+
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
196203
SERVICE: ${{ steps.service.outputs.service }}
197204
TAG: test-tag
198205
TRAFFIC: 80

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "ncc build -m src/index.ts",
8-
"lint": "eslint src/ --ext .ts,.tsx",
9-
"format": "prettier --write **/*.ts",
8+
"lint": "eslint src/ tests/ --ext .ts,.tsx",
9+
"format": "eslint src/ tests/ --ext .ts,.tsx --fix",
1010
"test": "mocha -r ts-node/register -t 150s 'tests/unit/*.test.ts' --exit",
1111
"e2e-tests": "mocha -r ts-node/register -t 150s 'tests/e2e.test.ts' --exit"
1212
},

src/deploy-cloudrun.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,15 @@ export async function run(): Promise<void> {
165165
}
166166

167167
// set PROJECT ID
168-
if (projectId) {
169-
await setupGcloud.setProject(projectId);
170-
} else if (credentials) {
171-
projectId = await setupGcloud.setProjectWithKey(credentials);
172-
} else if (process.env.GCLOUD_PROJECT) {
173-
await setupGcloud.setProject(process.env.GCLOUD_PROJECT);
168+
if (!projectId) {
169+
if (credentials) {
170+
const key = setupGcloud.parseServiceAccountKey(credentials);
171+
projectId = key.project_id;
172+
} else if (process.env.GCLOUD_PROJECT) {
173+
projectId = process.env.GCLOUD_PROJECT;
174+
}
174175
}
175-
// Fail if no Project Id is provided if not already set.
176-
const projectIdSet = await setupGcloud.isProjectIdSet();
177-
if (!projectIdSet)
178-
throw new Error(
179-
'No project Id provided. Ensure you have set either the project_id or credentials fields.',
180-
);
176+
if (projectId) cmd.push('--project', projectId);
181177

182178
// Install beta components if needed and prepend the beta command
183179
if (installBeta) {

tests/e2e.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function sleep(ms: number) {
2828

2929
describe('E2E tests', function () {
3030
const {
31+
PROJECT_ID,
3132
PARAMS,
3233
ANNOTATIONS,
3334
LABELS,
@@ -51,13 +52,15 @@ describe('E2E tests', function () {
5152
throw Error('URL not found.');
5253
}
5354
toolCommand = 'gcloud';
54-
if (SERVICE) {
55+
if (SERVICE && PROJECT_ID) {
5556
// get Service yaml
56-
let cmd = [
57+
const cmd = [
5758
'run',
5859
'services',
5960
'describe',
6061
SERVICE,
62+
'--project',
63+
PROJECT_ID,
6164
'--format',
6265
'yaml',
6366
'--platform',
@@ -200,17 +203,18 @@ describe('E2E tests', function () {
200203
});
201204

202205
it('has the correct revision count', async function () {
203-
if (COUNT && SERVICE) {
206+
if (COUNT && SERVICE && PROJECT_ID) {
204207
const max = 3;
205-
let attempt = 0;
208+
const attempt = 0;
206209
let revisions = [];
207210
while (attempt < max && revisions.length < parseInt(COUNT)) {
208211
await sleep(1000 * attempt);
209-
210-
let cmd = [
212+
const cmd = [
211213
'run',
212214
'revisions',
213215
'list',
216+
'--project',
217+
PROJECT_ID,
214218
'--service',
215219
SERVICE,
216220
'--format',

tests/unit/deploy-cloudrun.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import 'mocha';
1818
import * as sinon from 'sinon';
1919
import * as core from '@actions/core';
20+
import * as exec from '@actions/exec';
2021
import * as setupGcloud from '@google-github-actions/setup-cloud-sdk';
2122
import { expect } from 'chai';
2223
import { run, setUrlOutput, parseFlags } from '../../src/deploy-cloudrun';
2324

24-
/* eslint-disable @typescript-eslint/camelcase */
2525
// These are mock data for github actions inputs, where camel case is expected.
2626
const fakeInputs: { [key: string]: string } = {
2727
image: 'gcr.io/cloudrun/hello',
@@ -36,7 +36,6 @@ const fakeInputs: { [key: string]: string } = {
3636
revision_traffic: '',
3737
tag_traffic: '',
3838
};
39-
/* eslint-enable @typescript-eslint/camelcase */
4039

4140
function getInputMock(name: string): string {
4241
return fakeInputs[name];
@@ -53,10 +52,11 @@ describe('#run', function () {
5352
authenticateGcloudSDK: sinon.stub(setupGcloud, 'authenticateGcloudSDK'),
5453
isAuthenticated: sinon.stub(setupGcloud, 'isAuthenticated').resolves(true),
5554
isInstalled: sinon.stub(setupGcloud, 'isInstalled').returns(false),
56-
setProject: sinon.stub(setupGcloud, 'setProject'),
57-
setProjectWithKey: sinon.stub(setupGcloud, 'setProjectWithKey'),
55+
parseServiceAccountKey: sinon.stub(setupGcloud, 'parseServiceAccountKey'),
5856
isProjectIdSet: sinon.stub(setupGcloud, 'isProjectIdSet').resolves(true),
5957
installComponent: sinon.stub(setupGcloud, 'installComponent'),
58+
59+
getExecOutput: sinon.stub(exec, 'getExecOutput'),
6060
};
6161
});
6262

@@ -67,21 +67,22 @@ describe('#run', function () {
6767
it('sets the project ID if provided', async function () {
6868
this.stubs.getInput.withArgs('project_id').returns('my-test-project');
6969
await run();
70-
expect(this.stubs.setProject.withArgs('my-test-project').callCount).to.eq(1);
70+
71+
const call = this.stubs.getExecOutput.getCall(0);
72+
expect(call).to.be;
73+
const args = call.args[1];
74+
expect(args).to.include.members(['--project', 'my-test-project']);
7175
});
7276
it('sets the project ID if GCLOUD_PROJECT is provided', async function () {
7377
this.stubs.getInput.withArgs('project_id').returns('');
7478
this.stubs.getInput.withArgs('credentials').returns('');
7579
process.env.GCLOUD_PROJECT = 'my-test-project';
7680
await run();
77-
expect(this.stubs.setProject.withArgs('my-test-project').callCount).to.eq(1);
78-
});
79-
it('does not set the project ID if not provided', async function () {
80-
this.stubs.getInput.withArgs('project_id').returns('');
81-
this.stubs.getInput.withArgs('credentials').returns('');
82-
process.env.GCLOUD_PROJECT = '';
83-
await run();
84-
expect(this.stubs.setProject.callCount).to.eq(0);
81+
82+
const call = this.stubs.getExecOutput.getCall(0);
83+
expect(call).to.be;
84+
const args = call.args[1];
85+
expect(args).to.include.members(['--project', 'my-test-project']);
8586
});
8687
it('installs the gcloud SDK if it is not already installed', async function () {
8788
this.stubs.isInstalled.returns(false);
@@ -102,7 +103,7 @@ describe('#run', function () {
102103
this.stubs.getInput.withArgs('credentials').returns('key');
103104
this.stubs.getInput.withArgs('project_id').returns('');
104105
await run();
105-
expect(this.stubs.setProjectWithKey.withArgs('key').callCount).to.eq(1);
106+
expect(this.stubs.parseServiceAccountKey.withArgs('key').callCount).to.eq(1);
106107
});
107108
it('fails if credentials and project_id are not provided', async function () {
108109
this.stubs.getInput.withArgs('credentials').returns('');

0 commit comments

Comments
 (0)