-
-
Notifications
You must be signed in to change notification settings - Fork 889
Feat: two phase deployment, version pinning #1739
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
apps/webapp/app/routes/api.v1.deployments.$deploymentVersion.promote.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { authenticateApiRequest } from "~/services/apiAuth.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { ServiceValidationError } from "~/v3/services/baseService.server"; | ||
| import { ChangeCurrentDeploymentService } from "~/v3/services/changeCurrentDeployment.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| deploymentVersion: z.string(), | ||
| }); | ||
|
|
||
| export async function action({ request, params }: ActionFunctionArgs) { | ||
| // Ensure this is a POST request | ||
| if (request.method.toUpperCase() !== "POST") { | ||
| return { status: 405, body: "Method Not Allowed" }; | ||
| } | ||
|
|
||
| const parsedParams = ParamsSchema.safeParse(params); | ||
|
|
||
| if (!parsedParams.success) { | ||
| return json({ error: "Invalid params" }, { status: 400 }); | ||
| } | ||
|
|
||
| // Next authenticate the request | ||
| const authenticationResult = await authenticateApiRequest(request); | ||
|
|
||
| if (!authenticationResult) { | ||
| logger.info("Invalid or missing api key", { url: request.url }); | ||
| return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
| } | ||
|
|
||
| const authenticatedEnv = authenticationResult.environment; | ||
|
|
||
| const { deploymentVersion } = parsedParams.data; | ||
|
|
||
| const deployment = await prisma.workerDeployment.findFirst({ | ||
| where: { | ||
| version: deploymentVersion, | ||
| environmentId: authenticatedEnv.id, | ||
| }, | ||
| }); | ||
|
|
||
| if (!deployment) { | ||
| return json({ error: "Deployment not found" }, { status: 404 }); | ||
| } | ||
|
|
||
| try { | ||
| const service = new ChangeCurrentDeploymentService(); | ||
| await service.call(deployment, "promote"); | ||
|
|
||
| return json( | ||
| { | ||
| id: deployment.friendlyId, | ||
| version: deployment.version, | ||
| shortCode: deployment.shortCode, | ||
| }, | ||
| { status: 200 } | ||
| ); | ||
| } catch (error) { | ||
| if (error instanceof ServiceValidationError) { | ||
| return json({ error: error.message }, { status: 400 }); | ||
| } else { | ||
| return json({ error: "Failed to promote deployment" }, { status: 500 }); | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
apps/webapp/app/routes/resources.$projectId.deployments.$deploymentShortCode.promote.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { parse } from "@conform-to/zod"; | ||
| import { ActionFunction, json } from "@remix-run/node"; | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { requireUserId } from "~/services/session.server"; | ||
| import { ChangeCurrentDeploymentService } from "~/v3/services/changeCurrentDeployment.server"; | ||
|
|
||
| export const rollbackSchema = z.object({ | ||
| redirectUrl: z.string(), | ||
| }); | ||
|
|
||
| const ParamSchema = z.object({ | ||
| projectId: z.string(), | ||
| deploymentShortCode: z.string(), | ||
| }); | ||
|
|
||
| export const action: ActionFunction = async ({ request, params }) => { | ||
| const userId = await requireUserId(request); | ||
| const { projectId, deploymentShortCode } = ParamSchema.parse(params); | ||
|
|
||
| const formData = await request.formData(); | ||
| const submission = parse(formData, { schema: rollbackSchema }); | ||
|
|
||
| if (!submission.value) { | ||
| return json(submission); | ||
| } | ||
|
|
||
| try { | ||
| const project = await prisma.project.findUnique({ | ||
| where: { | ||
| id: projectId, | ||
| organization: { | ||
| members: { | ||
| some: { | ||
| userId, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!project) { | ||
| return redirectWithErrorMessage(submission.value.redirectUrl, request, "Project not found"); | ||
| } | ||
|
|
||
| const deployment = await prisma.workerDeployment.findUnique({ | ||
| where: { | ||
| projectId_shortCode: { | ||
| projectId: project.id, | ||
| shortCode: deploymentShortCode, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!deployment) { | ||
| return redirectWithErrorMessage( | ||
| submission.value.redirectUrl, | ||
| request, | ||
| "Deployment not found" | ||
| ); | ||
| } | ||
|
|
||
| const rollbackService = new ChangeCurrentDeploymentService(); | ||
| await rollbackService.call(deployment, "promote"); | ||
ericallam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return redirectWithSuccessMessage( | ||
| submission.value.redirectUrl, | ||
| request, | ||
| `Promoted deployment version ${deployment.version} to current.` | ||
| ); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| logger.error("Failed to promote deployment", { | ||
| error: { | ||
| name: error.name, | ||
| message: error.message, | ||
| stack: error.stack, | ||
| }, | ||
| }); | ||
| submission.error = { runParam: error.message }; | ||
| return json(submission); | ||
| } else { | ||
| logger.error("Failed to promote deployment", { error }); | ||
| submission.error = { runParam: JSON.stringify(error) }; | ||
| return json(submission); | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.