Skip to content

Commit c3aba2c

Browse files
chore: build dist deploy-cloudrun (#32)
1 parent 9de5186 commit c3aba2c

File tree

1 file changed

+92
-40
lines changed

1 file changed

+92
-40
lines changed

dist/index.js

Lines changed: 92 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36386,6 +36386,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3638636386
exports.CloudRun = void 0;
3638736387
const core = __importStar(__webpack_require__(470));
3638836388
const googleapis_1 = __webpack_require__(819);
36389+
const lodash_1 = __webpack_require__(998);
3638936390
/**
3639036391
* Wraps interactions with the Google Cloud Run API.
3639136392
*
@@ -36545,8 +36546,10 @@ class CloudRun {
3654536546
};
3654636547
serviceResponse = yield this.run.namespaces.services.create(createServiceRequest, this.methodOptions);
3654736548
}
36549+
const url = yield this.pollService(serviceResponse.data);
3654836550
core.info(`Service ${service.name} has been successfully deployed.`);
36549-
return serviceResponse.data;
36551+
// return serviceResponse.data;
36552+
return url;
3655036553
});
3655136554
}
3655236555
/**
@@ -36619,8 +36622,68 @@ class CloudRun {
3661936622
yield this.run.projects.locations.services.setIamPolicy(setIamPolicyRequest, this.methodOptions);
3662036623
});
3662136624
}
36625+
/**
36626+
* Poll service revision until ready
36627+
* @param serviceResponse
36628+
* @returns service url or revision url
36629+
*/
36630+
pollService(serviceResponse) {
36631+
return __awaiter(this, void 0, void 0, function* () {
36632+
let url = getUrl(serviceResponse);
36633+
const maxAttempts = 60; // Timeout after 300 seconds
36634+
let attempt = 0;
36635+
// Revision is ready and url is found before timeout
36636+
while (!getReadyStatus(serviceResponse) && !url && attempt < maxAttempts) {
36637+
attempt += 1;
36638+
yield sleep(5000);
36639+
serviceResponse = yield this.getService(serviceResponse.metadata.name);
36640+
url = getUrl(serviceResponse);
36641+
}
36642+
if (!url)
36643+
throw new Error('Timeout error: service revision is not ready.');
36644+
return url;
36645+
});
36646+
}
3662236647
}
3662336648
exports.CloudRun = CloudRun;
36649+
/** Retrieve status of new revision */
36650+
function getReadyStatus(serviceResponse) {
36651+
// Retrieve the revision name
36652+
const revisionName = lodash_1.get(serviceResponse, 'spec.template.metadata.name');
36653+
// Retrieve the revision statuses
36654+
const createdRevision = lodash_1.get(serviceResponse, 'status.latestCreatedRevisionName');
36655+
const latestRevision = lodash_1.get(serviceResponse, 'status.latestReadyRevisionName');
36656+
// Latest created revision must equal latest ready revision
36657+
if (revisionName) {
36658+
// After first deployment, revision name is set
36659+
return (revisionName &&
36660+
createdRevision &&
36661+
latestRevision &&
36662+
revisionName == createdRevision &&
36663+
revisionName == latestRevision);
36664+
}
36665+
else {
36666+
// First deployment will not have a revision name
36667+
return (createdRevision && latestRevision && latestRevision == createdRevision);
36668+
}
36669+
}
36670+
function sleep(ms) {
36671+
return new Promise((resolve) => setTimeout(resolve, ms));
36672+
}
36673+
/** Get service url or tagged revision url */
36674+
function getUrl(serviceResponse) {
36675+
const revisionName = lodash_1.get(serviceResponse, 'spec.template.metadata.name');
36676+
// Find revision url
36677+
const traffic = lodash_1.get(serviceResponse, 'status.traffic');
36678+
let revision;
36679+
if (traffic) {
36680+
revision = traffic.find((revision) => {
36681+
return revision.revisionName == revisionName && revision.url;
36682+
});
36683+
}
36684+
// Or return service url
36685+
return lodash_1.get(revision, 'url') || lodash_1.get(serviceResponse, 'status.url') || '';
36686+
}
3662436687

3662536688

3662636689
/***/ }),
@@ -124607,44 +124670,41 @@ class Service {
124607124670
* @param prevService the previous Cloud Run service revision
124608124671
*/
124609124672
merge(prevService) {
124610-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
124673+
const currentService = lodash_1.cloneDeep(this.request);
124611124674
// Get Revision names if set
124612-
const name = lodash_1.get(this.request, 'spec.template.metadata.name');
124675+
const name = lodash_1.get(currentService, 'spec.template.metadata.name');
124613124676
const previousName = lodash_1.get(prevService, 'spec.template.metadata.name');
124614-
// Merge Revision metadata
124615-
const labels = Object.assign(Object.assign({}, (_c = (_b = (_a = prevService.spec) === null || _a === void 0 ? void 0 : _a.template) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.labels), (_f = (_e = (_d = this.request.spec) === null || _d === void 0 ? void 0 : _d.template) === null || _e === void 0 ? void 0 : _e.metadata) === null || _f === void 0 ? void 0 : _f.labels);
124616-
const annotations = Object.assign(Object.assign({}, (_j = (_h = (_g = prevService.spec) === null || _g === void 0 ? void 0 : _g.template) === null || _h === void 0 ? void 0 : _h.metadata) === null || _j === void 0 ? void 0 : _j.annotations), (_m = (_l = (_k = this.request.spec) === null || _k === void 0 ? void 0 : _k.template) === null || _l === void 0 ? void 0 : _l.metadata) === null || _m === void 0 ? void 0 : _m.annotations);
124617-
this.request.spec.template.metadata = {
124618-
annotations,
124619-
labels,
124620-
};
124621-
// Force update with Revision name change
124622-
this.request.spec.template.metadata.name = this.generateRevisionName(name, previousName);
124623-
// Merge Container spec
124624-
const prevContainer = prevService.spec.template.spec.containers[0];
124625-
const currentContainer = this.request.spec.template.spec.containers[0];
124626-
const container = Object.assign(Object.assign({}, prevContainer), currentContainer);
124627-
// Merge Revision spec
124628-
const spec = Object.assign(Object.assign({}, (_p = (_o = prevService.spec) === null || _o === void 0 ? void 0 : _o.template) === null || _p === void 0 ? void 0 : _p.spec), this.request.spec.template.spec);
124629-
if (!currentContainer.command) {
124630-
// Remove entrypoint cmd and arguments if not specified
124631-
delete container.command;
124632-
delete container.args;
124633-
}
124677+
const generatedName = this.generateRevisionName(name, previousName);
124634124678
// Merge Env vars
124679+
const prevEnvVars = lodash_1.get(prevService, 'spec.template.spec.containers')[0]
124680+
.env;
124681+
const currentEnvVars = lodash_1.get(currentService, 'spec.template.spec.containers')[0].env;
124635124682
let env = [];
124636-
if (currentContainer.env) {
124637-
env = currentContainer.env.map((envVar) => envVar);
124683+
if (currentEnvVars) {
124684+
env = currentEnvVars.map((envVar) => envVar);
124638124685
}
124639124686
const keys = env === null || env === void 0 ? void 0 : env.map((envVar) => envVar.name);
124640-
(_q = prevContainer.env) === null || _q === void 0 ? void 0 : _q.forEach((envVar) => {
124687+
prevEnvVars === null || prevEnvVars === void 0 ? void 0 : prevEnvVars.forEach((envVar) => {
124641124688
if (!keys.includes(envVar.name)) {
124642-
return env.push(envVar);
124689+
// Add old env vars without duplicating
124690+
env.push(envVar);
124643124691
}
124644124692
});
124645-
container.env = env;
124646-
spec.containers = [container];
124647-
this.request.spec.template.spec = spec;
124693+
const newEnv = lodash_1.cloneDeep(env);
124694+
// Deep Merge Service
124695+
const mergedServices = lodash_1.merge(prevService, currentService);
124696+
// Force update with Revision name change
124697+
if (!lodash_1.get(mergedServices, 'spec.template.metadata')) {
124698+
mergedServices.spec.template.metadata = {
124699+
name: generatedName,
124700+
};
124701+
}
124702+
else {
124703+
mergedServices.spec.template.metadata.name = generatedName;
124704+
}
124705+
// Merge Container spec
124706+
mergedServices.spec.template.spec.containers[0].env = newEnv;
124707+
this.request = mergedServices;
124648124708
}
124649124709
generateRevisionName(name, prevName) {
124650124710
const message = 'Resource name must use only lowercase letters, numbers and ' +
@@ -126252,10 +126312,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
126252126312
const core = __importStar(__webpack_require__(470));
126253126313
const cloudRun_1 = __webpack_require__(115);
126254126314
const service_1 = __webpack_require__(304);
126255-
const lodash_1 = __webpack_require__(998);
126256-
function sleep(ms) {
126257-
return new Promise((resolve) => setTimeout(resolve, ms));
126258-
}
126259126315
/**
126260126316
* Executes the main action. It includes the main business logic and is the
126261126317
* primary entry point. It is documented inline.
@@ -126276,13 +126332,9 @@ function run() {
126276126332
// Initialize service
126277126333
const service = new service_1.Service({ image, name, envVars, yaml });
126278126334
// Deploy service
126279-
let serviceResponse = yield client.deploy(service);
126280-
while (!lodash_1.get(serviceResponse, 'status.url')) {
126281-
serviceResponse = yield client.getService(service.name);
126282-
yield sleep(2000);
126283-
}
126335+
const url = yield client.deploy(service);
126284126336
// Set URL as output
126285-
core.setOutput('url', lodash_1.get(serviceResponse, 'status.url'));
126337+
core.setOutput('url', url);
126286126338
}
126287126339
catch (error) {
126288126340
core.setFailed(error.message);

0 commit comments

Comments
 (0)