Skip to content

Commit

Permalink
feat: fix zip file creation, improve logs (#48)
Browse files Browse the repository at this point in the history
* feat: fix zip file creation, improve logs

* fix test name

* use try/catch instead of chaining
  • Loading branch information
bharathkkb committed Feb 25, 2021
1 parent f63fa6d commit c084c5e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
24 changes: 23 additions & 1 deletion .github/workflows/deploy-cf-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,26 @@ jobs:
env:
DEPLOY_CF_SA_KEY_JSON: ${{ secrets.DEPLOY_CF_SA_KEY_JSON }}
CF_NAME: projects/${{ secrets.DEPLOY_CF_PROJECT_ID }}/locations/us-central1/functions/cf-event-json-${{ github.run_number }}

b64_json_invalid_src:
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name }}
name: with invalid source dir
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: build
name: Build dist
run: |-
npm install
npm run build
- id: deploy
uses: ./
continue-on-error: true
with:
name: cf-http-invalid-${{ github.run_number }}
runtime: nodejs10
entry_point: helloWorld
source_dir: ./foo/bar # invalid source dir
credentials: ${{ secrets.DEPLOY_CF_SA_KEY_B64 }}
- name: Catch failure
run: |-
if [ ${{ steps.deploy.outcome }} != 'failure' ]; then exit 1; fi
18 changes: 16 additions & 2 deletions src/cloudFunctionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import * as core from '@actions/core';
import * as path from 'path';
import * as os from 'os';
import { GaxiosResponse } from 'gaxios';
import { CloudFunction } from './cloudFunction';
import { uploadSource, zipDir, deleteZipFile } from './util';
Expand Down Expand Up @@ -196,13 +198,25 @@ export class CloudFunctionClient {
async deploy(cf: CloudFunction): Promise<cloudfunctions_v1.Schema$Operation> {
const authClient = await this.getAuthClient();
const deployedFunctions = await this.listFunctions();
const zipPath = await zipDir(cf.sourceDir);
const zipPath = path.join(
os.tmpdir(),
`cfsrc-${Math.floor(Math.random() * 100000)}.zip`,
);
try {
await zipDir(cf.sourceDir, zipPath);
} catch (err) {
throw new Error(`Zip file ${zipPath} creation failed: ${err}`);
}
const uploadUrl = await this.getUploadUrl();
if (!uploadUrl.uploadUrl) {
throw new Error('Unable to generate signed Url');
}
// Upload source code
await uploadSource(uploadUrl.uploadUrl, zipPath);
try {
await uploadSource(uploadUrl.uploadUrl, zipPath);
} catch (err) {
throw new Error(`Zip file upload failed: ${err}`);
}
// Delete temp zip file after upload
await deleteZipFile(zipPath);
cf.setSourceUrl(uploadUrl.uploadUrl);
Expand Down
45 changes: 33 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as fs from 'fs';
import * as core from '@actions/core';
import { Gaxios } from 'gaxios';
import * as Archiver from 'archiver';

Expand All @@ -24,22 +25,41 @@ import * as Archiver from 'archiver';
* @param dirPath Directory to zip.
* @returns filepath of the created zip file.
*/
export async function zipDir(dirPath: string): Promise<string> {
export async function zipDir(
dirPath: string,
outputPath: string,
): Promise<string> {
// Check dirpath
if (!fs.existsSync(dirPath)) {
throw new Error(`Unable to find ${dirPath}`);
}
// Create output file stream
const outputPath = `./cfsrc-${Math.floor(Math.random() * 100000)}.zip`;
const output = fs.createWriteStream(outputPath);
// Init archive
const archive = Archiver.create('zip');
archive.pipe(output);
// Add dir to root of archive
archive.directory(dirPath, false);
// Finish writing files
archive.finalize();
return outputPath;
return new Promise((resolve, reject) => {
// Create output file stream
const output = fs.createWriteStream(outputPath);
output.on('finish', () => {
core.info(`zip file ${outputPath} created successfully`);
resolve(outputPath);
});
// Init archive
const archive = Archiver.create('zip');
// log archive warnings
archive.on('warning', (err: Archiver.ArchiverError) => {
if (err.code === 'ENOENT') {
core.info(err.message);
} else {
reject(err);
}
});
// listen for all archive data to be written
output.on('close', function () {
core.info(`function source zipfile created: ${archive.pointer()} bytes`);
});
archive.pipe(output);
// Add dir to root of archive
archive.directory(dirPath, false);
// Finish writing files
archive.finalize();
});
}

/**
Expand Down Expand Up @@ -89,5 +109,6 @@ export async function uploadSource(
`Failed to upload function source code: ${resp.statusText}`,
);
}
core.info(`zip file ${zipPath} uploaded successfully`);
return uploadUrl;
}

0 comments on commit c084c5e

Please sign in to comment.