setup-gcloud/appengine-deploy/src/main.ts
averikitsch d153f05d28 Add appengine-deploy action and refactor setup-gcloud
initial

Add test app and tests

add license

Update ReadMe and runner

Fix lint errors

Missed fixes

Update tests to run integration on PR

rename pkg -> setupGloudSDK

Update ts congig

Remove project id requirement

Update gcloud set project id func

Add unit tests for pkg

Add set project to action

rebuild

Fix gcloud typo

lint pkg and update project id dependency

Update tmp JSON writer

Add cleanup to integration tests

Update get input validation

fix json string

Fix test order and version name

add project id to test

remove clean up step

Update project url

Add no promote flag

test stderr vs stdout

Update pkg build

refactor setup-gcloud

clean up

Fix tool command for isAuthenticated

debugging

test command

Fix isInstalled method

Clean up
2020-04-12 16:23:05 -07:00

108 lines
3.2 KiB
TypeScript

/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as setupGcloud from '../../setupGcloudSDK/dist/index';
async function run(): Promise<void> {
try {
// Get action inputs.
let projectId = core.getInput('project_id');
const deliverables = core.getInput('deliverables');
const imageUrl = core.getInput('image-url');
const version = core.getInput('version');
const promote = core.getInput('promote');
const serviceAccountKey = core.getInput('credentials');
// Install gcloud if not already installed.
if (!setupGcloud.isInstalled()) {
const gcloudVersion = await setupGcloud.getLatestGcloudSDKVersion();
await setupGcloud.installGcloudSDK(gcloudVersion);
}
// Fail if no Project Id is provided.
if (projectId === '' && serviceAccountKey === '') {
core.setFailed('No project Id provided.');
}
// Authenticate gcloud SDK.
if (serviceAccountKey) {
await setupGcloud.authenticateGcloudSDK(serviceAccountKey);
// Set and retrieve Project Id if not provided
if (projectId === '') {
projectId = await setupGcloud.setProject(serviceAccountKey);
}
}
if (!setupGcloud.isAuthenticated()) {
core.setFailed('Error authenticating the Cloud SDK.');
}
const toolCommand = setupGcloud.getToolCommand();
// Create app engine gcloud cmd.
const appDeployCmd = ['app', 'deploy', '--quiet', deliverables];
// Add gcloud flags.
if (projectId !== '') {
appDeployCmd.push('--project', projectId);
}
if (imageUrl !== '') {
appDeployCmd.push('--image-url', imageUrl);
}
if (version !== '') {
appDeployCmd.push('--version', version);
}
if (promote === '' || String(promote).toLowerCase() === 'true') {
appDeployCmd.push('--promote');
} else {
appDeployCmd.push('--no-promote');
}
// Get output of gcloud cmd.
let output = '';
const stdout = (data: Buffer): void => {
output += data.toString();
};
const options = {
listeners: {
stderr: stdout,
},
};
// Run gcloud cmd.
await exec.exec(toolCommand, appDeployCmd, options);
// Set url as output.
const urlMatch = output.match(
/https:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.com/,
);
if (urlMatch) {
const url = urlMatch[0];
core.setOutput('url', url);
} else {
core.info(
'Can not find URL, defaulting to https://PROJECT_ID.appspot.com',
);
core.setOutput('url', `https://${projectId}.appspot.com`);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();