From ad3eda6d79697f81b09e378350d619195dbb8a27 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Wed, 15 Nov 2023 14:25:18 +0800 Subject: [PATCH 1/2] fix(siteCreate): add redirect rules --- src/services/identity/DeploymentClient.ts | 45 ++++++++++++++++----- src/services/identity/DeploymentsService.ts | 5 ++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/services/identity/DeploymentClient.ts b/src/services/identity/DeploymentClient.ts index 09a13594a..7bac36377 100644 --- a/src/services/identity/DeploymentClient.ts +++ b/src/services/identity/DeploymentClient.ts @@ -79,17 +79,40 @@ class DeploymentClient { generateCreateAppInput = ( repoName: string, - repoUrl: string - ): CreateAppCommandInput => ({ - name: repoName, - accessToken: SYSTEM_GITHUB_TOKEN, - repository: repoUrl, - buildSpec: AMPLIFY_BUILD_SPEC, - environmentVariables: { - JEKYLL_ENV: "development", - }, - customRules: [{ source: "/<*>", target: "/404.html", status: "404" }], - }) + repoUrl: string, + isStagingLite: boolean + ): CreateAppCommandInput => { + const stgLiteRedirectRules = [ + { + source: "/files/<*>", + target: `https://raw.githubusercontent.com/isomerpages/${repoName}/staging/files/<*>`, + status: "200", + }, + { + source: "/images/<*>", + target: `https://raw.githubusercontent.com/isomerpages/${repoName}/staging/images/<*>`, + status: "200", + }, + ] + const defaultRedirectRules = [ + { source: "/<*>", target: "/404.html", status: "404" }, + ] + + const redirectRules = isStagingLite + ? [...stgLiteRedirectRules, ...defaultRedirectRules] + : defaultRedirectRules + + return { + name: repoName, + accessToken: SYSTEM_GITHUB_TOKEN, + repository: repoUrl, + buildSpec: AMPLIFY_BUILD_SPEC, + environmentVariables: { + JEKYLL_ENV: "development", + }, + customRules: redirectRules, + } + } generateCreateBranchInput = ( appId: string, diff --git a/src/services/identity/DeploymentsService.ts b/src/services/identity/DeploymentsService.ts index 9b2dda902..1bd35b34e 100644 --- a/src/services/identity/DeploymentsService.ts +++ b/src/services/identity/DeploymentsService.ts @@ -69,7 +69,7 @@ class DeploymentsService { return this.create({ stagingUrl: Brand.fromString( - `https://staging.${amplifyStagingLiteResult.value.defaultDomain}` + `https://staging-lite.${amplifyStagingLiteResult.value.defaultDomain}` ), productionUrl: Brand.fromString( `https://master.${amplifyStagingResult.value.defaultDomain}` @@ -105,7 +105,8 @@ class DeploymentsService { const createAppOptions = this.deploymentClient.generateCreateAppInput( appName, - repoUrl + repoUrl, + createStagingLite ) // 1. Create Amplify app return this.deploymentClient From 2248039cd84654ba01f0196591aeaed9238dde50 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Thu, 16 Nov 2023 10:29:35 +0800 Subject: [PATCH 2/2] fix(amplify): fix release branch issue --- src/services/identity/DeploymentClient.ts | 25 ++++++++++++---- src/services/identity/DeploymentsService.ts | 32 +++++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/services/identity/DeploymentClient.ts b/src/services/identity/DeploymentClient.ts index 7bac36377..eb7c8598d 100644 --- a/src/services/identity/DeploymentClient.ts +++ b/src/services/identity/DeploymentClient.ts @@ -13,6 +13,9 @@ import { ListJobsCommand, ListJobsCommandOutput, JobSummary, + StartJobCommand, + StartJobCommandOutput, + StartJobCommandInput, } from "@aws-sdk/client-amplify" import { ResultAsync, errAsync, fromPromise, okAsync } from "neverthrow" @@ -77,11 +80,23 @@ class DeploymentClient { this.amplifyClient.send(new ListJobsCommand({ appId, branchName })) ) as ResultAsync - generateCreateAppInput = ( - repoName: string, - repoUrl: string, + sendStartJobCommand = (options: StartJobCommandInput) => + wrap(this.amplifyClient.send(new StartJobCommand(options))) as ResultAsync< + StartJobCommandOutput, + AmplifyError + > + + generateCreateAppInput = ({ + appName, + repoName, + repoUrl, + isStagingLite, + }: { + appName: string + repoUrl: string + repoName: string isStagingLite: boolean - ): CreateAppCommandInput => { + }): CreateAppCommandInput => { const stgLiteRedirectRules = [ { source: "/files/<*>", @@ -103,7 +118,7 @@ class DeploymentClient { : defaultRedirectRules return { - name: repoName, + name: appName, accessToken: SYSTEM_GITHUB_TOKEN, repository: repoUrl, buildSpec: AMPLIFY_BUILD_SPEC, diff --git a/src/services/identity/DeploymentsService.ts b/src/services/identity/DeploymentsService.ts index 1bd35b34e..ee4924e9f 100644 --- a/src/services/identity/DeploymentsService.ts +++ b/src/services/identity/DeploymentsService.ts @@ -98,16 +98,17 @@ class DeploymentsService { createAmplifyAppOnAws = async ( repoName: string, appName: string, - createStagingLite: boolean + isStagingLite: boolean ): Promise> => { const repoUrl = `https://github.com/isomerpages/${repoName}` logger.info(`PublishToAmplify ${repoUrl}`) - const createAppOptions = this.deploymentClient.generateCreateAppInput( + const createAppOptions = this.deploymentClient.generateCreateAppInput({ appName, repoUrl, - createStagingLite - ) + repoName, + isStagingLite, + }) // 1. Create Amplify app return this.deploymentClient .sendCreateApp(createAppOptions) @@ -153,17 +154,38 @@ class DeploymentsService { ) // 3. Create branches - if (createStagingLite) { + if (isStagingLite) { return this.deploymentClient .sendCreateBranch(createStagingLiteBranchInput) + .andThen(() => + this.deploymentClient.sendStartJobCommand({ + appId: amplifyInfo.id, + branchName: "staging-lite", + jobType: "RELEASE", + }) + ) .map(() => amplifyInfo) } return this.deploymentClient .sendCreateBranch(createMasterBranchInput) + .andThen(() => + this.deploymentClient.sendStartJobCommand({ + appId: amplifyInfo.id, + branchName: "master", + jobType: "RELEASE", + }) + ) .map(() => amplifyInfo) .andThen(() => this.deploymentClient .sendCreateBranch(createStagingBranchInput) + .andThen(() => + this.deploymentClient.sendStartJobCommand({ + appId: amplifyInfo.id, + branchName: "staging", + jobType: "RELEASE", + }) + ) .map(() => amplifyInfo) ) })