Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(siteCreate): add redirect rules #1036

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions src/services/identity/DeploymentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
ListJobsCommand,
harishv7 marked this conversation as resolved.
Show resolved Hide resolved
ListJobsCommandOutput,
JobSummary,
StartJobCommand,
StartJobCommandOutput,
StartJobCommandInput,
} from "@aws-sdk/client-amplify"
import { ResultAsync, errAsync, fromPromise, okAsync } from "neverthrow"

Expand Down Expand Up @@ -77,19 +80,54 @@ class DeploymentClient {
this.amplifyClient.send(new ListJobsCommand({ appId, branchName }))
) as ResultAsync<ListJobsCommandOutput, AmplifyError>

generateCreateAppInput = (
repoName: string,
sendStartJobCommand = (options: StartJobCommandInput) =>
wrap(this.amplifyClient.send(new StartJobCommand(options))) as ResultAsync<
StartJobCommandOutput,
AmplifyError
>

generateCreateAppInput = ({
appName,
repoName,
repoUrl,
isStagingLite,
}: {
appName: 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" }],
})
repoName: 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: appName,
accessToken: SYSTEM_GITHUB_TOKEN,
repository: repoUrl,
buildSpec: AMPLIFY_BUILD_SPEC,
environmentVariables: {
JEKYLL_ENV: "development",
},
customRules: redirectRules,
}
}

generateCreateBranchInput = (
appId: string,
Expand Down
35 changes: 29 additions & 6 deletions src/services/identity/DeploymentsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -98,15 +98,17 @@ class DeploymentsService {
createAmplifyAppOnAws = async (
repoName: string,
appName: string,
createStagingLite: boolean
isStagingLite: boolean
): Promise<Result<AmplifyInfo, AmplifyError>> => {
const repoUrl = `https://github.com/isomerpages/${repoName}`
logger.info(`PublishToAmplify ${repoUrl}`)

const createAppOptions = this.deploymentClient.generateCreateAppInput(
const createAppOptions = this.deploymentClient.generateCreateAppInput({
appName,
repoUrl
)
repoUrl,
repoName,
isStagingLite,
})
// 1. Create Amplify app
return this.deploymentClient
.sendCreateApp(createAppOptions)
Expand Down Expand Up @@ -152,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({
harishv7 marked this conversation as resolved.
Show resolved Hide resolved
appId: amplifyInfo.id,
branchName: "staging",
jobType: "RELEASE",
})
)
.map(() => amplifyInfo)
)
})
Expand Down
Loading