Skip to content

Commit 190e2c1

Browse files
authored
fix: Error when project ID or credentials are not provided (#27)
* Squash commits Add new tests update tests Update work dir fix tests fix gcloud * Update deploy-appengine-inputs-it.yml * Update deploy-appengine-inputs-it.yml * Update deploy-appengine-creds-it.yml
1 parent c85fa33 commit 190e2c1

File tree

6 files changed

+135
-51
lines changed

6 files changed

+135
-51
lines changed

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

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,31 @@ jobs:
1313
with:
1414
project_id: ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
1515
service_account_key: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_JSON }}
16-
export_default_credentials: true
17-
- name: create app
18-
run: |-
19-
cat <<EOF > ${{ github.workspace }}/app.yaml
20-
service: "${{ github.job }}-${{ github.run_number }}"
21-
runtime: "nodejs10"
22-
EOF
23-
24-
cat <<EOF > ${{ github.workspace }}/server.js
25-
const http = require('http');
26-
const server = http.createServer(function (req, res) {
27-
res.writeHead(200)
28-
res.end('Hello world!');
29-
});
30-
server.listen(process.env.PORT || 8080);
31-
EOF
16+
17+
- name: Update app.yaml
18+
run: |-
19+
echo "service: ${{ github.job }}-${{ github.run_number }}" >> ${{ github.workspace }}/example-app/app.yaml
20+
3221
- name: Build dependency
3322
working-directory: setupGcloudSDK
3423
run: |-
3524
npm install
3625
npm run build
26+
3727
- id: build
3828
name: Build dist
3929
run: |-
4030
npm install
4131
npm run build
32+
4233
- id: deploy
4334
name: Deploy to App Engine
4435
uses: ./
4536
with:
46-
project_id: ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
4737
version: gcloud
4838
promote: false
39+
working_directory: ${{ github.workspace }}/example-app/
40+
4941
- name: Test Output
5042
run: |-
5143
curl '${{ steps.deploy.outputs.url }}' \
@@ -56,31 +48,71 @@ jobs:
5648
--retry-connrefused \
5749
--retry-delay 5 \
5850
--retry-max-time 300
51+
5952
- name: Clean Up
6053
if: ${{ always() }}
6154
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
6255

56+
gcloud2:
57+
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name }}
58+
name: with setup-gcloud - no project Id
59+
runs-on: ubuntu-latest
60+
steps:
61+
- uses: actions/checkout@v2
62+
- uses: google-github-actions/setup-gcloud@master
63+
with:
64+
service_account_key: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_JSON }}
65+
66+
- name: Update app.yaml
67+
run: |-
68+
echo "service: ${{ github.job }}-${{ github.run_number }}" >> ${{ github.workspace }}/example-app/app.yaml
69+
70+
- name: Build dependency
71+
working-directory: setupGcloudSDK
72+
run: |-
73+
npm install
74+
npm run build
75+
76+
- id: build
77+
name: Build dist
78+
run: |-
79+
npm install
80+
npm run build
81+
82+
- id: deploy
83+
name: Deploy to App Engine
84+
uses: ./
85+
with:
86+
project_id: ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
87+
version: gcloud
88+
promote: false
89+
working_directory: ${{ github.workspace }}/example-app/
90+
91+
- name: Test Output
92+
run: |-
93+
curl '${{ steps.deploy.outputs.url }}' \
94+
--silent \
95+
--fail \
96+
--location \
97+
--retry 5 \
98+
--retry-connrefused \
99+
--retry-delay 5 \
100+
--retry-max-time 300
101+
102+
- name: Clean Up
103+
if: ${{ always() }}
104+
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet --project ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
105+
63106
b64-json:
64107
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name }}
65108
name: with base64 json creds
66109
runs-on: ubuntu-latest
67110
steps:
68111
- uses: actions/checkout@v2
69-
- name: create app
70-
run: |-
71-
cat <<EOF > ${{ github.workspace }}/app.yaml
72-
service: "${{ github.job }}-${{ github.run_number }}"
73-
runtime: "nodejs10"
74-
EOF
75-
76-
cat <<EOF > ${{ github.workspace }}/server.js
77-
const http = require('http');
78-
const server = http.createServer(function (req, res) {
79-
res.writeHead(200)
80-
res.end('Hello world!');
81-
});
82-
server.listen(process.env.PORT || 8080);
83-
EOF
112+
- name: Update app.yaml
113+
run: |-
114+
echo "service: ${{ github.job }}-${{ github.run_number }}" >> ${{ github.workspace }}/example-app/app.yaml
115+
84116
- name: Build dependency
85117
working-directory: setupGcloudSDK
86118
run: |-
@@ -91,13 +123,16 @@ jobs:
91123
run: |-
92124
npm install
93125
npm run build
126+
94127
- id: deploy
95128
name: Deploy to App Engine
96129
uses: ./
97130
with:
98131
credentials: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_B64 }}
99132
version: b64-json
100133
promote: false # Allows for deletion
134+
working_directory: ${{ github.workspace }}/example-app/
135+
101136
- name: Test Output
102137
run: |-
103138
curl '${{ steps.deploy.outputs.url }}' \
@@ -118,21 +153,10 @@ jobs:
118153
runs-on: ubuntu-latest
119154
steps:
120155
- uses: actions/checkout@v2
121-
- name: create app
122-
run: |-
123-
cat <<EOF > ${{ github.workspace }}/app.yaml
124-
service: "${{ github.job }}-${{ github.run_number }}"
125-
runtime: "nodejs10"
126-
EOF
127-
128-
cat <<EOF > ${{ github.workspace }}/server.js
129-
const http = require('http');
130-
const server = http.createServer(function (req, res) {
131-
res.writeHead(200)
132-
res.end('Hello world!');
133-
});
134-
server.listen(process.env.PORT || 8080);
135-
EOF
156+
- name: Update app.yaml
157+
run: |-
158+
echo "service: ${{ github.job }}-${{ github.run_number }}" >> ${{ github.workspace }}/example-app/app.yaml
159+
136160
- name: Build dependency
137161
working-directory: setupGcloudSDK
138162
run: |-
@@ -143,13 +167,16 @@ jobs:
143167
run: |-
144168
npm install
145169
npm run build
170+
146171
- id: deploy
147172
name: Deploy to App Engine
148173
uses: ./
149174
with:
150175
credentials: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_JSON }}
151176
version: json
152177
promote: false # Allows for deletion
178+
working_directory: ${{ github.workspace }}/example-app/
179+
153180
- name: Test Output
154181
run: |-
155182
curl '${{ steps.deploy.outputs.url }}' \
@@ -160,6 +187,7 @@ jobs:
160187
--retry-connrefused \
161188
--retry-delay 5 \
162189
--retry-max-time 300
190+
163191
- name: Clean Up
164192
if: ${{ always() }}
165193
run: gcloud app services delete "${{ github.job }}-${{ github.run_number }}" --quiet
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: deploy-appengine Inputs Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
bad-input:
7+
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name }}
8+
name: with bad inputs
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@master
13+
with:
14+
node-version: 12.x
15+
- name: Build dependency
16+
working-directory: setupGcloudSDK
17+
run: |-
18+
npm install
19+
npm run build
20+
- id: build
21+
name: Build dist
22+
run: |-
23+
npm install
24+
npm run build
25+
26+
- name: Deploy fails with no credentials
27+
id: test2
28+
uses: ./
29+
continue-on-error: true
30+
with:
31+
project_id: ${{ secrets.APPENGINE_DEPLOY_PROJECT_ID }}
32+
working_directory: ${{ github.workspace }}/example-app/
33+
- name: Catch failure
34+
run: |-
35+
if [ ${{ steps.test2.outcome }} != 'failure' ]; then exit 1; fi
36+
37+
- uses: google-github-actions/setup-gcloud@master
38+
with:
39+
service_account_key: ${{ secrets.APPENGINE_DEPLOY_SA_KEY_JSON }}
40+
41+
- name: Deploy fails with no project Id
42+
id: test1
43+
uses: ./
44+
continue-on-error: true
45+
with:
46+
working_directory: ${{ github.workspace }}/example-app/
47+
- name: Catch failure
48+
run: |-
49+
if [ ${{ steps.test1.outcome }} != 'failure' ]; then exit 1; fi
50+

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ inputs:
5252
promote:
5353
description: Promote the deployed version to receive all traffic.
5454
required: false
55-
default: true
55+
default: "true"
5656

5757
credentials:
5858
description: |-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"build": "ncc build src/main.ts",
88
"lint": "eslint . --ext .ts,.tsx",
9-
"format": "prettier --write **/**/*.ts",
9+
"format": "prettier --write **/**.ts",
1010
"test": "mocha -r ts-node/register -t 90s 'setupGcloudSDK/tests/*.test.ts'"
1111
},
1212
"repository": {

setupGcloudSDK/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ export async function isAuthenticated(): Promise<boolean> {
9797
const stdout = (data: Buffer): void => {
9898
output += data.toString();
9999
};
100+
const stderr = (data: Buffer): void => {
101+
output += data.toString();
102+
};
100103
const options = {
101104
listeners: {
102105
stdout,
106+
stderr,
103107
},
104108
};
105109

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ async function run(): Promise<void> {
5959
// Fail if no Project Id is provided if not already set.
6060
const projectIdSet = await setupGcloud.isProjectIdSet();
6161
if (!projectIdSet && projectId === '' && serviceAccountKey === '') {
62-
core.setFailed('No project Id provided.');
62+
throw new Error(
63+
'No project Id provided. Ensure you have either project_id and/or credentials inputs are set.',
64+
);
6365
}
6466

6567
// Authenticate gcloud SDK.
@@ -72,7 +74,7 @@ async function run(): Promise<void> {
7274
}
7375
const authenticated = await setupGcloud.isAuthenticated();
7476
if (!authenticated) {
75-
core.setFailed('Error authenticating the Cloud SDK.');
77+
throw new Error('Error authenticating the Cloud SDK.');
7678
}
7779

7880
const toolCommand = setupGcloud.getToolCommand();

0 commit comments

Comments
 (0)