Skip to content

Commit c084c5e

Browse files
authored
feat: fix zip file creation, improve logs (#48)
* feat: fix zip file creation, improve logs * fix test name * use try/catch instead of chaining
1 parent f63fa6d commit c084c5e

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,26 @@ jobs:
155155
env:
156156
DEPLOY_CF_SA_KEY_JSON: ${{ secrets.DEPLOY_CF_SA_KEY_JSON }}
157157
CF_NAME: projects/${{ secrets.DEPLOY_CF_PROJECT_ID }}/locations/us-central1/functions/cf-event-json-${{ github.run_number }}
158-
158+
b64_json_invalid_src:
159+
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name }}
160+
name: with invalid source dir
161+
runs-on: ubuntu-latest
162+
steps:
163+
- uses: actions/checkout@v2
164+
- id: build
165+
name: Build dist
166+
run: |-
167+
npm install
168+
npm run build
169+
- id: deploy
170+
uses: ./
171+
continue-on-error: true
172+
with:
173+
name: cf-http-invalid-${{ github.run_number }}
174+
runtime: nodejs10
175+
entry_point: helloWorld
176+
source_dir: ./foo/bar # invalid source dir
177+
credentials: ${{ secrets.DEPLOY_CF_SA_KEY_B64 }}
178+
- name: Catch failure
179+
run: |-
180+
if [ ${{ steps.deploy.outcome }} != 'failure' ]; then exit 1; fi

src/cloudFunctionClient.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import * as core from '@actions/core';
18+
import * as path from 'path';
19+
import * as os from 'os';
1820
import { GaxiosResponse } from 'gaxios';
1921
import { CloudFunction } from './cloudFunction';
2022
import { uploadSource, zipDir, deleteZipFile } from './util';
@@ -196,13 +198,25 @@ export class CloudFunctionClient {
196198
async deploy(cf: CloudFunction): Promise<cloudfunctions_v1.Schema$Operation> {
197199
const authClient = await this.getAuthClient();
198200
const deployedFunctions = await this.listFunctions();
199-
const zipPath = await zipDir(cf.sourceDir);
201+
const zipPath = path.join(
202+
os.tmpdir(),
203+
`cfsrc-${Math.floor(Math.random() * 100000)}.zip`,
204+
);
205+
try {
206+
await zipDir(cf.sourceDir, zipPath);
207+
} catch (err) {
208+
throw new Error(`Zip file ${zipPath} creation failed: ${err}`);
209+
}
200210
const uploadUrl = await this.getUploadUrl();
201211
if (!uploadUrl.uploadUrl) {
202212
throw new Error('Unable to generate signed Url');
203213
}
204214
// Upload source code
205-
await uploadSource(uploadUrl.uploadUrl, zipPath);
215+
try {
216+
await uploadSource(uploadUrl.uploadUrl, zipPath);
217+
} catch (err) {
218+
throw new Error(`Zip file upload failed: ${err}`);
219+
}
206220
// Delete temp zip file after upload
207221
await deleteZipFile(zipPath);
208222
cf.setSourceUrl(uploadUrl.uploadUrl);

src/util.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as fs from 'fs';
18+
import * as core from '@actions/core';
1819
import { Gaxios } from 'gaxios';
1920
import * as Archiver from 'archiver';
2021

@@ -24,22 +25,41 @@ import * as Archiver from 'archiver';
2425
* @param dirPath Directory to zip.
2526
* @returns filepath of the created zip file.
2627
*/
27-
export async function zipDir(dirPath: string): Promise<string> {
28+
export async function zipDir(
29+
dirPath: string,
30+
outputPath: string,
31+
): Promise<string> {
2832
// Check dirpath
2933
if (!fs.existsSync(dirPath)) {
3034
throw new Error(`Unable to find ${dirPath}`);
3135
}
32-
// Create output file stream
33-
const outputPath = `./cfsrc-${Math.floor(Math.random() * 100000)}.zip`;
34-
const output = fs.createWriteStream(outputPath);
35-
// Init archive
36-
const archive = Archiver.create('zip');
37-
archive.pipe(output);
38-
// Add dir to root of archive
39-
archive.directory(dirPath, false);
40-
// Finish writing files
41-
archive.finalize();
42-
return outputPath;
36+
return new Promise((resolve, reject) => {
37+
// Create output file stream
38+
const output = fs.createWriteStream(outputPath);
39+
output.on('finish', () => {
40+
core.info(`zip file ${outputPath} created successfully`);
41+
resolve(outputPath);
42+
});
43+
// Init archive
44+
const archive = Archiver.create('zip');
45+
// log archive warnings
46+
archive.on('warning', (err: Archiver.ArchiverError) => {
47+
if (err.code === 'ENOENT') {
48+
core.info(err.message);
49+
} else {
50+
reject(err);
51+
}
52+
});
53+
// listen for all archive data to be written
54+
output.on('close', function () {
55+
core.info(`function source zipfile created: ${archive.pointer()} bytes`);
56+
});
57+
archive.pipe(output);
58+
// Add dir to root of archive
59+
archive.directory(dirPath, false);
60+
// Finish writing files
61+
archive.finalize();
62+
});
4363
}
4464

4565
/**
@@ -89,5 +109,6 @@ export async function uploadSource(
89109
`Failed to upload function source code: ${resp.statusText}`,
90110
);
91111
}
112+
core.info(`zip file ${zipPath} uploaded successfully`);
92113
return uploadUrl;
93114
}

0 commit comments

Comments
 (0)