Skip to content
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
3 changes: 1 addition & 2 deletions src/pages/api/v1/builds/[uuid]/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {

const buildId = build.id;
const runUUID = nanoid();
await buildService.resolveAndDeployBuildQueue.add('resolve-deploy', {
await buildService.enqueueResolveAndDeployBuild({
buildId,
runUUID,
correlationId,
});

getLogger({ stage: LogStage.BUILD_QUEUED }).info('Build: redeploy queued');
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/v1/builds/[uuid]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio

if (build.pullRequest?.deployOnUpdate) {
getLogger({ stage: LogStage.BUILD_QUEUED }).info(`Triggering redeploy after UUID update`);
await new BuildService().resolveAndDeployBuildQueue.add('resolve-deploy', {
await new BuildService().enqueueResolveAndDeployBuild({
buildId: build.id,
runUUID: nanoid(),
correlationId,
});
}

Expand Down
45 changes: 13 additions & 32 deletions src/pages/api/v1/builds/[uuid]/services/[name]/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/

import { NextApiRequest, NextApiResponse } from 'next/types';
import { withLogContext, getLogger, extractContextForQueue, LogStage } from 'server/lib/logger';
import GithubService from 'server/services/github';
import { Build } from 'server/models';
import DeployService from 'server/services/deploy';
import { DeployStatus } from 'shared/constants';
import { withLogContext, getLogger, LogStage } from 'server/lib/logger';
import { nanoid } from 'nanoid';
import BuildService from 'server/services/build';

Expand Down Expand Up @@ -102,55 +98,40 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {

return withLogContext({ correlationId, buildUuid: uuid as string }, async () => {
try {
const githubService = new GithubService();
const build: Build = await githubService.db.models.Build.query()
const buildService = new BuildService();
const build = await buildService.db.models.Build.query()
.findOne({
uuid,
})
.withGraphFetched('deploys.deployable');

const buildId = build.id;

if (!build) {
getLogger().debug(`Build not found`);
return res.status(404).json({ error: `Build not found for ${uuid}` });
}

const deploy = build.deploys.find((deploy) => deploy.deployable.name === name);
const deploy = build.deploys?.find((candidate) => candidate.deployable?.name === name);

if (!deploy) {
if (!deploy?.deployable) {
getLogger().debug(`Deployable not found: service=${name}`);
res.status(404).json({ error: `${name} service is not found in ${uuid} build.` });
return;
}

const githubRepositoryId = deploy.deployable.repositoryId;
const githubRepositoryId =
deploy.deployable.resolvedFromRepositoryId ?? deploy.deployable.repositoryId ?? deploy.githubRepositoryId;
if (githubRepositoryId == null) {
throw new Error(`Cannot redeploy ${name}: source repository is unknown.`);
}

const runUUID = nanoid();
const buildService = new BuildService();
await buildService.resolveAndDeployBuildQueue.add('resolve-deploy', {
buildId,
githubRepositoryId,
await buildService.enqueueResolveAndDeployBuild({
buildId: build.id,
githubRepositoryId: Number(githubRepositoryId),
runUUID,
...extractContextForQueue(),
});

getLogger({ stage: LogStage.BUILD_QUEUED }).info(`Build: service redeploy queued service=${name}`);

const deployService = new DeployService();

await deploy.$query().patchAndFetch({
runUUID,
});

await deployService.patchAndUpdateActivityFeed(
deploy,
{
status: DeployStatus.QUEUED,
},
runUUID,
githubRepositoryId
);
return res.status(200).json({
status: 'success',
message: `Redeploy for service ${name} in build ${uuid} has been queued`,
Expand Down
1 change: 1 addition & 0 deletions src/server/db/migrations/001_seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export async function up(knex: Knex): Promise<any> {
"ingressAnnotations" json DEFAULT '{}'::json,
"appShort" varchar(255) DEFAULT NULL::character varying,
helm json DEFAULT '{}'::json,
"requires" text[] NOT NULL DEFAULT ARRAY[]::text[],
"deploymentDependsOn" text[] DEFAULT ARRAY[]::text[],
builder jsonb DEFAULT '{}'::jsonb,
"ecr" varchar(255) default NULL::character varying,
Expand Down
45 changes: 45 additions & 0 deletions src/server/db/migrations/031_add_deployment_reconciliation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2026 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Knex } from 'knex';

const PENDING_INDEX = 'builds_reconcile_pending';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('builds', (table) => {
table.bigInteger('desiredGeneration').notNullable().defaultTo(0);
table.bigInteger('observedGeneration').notNullable().defaultTo(0);
table.jsonb('acceptedRefs').notNullable().defaultTo('{}');
});

await knex.raw(
`
create index ?? on builds (id)
where "desiredGeneration" > "observedGeneration" and "deletedAt" is null
`,
[PENDING_INDEX]
);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw('drop index if exists ??', [PENDING_INDEX]);

await knex.schema.alterTable('builds', (table) => {
table.dropColumn('acceptedRefs');
table.dropColumn('observedGeneration');
table.dropColumn('desiredGeneration');
});
}
33 changes: 33 additions & 0 deletions src/server/db/migrations/032_add_requires_to_deployables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2026 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
if (await knex.schema.hasColumn('deployables', 'requires')) return;

await knex.schema.alterTable('deployables', (table) => {
table.specificType('requires', 'text[]').notNullable().defaultTo(knex.raw('ARRAY[]::text[]'));
});
}

export async function down(knex: Knex): Promise<void> {
if (!(await knex.schema.hasColumn('deployables', 'requires'))) return;

await knex.schema.alterTable('deployables', (table) => {
table.dropColumn('requires');
});
}
7 changes: 7 additions & 0 deletions src/server/jobs/__tests__/apiEnvironmentJobWiring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ describe('bootstrapJobs API-environment wiring', () => {
const handleApiEnvironmentCreateFailure = jest.fn();
const processApiEnvironmentCreateQueue = jest.fn();
const processApiEnvironmentExpiryQueue = jest.fn();
const processDeploymentReconciliationQueue = jest.fn();
const setupApiEnvironmentExpiryJob = jest.fn().mockResolvedValue(undefined);
const setupDeploymentReconciliationSweep = jest.fn();

const services: any = {
GithubService: { processWebhooks: jest.fn() },
Expand All @@ -78,12 +80,14 @@ describe('bootstrapJobs API-environment wiring', () => {
SitesService: { setupSitesCleanupJob: jest.fn(), processSitesCleanupQueue: jest.fn() },
BuildService: {
setupApiEnvironmentExpiryJob,
setupDeploymentReconciliationSweep,
processApiEnvironmentCreateQueue,
handleApiEnvironmentCreateFailure,
processApiEnvironmentExpiryQueue,
processDeleteQueue: jest.fn(),
processResolveAndDeployBuildQueue: jest.fn(),
processBuildQueue: jest.fn(),
processDeploymentReconciliationQueue,
},
Webhook: { processWebhookQueue: jest.fn() },
Ingress: { createOrUpdateIngressForBuild: jest.fn(), ingressCleanupForBuild: jest.fn() },
Expand All @@ -95,11 +99,14 @@ describe('bootstrapJobs API-environment wiring', () => {

const createWorker = registeredWorkers.find((w) => w.queue === QUEUE_NAMES.API_ENV_CREATE);
const expiryWorker = registeredWorkers.find((w) => w.queue === QUEUE_NAMES.API_ENV_EXPIRY);
const reconciliationWorker = registeredWorkers.find((w) => w.queue === QUEUE_NAMES.DEPLOYMENT_RECONCILIATION);

expect(createWorker?.handler).toBe(processApiEnvironmentCreateQueue);
expect(expiryWorker?.handler).toBe(processApiEnvironmentExpiryQueue);
expect(reconciliationWorker?.handler).toBe(processDeploymentReconciliationQueue);
expect(workerOnHandlers[QUEUE_NAMES.API_ENV_CREATE]?.failed).toBe(handleApiEnvironmentCreateFailure);
expect(setupApiEnvironmentExpiryJob).toHaveBeenCalled();
expect(setupDeploymentReconciliationSweep).toHaveBeenCalled();

const ownerSweepWorker = registeredWorkers.find((w) => w.queue === QUEUE_NAMES.API_TOKEN_OWNER_SWEEP);
expect(ownerSweepWorker?.handler).toBe(processApiTokenOwnerSweep);
Expand Down
10 changes: 10 additions & 0 deletions src/server/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function bootstrapJobs(services: IServices) {
services.BuildService.setupApiEnvironmentExpiryJob().catch((error) =>
getLogger().error({ error }, 'Jobs: api environment expiry setup failed')
);
services.BuildService.setupDeploymentReconciliationSweep();

const apiEnvCreateWorker = queueManager.registerWorker(
QUEUE_NAMES.API_ENV_CREATE,
Expand Down Expand Up @@ -164,6 +165,15 @@ export default function bootstrapJobs(services: IServices) {
concurrency: 125,
});

queueManager.registerWorker(
QUEUE_NAMES.DEPLOYMENT_RECONCILIATION,
services.BuildService.processDeploymentReconciliationQueue,
{
connection: redisClient.getConnection(),
concurrency: 125,
}
);

queueManager.registerWorker(QUEUE_NAMES.GITHUB_DEPLOYMENT, services.GithubService.processGithubDeployment, {
connection: redisClient.getConnection(),
concurrency: 125,
Expand Down
Loading
Loading