Skip to content

Commit 555ade4

Browse files
authored
bug: set project via a flag if specified (#209)
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. Also, I'm not sure it was ever a good idea to modify global configuration for gcloud just because someone deployed a service. Using the flag is probably the best approach here. You might notice that I added PROJECT_ID to the e2e tests. That's because they were secretly tightly coupled with the previous step that had previously run.
1 parent 7a84f16 commit 555ade4

File tree

6 files changed

+64
-49
lines changed

6 files changed

+64
-49
lines changed

.github/workflows/deploy-appengine-creds-it.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ jobs:
4949
5050
- name: Clean Up
5151
if: ${{ always() }}
52-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
52+
run: |-
53+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
54+
--quiet \
55+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
5356
5457
gcloud2:
5558
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -93,7 +96,10 @@ jobs:
9396
9497
- name: Clean Up
9598
if: ${{ always() }}
96-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet --project ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
99+
run: |-
100+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
101+
--quiet \
102+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
97103
98104
b64-json:
99105
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -132,7 +138,10 @@ jobs:
132138
--retry-max-time 300
133139
- name: Clean Up
134140
if: ${{ always() }}
135-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
141+
run: |-
142+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
143+
--quiet \
144+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
136145
137146
json:
138147
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -172,7 +181,10 @@ jobs:
172181
173182
- name: Clean Up
174183
if: ${{ always() }}
175-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
184+
run: |-
185+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
186+
--quiet \
187+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
176188
177189
wif:
178190
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -226,4 +238,7 @@ jobs:
226238
227239
- name: Clean Up
228240
if: ${{ always() }}
229-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
241+
run: |-
242+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
243+
--quiet \
244+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"

.github/workflows/deploy-appengine-inputs-it.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ jobs:
5757
--retry-max-time 300
5858
- name: Clean Up
5959
if: ${{ always() }}
60-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
60+
run: |-
61+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
62+
--quiet \
63+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
6164
6265
bad-input:
6366
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -85,10 +88,10 @@ jobs:
8588
run: |-
8689
if [ ${{ steps.test2.outcome }} != 'failure' ]; then exit 1; fi
8790
88-
# unset project id set by previous deploy
91+
# unset project id set by previous deploy
8992
- run: |-
9093
gcloud config unset project
91-
94+
9295
- uses: google-github-actions/setup-gcloud@master
9396
with:
9497
service_account_key: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_JSON }}
@@ -102,4 +105,3 @@ jobs:
102105
- name: Catch failure
103106
run: |-
104107
if [ ${{ steps.test1.outcome }} != 'failure' ]; then exit 1; fi
105-

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ jobs:
5858
--retry-max-time 300
5959
- name: Clean Up
6060
if: ${{ always() }}
61-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
61+
run: |-
62+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
63+
--quiet \
64+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
6265
6366
deliverable:
6467
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
@@ -111,8 +114,11 @@ jobs:
111114
--retry-max-time 300
112115
- name: Clean Up
113116
if: ${{ always() }}
114-
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
115-
117+
run: |-
118+
gcloud app services delete "${{ github.job }}-${{ github.run_number }}" \
119+
--quiet \
120+
--project="${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}"
121+
116122
failure:
117123
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
118124
name: with bad path
@@ -153,4 +159,3 @@ jobs:
153159
if [ ${{ steps.deliverable.outcome }} != 'failure' ]; then
154160
exit 1
155161
fi
156-

package.json

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

src/deploy-appengine.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ import {
3030
getLatestGcloudSDKVersion,
3131
isInstalled as isGcloudSDKInstalled,
3232
installGcloudSDK,
33-
isProjectIdSet,
34-
setProject,
33+
parseServiceAccountKey,
3534
authenticateGcloudSDK,
36-
setProjectWithKey,
3735
isAuthenticated,
3836
getToolCommand,
3937
} from '@google-github-actions/setup-cloud-sdk';
@@ -73,13 +71,13 @@ export async function run(): Promise<void> {
7371
const imageUrl = getInput('image_url');
7472
const version = getInput('version');
7573
const promote = getInput('promote');
76-
const serviceAccountKey = getInput('credentials');
74+
const credentials = getInput('credentials');
7775
const flags = getInput('flags');
7876

7977
// Add warning if using credentials
80-
if (serviceAccountKey) {
78+
if (credentials) {
8179
logWarning(
82-
'"credentials" input has been deprecated. ' +
80+
'The "credentials" input is deprecated. ' +
8381
'Please switch to using google-github-actions/auth which supports both Workload Identity Federation and JSON Key authentication. ' +
8482
'For more details, see https://github.com/google-github-actions/deploy-appengine#authorization',
8583
);
@@ -106,31 +104,27 @@ export async function run(): Promise<void> {
106104
await installGcloudSDK(gcloudVersion);
107105
}
108106

109-
// set PROJECT ID
110-
if (projectId) {
111-
await setProject(projectId);
112-
} else if (serviceAccountKey) {
113-
projectId = await setProjectWithKey(serviceAccountKey);
114-
} else if (process.env.GCLOUD_PROJECT) {
115-
await setProject(process.env.GCLOUD_PROJECT);
116-
}
117-
// Fail if no Project Id is provided if not already set.
118-
const projectIdSet = await isProjectIdSet();
119-
if (!projectIdSet) {
120-
throw new Error(
121-
'No project Id provided. Ensure you have either project_id and/or credentials inputs are set.',
122-
);
123-
}
124-
125-
// Either serviceAccountKey or GOOGLE_GHA_CREDS_PATH env var required
126-
if (serviceAccountKey || process.env.GOOGLE_GHA_CREDS_PATH) {
127-
await authenticateGcloudSDK(serviceAccountKey);
107+
// Either credentials or GOOGLE_GHA_CREDS_PATH env var required
108+
if (credentials || process.env.GOOGLE_GHA_CREDS_PATH) {
109+
await authenticateGcloudSDK(credentials);
128110
}
129111
const authenticated = await isAuthenticated();
130112
if (!authenticated) {
131113
throw new Error('Error authenticating the Cloud SDK.');
132114
}
133115

116+
// set PROJECT ID
117+
if (!projectId) {
118+
if (credentials) {
119+
logInfo(`Extracting project ID from service account key`);
120+
const key = parseServiceAccountKey(credentials);
121+
projectId = key.project_id;
122+
} else if (process.env.GCLOUD_PROJECT) {
123+
logInfo(`Extracting project ID $GCLOUD_PROJECT`);
124+
projectId = process.env.GCLOUD_PROJECT;
125+
}
126+
}
127+
134128
const toolCommand = getToolCommand();
135129

136130
// Create app engine gcloud cmd.

tests/deploy-appengine.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('#run', function () {
5050
isAuthenticated: sinon.stub(setupGcloud, 'isAuthenticated').resolves(true),
5151
isInstalled: sinon.stub(setupGcloud, 'isInstalled').returns(false),
5252
setProject: sinon.stub(setupGcloud, 'setProject'),
53-
setProjectWithKey: sinon.stub(setupGcloud, 'setProjectWithKey'),
53+
parseServiceAccountKey: sinon.stub(setupGcloud, 'parseServiceAccountKey'),
5454
isProjectIdSet: sinon.stub(setupGcloud, 'isProjectIdSet').resolves(false),
5555
getExecOutput: sinon.stub(exec, 'getExecOutput'),
5656
};
@@ -80,7 +80,7 @@ describe('#run', function () {
8080
this.stubs.getInput.withArgs('credentials').returns('key');
8181
this.stubs.getInput.withArgs('project_id').returns('');
8282
await run();
83-
expect(this.stubs.setProjectWithKey.withArgs('key').callCount).to.eq(1);
83+
expect(this.stubs.parseServiceAccountKey.withArgs('key').callCount).to.eq(1);
8484
});
8585
it('fails if credentials and project_id are not provided', async function () {
8686
this.stubs.getInput.withArgs('credentials').returns('');
@@ -137,15 +137,15 @@ describe('#setUrlOutput', function () {
137137
target url: [https://PROJECT_ID.uc.r.appspot.com]
138138
139139
140-
Do you want to continue (Y/n)?
140+
Do you want to continue (Y/n)?
141141
142142
Beginning deployment of service [default]...
143143
╔════════════════════════════════════════════════════════════╗
144144
╠═ Uploading 6 files to Google Cloud Storage ═╣
145145
╚════════════════════════════════════════════════════════════╝
146146
File upload done.
147-
Updating service [default]...done.
148-
Setting traffic split for service [default]...done.
147+
Updating service [default]...done.
148+
Setting traffic split for service [default]...done.
149149
Deployed service [default] to [https://PROJECT_ID.uc.r.appspot.com]
150150
151151
You can stream logs from the command line by running:
@@ -170,15 +170,15 @@ describe('#setUrlOutput', function () {
170170
target url: [https://service-v2-dot-PROJECT_ID.uc.r.appspot.com]
171171
172172
173-
Do you want to continue (Y/n)?
173+
Do you want to continue (Y/n)?
174174
175175
Beginning deployment of service [service-v2]...
176176
╔════════════════════════════════════════════════════════════╗
177177
╠═ Uploading 1 file to Google Cloud Storage ═╣
178178
╚════════════════════════════════════════════════════════════╝
179179
File upload done.
180-
Updating service [service-v2]...done.
181-
Setting traffic split for service [service-v2]...done.
180+
Updating service [service-v2]...done.
181+
Setting traffic split for service [service-v2]...done.
182182
Deployed service [service-v2] to [https://service-v2-dot-PROJECT_ID.uc.r.appspot.com]
183183
184184
You can stream logs from the command line by running:

0 commit comments

Comments
 (0)