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
10 changes: 5 additions & 5 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: sprout preview (reusable)
# Thin bootstrapper for GitHub pull-request previews: installs the pinned,
# checksum-verified `sprout` binary and calls `sprout ci preview` /
# `sprout ci teardown`. All preview behaviour (image build and push, seed
# image, deploy, PR comment, dotenv emission, log dump on failure) lives in
# the CLI. A reset is an ordinary action: run `sprout ci reset` from a
# hand-rolled job or laptop (same env as below) — no ticked box, no marker
# token, no server-side bookkeeping.
# image, deploy, PR comment, dotenv emission, teardown image-tag cleanup,
# log dump on failure) lives in the CLI. A reset is an ordinary action: run
# `sprout ci reset` from a hand-rolled job or laptop (same env as below) —
# no ticked box, no marker token, no server-side bookkeeping.
#
# Caller (`.github/workflows/sprout.yml` in the adopting repo):
#
Expand Down Expand Up @@ -154,7 +154,6 @@ jobs:

- name: Prepare image ref and env blobs
id: prep
if: github.event.action != 'closed'
env:
APP_ENV_BLOB: ${{ secrets.SPROUT_APP_ENV }}
SEED_ENV_BLOB: ${{ secrets.SPROUT_SEED_ENV }}
Expand Down Expand Up @@ -203,6 +202,7 @@ jobs:
SPROUT_URL: ${{ secrets.SPROUT_URL }}
SPROUT_TOKEN: ${{ secrets.SPROUT_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
GHCR_DELETE_TOKEN: ${{ secrets.REGISTRY_PASSWORD }}
run: |
set -eu
sprout ci teardown
110 changes: 110 additions & 0 deletions apps/cli/src/commands/ci-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
requireCiSource,
resolveCiIdentity,
resolveCiPreviewIdentity,
resolveCiPushedImageRefs,
resolveImageRef,
} from "./ci-identity.ts";
import type { CliDeps } from "../context.ts";
Expand Down Expand Up @@ -437,6 +438,37 @@ describe("resolveCiIdentity", () => {
});
});

test("GitHub locks the pull request head SHA so teardown matches the preview build", async () => {
const event = {
pull_request: { number: 42, head: { sha: "headsha" } },
};
expect(
await resolveCiIdentity(
deps(
{
GITHUB_REPOSITORY: "org/repo",
GITHUB_EVENT_NAME: "pull_request",
GITHUB_EVENT_PATH: "/event.json",
GITHUB_SHA: "basesha",
},
{
readTextFile: async (path) =>
path === "/event.json" ? JSON.stringify(event) : null,
},
),
),
).toEqual({
ok: true,
value: {
forge: "github",
repo: "https://github.com/org/repo",
prId: 42,
pipelineSource: "pull_request",
commitSha: "headsha",
},
});
});

test("leaves commitSha undefined when the forge's var is missing", async () => {
expect(
await resolveCiIdentity(
Expand Down Expand Up @@ -540,3 +572,81 @@ describe("resolveCiPreviewIdentity", () => {
});
});
});

describe("resolveCiPushedImageRefs", () => {
const identity = {
forge: "gitlab" as const,
pipelineSource: "merge_request_event" as const,
repo: "https://gitlab.com/group/repo",
prId: 17,
commitSha: "abc123",
};
const env = {
CI_REGISTRY_IMAGE: "registry.gitlab.com/group/repo",
CI_COMMIT_SHA: "abc123",
};

test("resolves the app ref and the commit-scoped seed ref", async () => {
expect(
await resolveCiPushedImageRefs(
deps(
env,
{
readTextFile: async (path) =>
path.endsWith(".sprout.yaml")
? `slug: myapp
preview:
hostname: "pr-{pr_id}.example.com"
seed:
dockerfile: Dockerfile.seed
`
: null,
},
),
identity,
),
).toEqual({
refs: [
"registry.gitlab.com/group/repo:abc123",
"registry.gitlab.com/group/repo:abc123-seed",
],
warnings: [],
});
});

test("never treats content-addressed seed tags as cleanup targets", async () => {
const result = await resolveCiPushedImageRefs(
deps(
env,
{
readTextFile: async (path) => {
if (path.endsWith(".sprout.yaml")) {
return `slug: myapp
preview:
hostname: "pr-{pr_id}.example.com"
seed:
dockerfile: Dockerfile.seed
inputs:
- Dockerfile.seed
`;
}
if (path.endsWith("Dockerfile.seed")) return "FROM oven/bun:1\n";
return null;
},
},
),
identity,
);
expect(result.warnings).toEqual([]);
expect(result.refs).toEqual(["registry.gitlab.com/group/repo:abc123"]);
});

test("skips silently when the commit's image ref cannot be derived", async () => {
expect(
await resolveCiPushedImageRefs(
deps({ CI_COMMIT_SHA: "abc123" }, { readTextFile: async () => null }),
identity,
),
).toEqual({ refs: [], warnings: [] });
});
});
54 changes: 52 additions & 2 deletions apps/cli/src/commands/ci-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
commitShaEnvVar,
resolveCommitSha,
resolvePrId,
resolvePullRequestHeadSha,
resolveRepoForForge,
} from "../identity.ts";
import { resolveDeployHostname } from "../hostname.ts";
import type { Result } from "../result.ts";
import { resolveSeedTarget } from "./seed-image.ts";

export type CiSource =
| { forge: "gitlab"; pipelineSource: "merge_request_event" }
Expand Down Expand Up @@ -107,28 +109,36 @@ export async function resolveCiIdentity(
if (!repo.ok) return repo;

let prId: Result<number>;
let eventPayload: unknown;
if (source.value.forge === "gitlab") {
prId = resolvePrId({ env: deps.env, forge: "gitlab" });
} else {
const event = await loadEventPayload(deps);
if (!event.ok) return event;
eventPayload = event.value;
prId = resolvePrId({
env: deps.env,
eventPayload: event.value,
eventPayload,
forge: "github",
});
}
if (!prId.ok) {
return prId;
}

const commitSha =
source.value.forge === "github"
? resolvePullRequestHeadSha(eventPayload) ??
resolveCommitSha(deps.env, "github")
: resolveCommitSha(deps.env, "gitlab");

return {
ok: true,
value: {
...source.value,
repo: repo.value,
prId: prId.value,
commitSha: resolveCommitSha(deps.env, source.value.forge),
commitSha,
},
};
}
Expand Down Expand Up @@ -162,3 +172,43 @@ export async function resolveCiPreviewIdentity(
},
};
}

export type CiPushedImageRefs = {
refs: string[];
warnings: string[];
};

/** Re-resolves the exact app + per-run seed refs the preview build pushed for this commit. */
export async function resolveCiPushedImageRefs(
deps: CliDeps,
identity: CiIdentity,
): Promise<CiPushedImageRefs> {
const app = resolveImageRef(deps.env, identity);
if (!app.ok) return { refs: [], warnings: [] };

const yaml = await loadYaml(deps);
if (!yaml.ok) {
return {
refs: [app.value],
warnings: [`registry cleanup: seed tag not resolved (${yaml.error})`],
};
}
if (!yaml.value.seed) return { refs: [app.value], warnings: [] };

const seed = await resolveSeedTarget(
yaml.value.seed,
app.value,
deps.cwd,
deps.readTextFile,
);
if (!seed.ok) {
return {
refs: [app.value],
warnings: [`registry cleanup: seed tag not resolved (${seed.error})`],
};
}
// Content-addressed seed tags are shared cache across previews, so only the
// commit-scoped seed tag is a per-run artifact this teardown may delete.
if (seed.value.allowReuse) return { refs: [app.value], warnings: [] };
return { refs: [app.value, seed.value.ref], warnings: [] };
}
8 changes: 6 additions & 2 deletions apps/cli/src/commands/ci-teardown-reseed-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ describe("sprout ci teardown", () => {
}),
);
expect(code).toBe(0);
expect(stderr).toEqual([]);
expect(stderr).toEqual([
"warning: registry tag cleanup skipped: no GitLab token available (set GITLAB_TOKEN with api scope, or run in a CI job with CI_JOB_TOKEN)",
]);
expect(captured).toEqual([
{
method: "POST",
Expand All @@ -130,7 +132,9 @@ describe("sprout ci teardown", () => {
}),
);
expect(code).toBe(0);
expect(stderr).toEqual([]);
expect(stderr).toEqual([
"warning: registry tag cleanup skipped: no GitLab token available (set GITLAB_TOKEN with api scope, or run in a CI job with CI_JOB_TOKEN)",
]);
});

test("surfaces the gateway error code on failure", async () => {
Expand Down
Loading
Loading