From a846531e4388817a969d092147536b7f05559cce Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Thu, 22 Feb 2024 12:50:35 +0530 Subject: [PATCH] Set DNS in application deployment request (#94) Co-authored-by: neeraj --- packages/backend/src/entity/Deployment.ts | 8 ++-- packages/backend/src/registry.ts | 6 +-- packages/backend/src/service.ts | 51 ++++++++++++----------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/backend/src/entity/Deployment.ts b/packages/backend/src/entity/Deployment.ts index 5771b5d..baf5fe8 100644 --- a/packages/backend/src/entity/Deployment.ts +++ b/packages/backend/src/entity/Deployment.ts @@ -87,11 +87,11 @@ export class Deployment { @Column('simple-json') applicationRecordData!: ApplicationRecord; - @Column('varchar') - applicationDeploymentRequestId!: string; + @Column('varchar', { nullable: true }) + applicationDeploymentRequestId!: string | null; - @Column('simple-json') - applicationDeploymentRequestData!: ApplicationDeploymentRequest; + @Column('simple-json', { nullable: true }) + applicationDeploymentRequestData!: ApplicationDeploymentRequest | null; @Column('varchar', { nullable: true }) applicationDeploymentRecordId!: string | null; diff --git a/packages/backend/src/registry.ts b/packages/backend/src/registry.ts index e3bce56..98d34c3 100644 --- a/packages/backend/src/registry.ts +++ b/packages/backend/src/registry.ts @@ -92,9 +92,9 @@ export class Registry { } async createApplicationDeploymentRequest (data: { + deployment: Deployment, appName: string, packageJsonName: string, - commitHash: string, repository: string, environmentVariables: { [key: string]: string } }): Promise<{ @@ -115,9 +115,9 @@ export class Registry { version: '1.0.0', name: `${applicationRecord.attributes.name}@${applicationRecord.attributes.app_version}`, application: `${crn}@${applicationRecord.attributes.app_version}`, + dns: `${data.deployment.project.name}-${data.deployment.id}`, // TODO: Not set in test-progressive-web-app CI - // dns: '$CERC_REGISTRY_DEPLOYMENT_SHORT_HOSTNAME', // deployment: '$CERC_REGISTRY_DEPLOYMENT_CRN', // https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b @@ -127,7 +127,7 @@ export class Registry { meta: JSON.stringify({ note: `Added by Snowball @ ${DateTime.utc().toFormat('EEE LLL dd HH:mm:ss \'UTC\' yyyy')}`, repository: data.repository, - repository_ref: data.commitHash + repository_ref: data.deployment.commitHash }) }; diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index 401e9a1..7829a36 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -344,30 +344,15 @@ export class Service { repoUrl: recordData.repoUrl }); - const environmentVariables = await this.db.getEnvironmentVariablesByProjectId(data.project.id!, { environment: Environment.Production }); - - const environmentVariablesObj = environmentVariables.reduce((acc, env) => { - acc[env.key] = env.value; - - return acc; - }, {} as { [key: string]: string }); - - const { applicationDeploymentRequestId, applicationDeploymentRequestData } = await this.registry.createApplicationDeploymentRequest( - { - appName: repo, - packageJsonName: packageJSON.name, - commitHash: data.commitHash!, - repository: recordData.repoUrl, - environmentVariables: environmentVariablesObj - }); - // Update previous deployment with prod branch domain // TODO: Fix unique constraint error for domain - await this.db.updateDeployment({ - domainId: data.domain?.id - }, { - domain: null - }); + if (data.domain) { + await this.db.updateDeployment({ + domainId: data.domain.id + }, { + domain: null + }); + } const newDeployment = await this.db.addDeployment({ project: data.project, @@ -378,8 +363,6 @@ export class Service { status: DeploymentStatus.Building, applicationRecordId, applicationRecordData, - applicationDeploymentRequestId, - applicationDeploymentRequestData, domain: data.domain, createdBy: Object.assign(new User(), { id: userId @@ -387,6 +370,26 @@ export class Service { }); log(`Created deployment ${newDeployment.id} and published application record ${applicationRecordId}`); + + const environmentVariables = await this.db.getEnvironmentVariablesByProjectId(data.project.id!, { environment: Environment.Production }); + + const environmentVariablesObj = environmentVariables.reduce((acc, env) => { + acc[env.key] = env.value; + + return acc; + }, {} as { [key: string]: string }); + + const { applicationDeploymentRequestId, applicationDeploymentRequestData } = await this.registry.createApplicationDeploymentRequest( + { + deployment: newDeployment, + appName: repo, + packageJsonName: packageJSON.name, + repository: recordData.repoUrl, + environmentVariables: environmentVariablesObj + }); + + await this.db.updateDeploymentById(newDeployment.id, { applicationDeploymentRequestId, applicationDeploymentRequestData }); + return newDeployment; }