Skip to content
Closed
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
98 changes: 98 additions & 0 deletions apps/cli/src/commands/ci-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ describe("resolveImageRef", () => {
error: "cannot derive image ref (set CI_REGISTRY_IMAGE and CI_COMMIT_SHA)",
});
});

test("uses the repository override in place of CI_REGISTRY_IMAGE", () => {
expect(
resolveImageRef(
{ CI_REGISTRY_IMAGE: "registry.gitlab.com/group/repo" },
{ forge: "gitlab", commitSha: "abc" },
"registry.gitlab.com/toptiere/dcos/product",
),
).toEqual({
ok: true,
value: "registry.gitlab.com/toptiere/dcos/product:abc",
});
});

test("uses the repository override without CI_REGISTRY_IMAGE set", () => {
expect(
resolveImageRef(
{},
{ forge: "github", commitSha: "def" },
"ghcr.io/org/sub/app",
),
).toEqual({ ok: true, value: "ghcr.io/org/sub/app:def" });
});

test("override still requires the forge SHA and names only that var", () => {
expect(
resolveImageRef(
{ CI_REGISTRY_IMAGE: "registry.example/app" },
{ forge: "gitlab", commitSha: undefined },
"registry.example/team/app",
),
).toEqual({
ok: false,
error: "cannot derive image ref (set CI_COMMIT_SHA)",
});
});
});

describe("resolveCiIdentity", () => {
Expand Down Expand Up @@ -537,6 +573,38 @@ describe("resolveCiPreviewIdentity", () => {
});
});

test("resolves the image ref through build.image_repo", async () => {
expect(
await resolveCiPreviewIdentity(
deps(
{
CI_PROJECT_URL: "https://gitlab.com/group/repo",
CI_MERGE_REQUEST_IID: "17",
CI_PIPELINE_SOURCE: "merge_request_event",
CI_COMMIT_SHA: "abc123",
},
{
readTextFile: async (path) =>
path.endsWith(".sprout.yaml")
? `slug: myapp
build:
image_repo: registry.gitlab.com/toptiere/dcos/product
preview:
hostname: "pr-{pr_id}.example.com"
`
: null,
},
),
),
).toMatchObject({
ok: true,
value: {
imageRef: "registry.gitlab.com/toptiere/dcos/product:abc123",
hostname: "pr-17.example.com",
},
});
});

test("errors when image ref cannot be derived", async () => {
expect(
await resolveCiPreviewIdentity(
Expand Down Expand Up @@ -614,6 +682,36 @@ seed:
});
});

test("resolves push refs under build.image_repo when set", async () => {
expect(
await resolveCiPushedImageRefs(
deps(
env,
{
readTextFile: async (path) =>
path.endsWith(".sprout.yaml")
? `slug: myapp
build:
image_repo: registry.gitlab.com/toptiere/dcos/product
preview:
hostname: "pr-{pr_id}.example.com"
seed:
dockerfile: Dockerfile.seed
`
: null,
},
),
identity,
),
).toEqual({
refs: [
"registry.gitlab.com/toptiere/dcos/product:abc123",
"registry.gitlab.com/toptiere/dcos/product:abc123-seed",
],
warnings: [],
});
});

test("never treats content-addressed seed tags as cleanup targets", async () => {
const result = await resolveCiPushedImageRefs(
deps(
Expand Down
28 changes: 21 additions & 7 deletions apps/cli/src/commands/ci-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,18 @@ export function requireCiSource(env: NodeJS.ProcessEnv): Result<CiSource> {
export function resolveImageRef(
env: NodeJS.ProcessEnv,
identity: Pick<CiIdentity, "forge" | "commitSha">,
imageRepo?: string,
): Result<string> {
const registry = env.CI_REGISTRY_IMAGE?.trim();
const override = imageRepo?.trim();
const registry = override || env.CI_REGISTRY_IMAGE?.trim();
const sha = identity.commitSha;
if (!registry || !sha) {
const shaVar = commitShaEnvVar(identity.forge);
return {
ok: false,
error: `cannot derive image ref (set CI_REGISTRY_IMAGE and ${shaVar})`,
error: override
? `cannot derive image ref (set ${shaVar})`
: `cannot derive image ref (set CI_REGISTRY_IMAGE and ${shaVar})`,
};
}
return { ok: true, value: `${registry}:${sha}` };
Expand Down Expand Up @@ -149,12 +153,16 @@ export async function resolveCiPreviewIdentity(
const base = await resolveCiIdentity(deps);
if (!base.ok) return base;

const imageRef = resolveImageRef(deps.env, base.value);
if (!imageRef.ok) return imageRef;

const yaml = await loadYaml(deps);
if (!yaml.ok) return yaml;

const imageRef = resolveImageRef(
deps.env,
base.value,
yaml.value.build?.image_repo,
);
if (!imageRef.ok) return imageRef;

const hostname = resolveDeployHostname(
yaml.value.preview.hostname,
base.value.prId,
Expand Down Expand Up @@ -183,10 +191,16 @@ export async function resolveCiPushedImageRefs(
deps: CliDeps,
identity: CiIdentity,
): Promise<CiPushedImageRefs> {
const app = resolveImageRef(deps.env, identity);
const yaml = await loadYaml(deps);
// An unreadable manifest cannot name an override repository, so fall back to
// the registry root exactly as a repo without `build.image_repo` does.
const app = resolveImageRef(
deps.env,
identity,
yaml.ok ? yaml.value.build?.image_repo : undefined,
);
if (!app.ok) return { refs: [], warnings: [] };

const yaml = await loadYaml(deps);
if (!yaml.ok) {
return {
refs: [app.value],
Expand Down
46 changes: 46 additions & 0 deletions apps/cli/src/commands/ci-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ health:
expect: 200
`;

const OVERRIDE_YAML = `slug: myapp
build:
dockerfile: Dockerfile
image_repo: registry.gitlab.com/toptiere/dcos/product
seed:
dockerfile: Dockerfile.seed
preview:
hostname: "pr-{pr_id}.myapp.preview.example.com"
health:
path: /health
interval: 2s
timeout: 120s
expect: 200
`;

const OVERRIDE_APP_REF = "registry.gitlab.com/toptiere/dcos/product:abc123";
const OVERRIDE_SEED_REF = `${OVERRIDE_APP_REF}-seed`;

const SEEDED_INPUTS_YAML = `slug: myapp
build:
dockerfile: Dockerfile
Expand Down Expand Up @@ -218,6 +236,34 @@ describe("sprout ci preview", () => {
});
});

test("build.image_repo pushes app and seed to the override repository", async () => {
const baseUrl = healthyGateway();
const cwd = await withWorkspace(OVERRIDE_YAML, {
"Dockerfile.seed": SEED_DOCKERFILE,
});
const code = await runCli(
["ci", "preview"],
deps({
cwd,
env: { SPROUT_URL: baseUrl, SPROUT_TOKEN: "t", ...GITLAB_MR_ENV },
readTextFile: readRealFile,
}),
);
expect(code).toBe(0);
expect(stderr).toEqual([]);
expect(dockerCalls).toEqual([
["docker", "build", "-f", "Dockerfile.seed", "-t", OVERRIDE_SEED_REF, "."],
["docker", "push", OVERRIDE_SEED_REF],
["docker", "build", "-f", "Dockerfile", "-t", OVERRIDE_APP_REF, "."],
["docker", "push", OVERRIDE_APP_REF],
]);
expect(captured).toHaveLength(1);
expect(captured[0]?.body).toMatchObject({
app_image: OVERRIDE_APP_REF,
seed_image: OVERRIDE_SEED_REF,
});
});

test("without a seed block only the app image is built, no -s", async () => {
const baseUrl = healthyGateway();
const cwd = await withWorkspace(MINIMAL_YAML);
Expand Down
10 changes: 7 additions & 3 deletions apps/cli/src/commands/ci-reseed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ export async function runCiReseed(
return fail(ctx.deps.io, "ci reseed requires -s <seed-image>");
}

const imageRef = resolveImageRef(ctx.deps.env, identity);
if (!imageRef.ok) return fail(ctx.deps.io, imageRef.error);

const yaml = await loadYaml(ctx.deps);
if (!yaml.ok) return fail(ctx.deps.io, yaml.error);

const imageRef = resolveImageRef(
ctx.deps.env,
identity,
yaml.value.build?.image_repo,
);
if (!imageRef.ok) return fail(ctx.deps.io, imageRef.error);

const assembled = buildReseedRequest(yaml.value, identity, {
appImage: imageRef.value,
seedImage: flags.value.seedImage,
Expand Down
41 changes: 41 additions & 0 deletions apps/cli/src/commands/ci-reset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ health:
expect: 200
`;

const OVERRIDE_YAML = `slug: myapp
build:
dockerfile: Dockerfile
image_repo: registry.gitlab.com/toptiere/dcos/product
seed:
dockerfile: Dockerfile.seed
preview:
hostname: "pr-{pr_id}.myapp.preview.example.com"
health:
path: /health
interval: 2s
timeout: 120s
expect: 200
`;

const GITLAB_MR_ENV = {
CI_PROJECT_URL: "https://gitlab.com/group/repo",
CI_MERGE_REQUEST_IID: "17",
Expand Down Expand Up @@ -219,6 +234,32 @@ describe("sprout ci reset", () => {
expect(posted).toContain("data wiped");
});

test("with build.image_repo deploys from the override refs without rebuilding", async () => {
const baseUrl = resetGateway();
const cwd = await withWorkspace(OVERRIDE_YAML, {
"Dockerfile.seed": "FROM oven/bun:1.4.0\n",
});
const code = await runCli(
["ci", "reset"],
deps({
cwd,
env: { SPROUT_URL: baseUrl, SPROUT_TOKEN: "t", ...GITLAB_MR_ENV },
readTextFile: readRealFile,
fetchFn: async (_url, init) =>
(init?.method ?? "GET") === "GET"
? Response.json([])
: Response.json({ id: 1 }),
}),
);
expect(code).toBe(0);
expect(stderr).toEqual([]);
expect(dockerCalls).toEqual([]);
expect(captured[1]?.body).toMatchObject({
app_image: "registry.gitlab.com/toptiere/dcos/product:abc123",
seed_image: "registry.gitlab.com/toptiere/dcos/product:abc123-seed",
});
});

test("second reset on a healthy preview succeeds", async () => {
const baseUrl = resetGateway();
const cwd = await withWorkspace(MINIMAL_YAML);
Expand Down
53 changes: 53 additions & 0 deletions apps/cli/src/commands/ci-teardown-reseed-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,59 @@ health:
expect(captured[0]?.body).not.toHaveProperty("services");
});

test("reseed deploys the app image from build.image_repo", async () => {
const baseUrl = startGateway(async (req, url) => {
if (url.pathname === "/v1/deploy" && req.method === "POST") {
captured.push({
method: req.method,
path: url.pathname,
body: await req.json(),
authorization: req.headers.get("authorization"),
});
}
return Response.json({
ok: true,
status: "running",
preview_url: "https://pr-17.myapp.preview.example.com",
canonical_repo_id: "https://gitlab.com/group/repo",
pr_id: 17,
slug: "myapp",
db_name: "sprout_myapp_pr17",
hostname: "pr-17.myapp.preview.example.com",
});
});
const cwd = await withWorkspace(`slug: myapp
build:
image_repo: registry.gitlab.com/toptiere/dcos/product
preview:
hostname: "pr-{pr_id}.myapp.preview.example.com"
health:
path: /health
interval: 2s
timeout: 120s
expect: 200
`);
const code = await runCli(
[
"ci",
"reseed",
"-s",
"registry.gitlab.com/toptiere/dcos/product:abc123-seed",
],
deps({
cwd,
env: { SPROUT_URL: baseUrl, SPROUT_TOKEN: "t", ...GITLAB_MR_ENV },
readTextFile: async (path) => Bun.file(path).text(),
}),
);
expect(code).toBe(0);
expect(captured).toHaveLength(1);
expect(captured[0]?.body).toMatchObject({
app_image: "registry.gitlab.com/toptiere/dcos/product:abc123",
seed_image: "registry.gitlab.com/toptiere/dcos/product:abc123-seed",
});
});

test("reseed reads SPROUT_APP_ENV blob and --seed-env-file like deploy", async () => {
let polls = 0;
const baseUrl = startGateway(async (req, url) => {
Expand Down
Loading
Loading