@@ -36386,6 +36386,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36386
36386
exports.CloudRun = void 0;
36387
36387
const core = __importStar(__webpack_require__(470));
36388
36388
const googleapis_1 = __webpack_require__(819);
36389
+ const lodash_1 = __webpack_require__(998);
36389
36390
/**
36390
36391
* Wraps interactions with the Google Cloud Run API.
36391
36392
*
@@ -36545,8 +36546,10 @@ class CloudRun {
36545
36546
};
36546
36547
serviceResponse = yield this.run.namespaces.services.create(createServiceRequest, this.methodOptions);
36547
36548
}
36549
+ const url = yield this.pollService(serviceResponse.data);
36548
36550
core.info(`Service ${service.name} has been successfully deployed.`);
36549
- return serviceResponse.data;
36551
+ // return serviceResponse.data;
36552
+ return url;
36550
36553
});
36551
36554
}
36552
36555
/**
@@ -36619,8 +36622,68 @@ class CloudRun {
36619
36622
yield this.run.projects.locations.services.setIamPolicy(setIamPolicyRequest, this.methodOptions);
36620
36623
});
36621
36624
}
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
+ }
36622
36647
}
36623
36648
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
+ }
36624
36687
36625
36688
36626
36689
/***/ }),
@@ -124607,44 +124670,41 @@ class Service {
124607
124670
* @param prevService the previous Cloud Run service revision
124608
124671
*/
124609
124672
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) ;
124611
124674
// 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');
124613
124676
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);
124634
124678
// 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;
124635
124682
let env = [];
124636
- if (currentContainer.env ) {
124637
- env = currentContainer.env .map((envVar) => envVar);
124683
+ if (currentEnvVars ) {
124684
+ env = currentEnvVars .map((envVar) => envVar);
124638
124685
}
124639
124686
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) => {
124641
124688
if (!keys.includes(envVar.name)) {
124642
- return env.push(envVar);
124689
+ // Add old env vars without duplicating
124690
+ env.push(envVar);
124643
124691
}
124644
124692
});
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;
124648
124708
}
124649
124709
generateRevisionName(name, prevName) {
124650
124710
const message = 'Resource name must use only lowercase letters, numbers and ' +
@@ -126252,10 +126312,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
126252
126312
const core = __importStar(__webpack_require__(470));
126253
126313
const cloudRun_1 = __webpack_require__(115);
126254
126314
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
- }
126259
126315
/**
126260
126316
* Executes the main action. It includes the main business logic and is the
126261
126317
* primary entry point. It is documented inline.
@@ -126276,13 +126332,9 @@ function run() {
126276
126332
// Initialize service
126277
126333
const service = new service_1.Service({ image, name, envVars, yaml });
126278
126334
// 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);
126284
126336
// Set URL as output
126285
- core.setOutput('url', lodash_1.get(serviceResponse, 'status. url') );
126337
+ core.setOutput('url', url);
126286
126338
}
126287
126339
catch (error) {
126288
126340
core.setFailed(error.message);
0 commit comments