Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

refactor(eve): publish package tarballs with Vercel OIDC (#3360)

Andrew Barbad5a4ab2abaca
diff --git a/.github/workflows/build-tarball.yml b/.github/workflows/build-tarball.yml
deleted file mode 100644
index 60eb464b5..000000000
--- a/.github/workflows/build-tarball.yml
+++ /dev/null
@@ -1,75 +0,0 @@
-name: Build tarball
-
-on:
- push:
- branches: [main]
- pull_request:
- branches: [main]
-
-permissions:
- contents: read
-
-jobs:
- build:
- if: >-
- github.event_name == 'push' ||
- github.event.pull_request.head.repo.full_name == github.repository
- runs-on: ubuntu-latest
- steps:
- - name: Select source
- id: source
- env:
- EVENT_NAME: ${{ github.event_name }}
- MAIN_SHA: ${{ github.sha }}
- PR_NUMBER: ${{ github.event.pull_request.number }}
- PR_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }}
- PR_SHA: ${{ github.event.pull_request.head.sha }}
- run: |
- if [ "$EVENT_NAME" = "push" ]; then
- {
- echo "repository=${{ github.repository }}"
- echo "sha=$MAIN_SHA"
- echo "ref=main"
- } >> "$GITHUB_OUTPUT"
- else
- {
- echo "repository=$PR_REPOSITORY"
- echo "sha=$PR_SHA"
- echo "ref=$PR_NUMBER"
- } >> "$GITHUB_OUTPUT"
- fi
-
- - name: Checkout source
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- with:
- repository: ${{ steps.source.outputs.repository }}
- ref: ${{ steps.source.outputs.sha }}
- persist-credentials: false
-
- - name: Setup pnpm
- uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10
-
- - name: Setup Node.js
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
- with:
- node-version-file: .nvmrc
- cache: pnpm
-
- - name: Install dependencies
- run: pnpm install --frozen-lockfile
-
- - name: Build package artifact
- env:
- EVE_PACKAGE_OUTPUT_DIRECTORY: ${{ github.workspace }}/package-artifact
- EVE_PACKAGE_PUBLIC_ORIGIN: https://pkg.eve.dev
- EVE_PACKAGE_REF: ${{ steps.source.outputs.ref }}
- EVE_PACKAGE_SOURCE_SHA: ${{ steps.source.outputs.sha }}
- run: pnpm --filter eve-package-artifacts package:prepare
-
- - name: Upload package artifact
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
- with:
- name: eve-package
- path: package-artifact
- if-no-files-found: error
- retention-days: 7
diff --git a/.github/workflows/publish-tarball.yml b/.github/workflows/publish-tarball.yml
deleted file mode 100644
index efa84335e..000000000
--- a/.github/workflows/publish-tarball.yml
+++ /dev/null
@@ -1,135 +0,0 @@
-name: Publish tarball
-
-on:
- workflow_run:
- workflows: [Build tarball]
- types: [completed]
-
-permissions:
- actions: read
- contents: read
- pull-requests: read
- statuses: write
-
-jobs:
- report-build-failure:
- if: >-
- github.event.workflow_run.head_repository.full_name == github.repository &&
- github.event.workflow_run.conclusion != 'success' &&
- (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'pull_request')
- runs-on: ubuntu-latest
- steps:
- - name: Report publication failure
- uses: actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71 # v9.0.0
- with:
- script: |
- const run = context.payload.workflow_run;
- await github.rest.repos.createCommitStatus({
- ...context.repo,
- sha: run.head_sha,
- state: "failure",
- context: "Package artifact",
- description: "Package tarball build failed",
- target_url: run.html_url,
- });
-
- publish:
- if: >-
- github.event.workflow_run.head_repository.full_name == github.repository &&
- github.event.workflow_run.conclusion == 'success' &&
- (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'pull_request')
- runs-on: ubuntu-latest
- concurrency:
- group: package-artifact-publish-${{ github.event.workflow_run.head_repository.id }}-${{ github.event.workflow_run.head_branch }}
- cancel-in-progress: false
- steps:
- - name: Report publication started
- uses: actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71 # v9.0.0
- with:
- script: |
- const run = context.payload.workflow_run;
- await github.rest.repos.createCommitStatus({
- ...context.repo,
- sha: run.head_sha,
- state: "pending",
- context: "Package artifact",
- description: "Publishing package tarball",
- target_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
- });
-
- # workflow_run loads this workflow from the default branch. Keep every
- # credentialed operation in that trusted checkout, never the PR checkout.
- - name: Checkout trusted publisher
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- with:
- ref: main
- path: trusted
- persist-credentials: false
-
- - name: Download package artifact
- uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
- with:
- name: eve-package
- path: package-artifact
- github-token: ${{ github.token }}
- run-id: ${{ github.event.workflow_run.id }}
-
- - name: Setup pnpm
- uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10
- with:
- package_json_file: trusted/package.json
-
- - name: Setup Node.js
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
- with:
- node-version-file: trusted/.nvmrc
- cache: pnpm
- cache-dependency-path: trusted/pnpm-lock.yaml
-
- - name: Install publisher dependencies
- working-directory: trusted
- run: pnpm install --frozen-lockfile --ignore-scripts
-
- # Resolve after setup and download so stale builds are rejected as close as
- # possible to the mutable pointer update.
- - name: Resolve publication target
- id: target
- uses: actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71 # v9.0.0
- with:
- script: |
- const { resolvePublicationTarget } = await import(
- `${process.env.GITHUB_WORKSPACE}/trusted/apps/package-artifacts/lib/publication-target.mjs`
- );
- const target = await resolvePublicationTarget(github, context);
- core.setOutput("sha", target.sha);
- core.setOutput("ref", target.ref);
-
- - name: Publish package artifact
- id: publish
- working-directory: trusted
- env:
- EVE_PACKAGE_BLOB_READ_WRITE_TOKEN: ${{ secrets.EVE_PACKAGE_BLOB_READ_WRITE_TOKEN }}
- EVE_PACKAGE_INPUT_DIRECTORY: ${{ github.workspace }}/package-artifact
- EVE_PACKAGE_REF: ${{ steps.target.outputs.ref }}
- EVE_PACKAGE_SOURCE_SHA: ${{ steps.target.outputs.sha }}
- GITHUB_REPOSITORY: ${{ github.repository }}
- GITHUB_TOKEN: ${{ github.token }}
- run: pnpm --filter eve-package-artifacts package:publish
-
- - name: Report publication result
- if: always()
- uses: actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71 # v9.0.0
- env:
- PUBLISH_OUTCOME: ${{ steps.publish.outcome }}
- with:
- script: |
- const run = context.payload.workflow_run;
- const success = process.env.PUBLISH_OUTCOME === "success";
- await github.rest.repos.createCommitStatus({
- ...context.repo,
- sha: run.head_sha,
- state: success ? "success" : "failure",
- context: "Package artifact",
- description: success ? "Package tarball is ready" : "Package tarball publication failed",
- target_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
- });
diff --git a/apps/package-artifacts/README.md b/apps/package-artifacts/README.md
index ad43fbe61..70bce6f27 100644
--- a/apps/package-artifacts/README.md
+++ b/apps/package-artifacts/README.md
@@ -1,6 +1,6 @@
# eve package artifacts
-A stable, read-only Vercel app at `pkg.eve.dev` proxies private Blob artifacts published by GitHub Actions. The app itself never packages source or writes Blob objects.
+The `eve-pkg` Vercel project builds and publishes eve tarballs to private Vercel Blob using deployment OIDC. Its production domain, `pkg.eve.dev`, serves both `main` and same-repository pull-request packages.
```text
/main/eve.tgz
@@ -16,21 +16,17 @@ Initialize an agent from the current `main` build with:
npm exec --yes --package=https://pkg.eve.dev/main/eve.tgz -- eve init my-agent
```
-A same-repository pull-request build is available at `/pr/<number>/eve.tgz` after its **Package artifact** commit status succeeds. Fork pull requests never build or publish package artifacts. The status is reported only after publication finishes, and no manual publishing step is required. Both moving routes redirect to an immutable `/<sha>/eve.tgz` artifact, and the packaged CLI stamps that immutable URL into generated projects.
-
-For example, after the package check passes on PR #123:
+A same-repository pull-request build is available after its **Vercel – eve-pkg** deployment succeeds. Fork pull requests and direct branch deployments never build or publish package artifacts. For example:
```bash
npm exec --yes --package=https://pkg.eve.dev/pr/123/eve.tgz -- eve init my-agent
```
-## Publishing
-
-[Build tarball](../../.github/workflows/build-tarball.yml) runs for `main` pushes and same-repository pull requests without credentials. It checks out the exact source SHA, packages eve, and uploads the tarball and metadata as a short-lived GitHub Actions artifact.
+Both moving routes redirect to an immutable `/<sha>/eve.tgz` artifact. The packaged CLI also stamps that immutable URL into generated projects.
-[Publish tarball](../../.github/workflows/publish-tarball.yml) is an automatic internal trust boundary. GitHub loads it from the default branch on successful package builds. It verifies that the build still represents the current `main` or pull-request head, downloads the artifact on a fresh runner, and uploads the bytes without executing or extracting them. It then reports the user-facing **Package artifact** commit status. Keeping publishing separate prevents pull-request code from changing the credentialed workflow or reading the Blob token.
+## Publishing
-The publisher writes:
+Vercel deploys `main` to Production and same-repository pull requests to Preview. The build derives the source SHA and PR number from Vercel system environment variables, packages eve, verifies that the source still represents the current branch or pull-request head, and writes the following objects:
```text
packages/<sha>/eve.tgz
@@ -41,16 +37,33 @@ packages/refs/pr/<number>.json
SHA objects are immutable. Main and PR pointer objects are mutable and short-cached.
+The build requires `VERCEL_OIDC_TOKEN` and `BLOB_STORE_ID` and passes them explicitly to the Blob SDK. It does not accept or use a static Blob write token.
+
## Project setup
-The Vercel package project must:
+The `eve-pkg` Vercel project must:
- use this directory as its project root;
-- connect the private package Blob store for reads;
+- enable Vercel system environment variables and OIDC;
+- connect the private package Blob store to Production and Preview;
- deploy `main` to Production;
-- set its Ignored Build Step to `test "$VERCEL_GIT_COMMIT_REF" != "main"`; and
-- disable Deployment Protection so package managers can reach the public proxy.
+- omit `BLOB_READ_WRITE_TOKEN` from Production and Preview;
+- disable Deployment Protection so package managers can reach the production proxy; and
+- use the following trusted Ignored Build Step:
+
+```sh
+if [ "$VERCEL_ENV" = "production" ]; then
+ test "$VERCEL_GIT_COMMIT_REF" != "main"
+else
+ test "$VERCEL_ENV" != "preview" ||
+ test "$VERCEL_GIT_REPO_OWNER" != "vercel" ||
+ test "$VERCEL_GIT_REPO_SLUG" != "eve" ||
+ test -z "$VERCEL_GIT_PULL_REQUEST_ID"
+fi
+```
+
+Vercel interprets exit code `0` as “skip this build.” The command therefore permits only production `main` and same-repository PR Preview deployments, rejecting forks before dependency installation. The build repeats the repository and deployment checks as defense in depth.
-Set the repository Actions secret `EVE_PACKAGE_BLOB_READ_WRITE_TOKEN` to the package store's write token. The user-facing workflow never references this secret. Successful same-repository pull-request builds publish automatically through the trusted default-branch publisher.
+Same-repository PR code runs with package-store OIDC access during its Preview build. This is acceptable only while write access to `vercel/eve` is restricted to trusted employees. The Blob store must remain package-only and must not contain unrelated application data.
The smoke check verifies public access and the downloaded main artifact's gzip signature.
diff --git a/apps/package-artifacts/lib/deployment-target.mjs b/apps/package-artifacts/lib/deployment-target.mjs
new file mode 100644
index 000000000..00460d636
--- /dev/null
+++ b/apps/package-artifacts/lib/deployment-target.mjs
@@ -0,0 +1,90 @@
+import { PULL_REQUEST_PATTERN, SHA_PATTERN } from "./package.mjs";
+
+const REPOSITORY = "vercel/eve";
+const REPOSITORY_OWNER = "vercel";
+const REPOSITORY_SLUG = "eve";
+
+export async function resolveDeploymentTarget(env, fetchImplementation = fetch) {
+ if (
+ env.VERCEL_GIT_REPO_OWNER !== REPOSITORY_OWNER ||
+ env.VERCEL_GIT_REPO_SLUG !== REPOSITORY_SLUG
+ ) {
+ return undefined;
+ }
+
+ const isMain = env.VERCEL_ENV === "production" && env.VERCEL_GIT_COMMIT_REF === "main";
+ const isPreview = env.VERCEL_ENV === "preview";
+ if (!isMain && !isPreview) return undefined;
+
+ const sourceSha = env.VERCEL_GIT_COMMIT_SHA;
+ if (!SHA_PATTERN.test(sourceSha ?? "")) {
+ throw new Error("VERCEL_GIT_COMMIT_SHA must be a 40-character Git commit SHA.");
+ }
+ const packageDomain = env.VERCEL_PROJECT_PRODUCTION_URL;
+ if (typeof packageDomain !== "string" || packageDomain.length === 0) {
+ throw new Error("VERCEL_PROJECT_PRODUCTION_URL is required for package publishing.");
+ }
+
+ const ref = isMain ? "main" : await resolvePullRequest(env, sourceSha, fetchImplementation);
+ if (ref === undefined) return undefined;
+ return { sourceSha, ref, origin: `https://${packageDomain}` };
+}
+
+export function vercelOidcCredentials(env) {
+ const oidcToken = env.VERCEL_OIDC_TOKEN;
+ const storeId = env.BLOB_STORE_ID;
+ if (typeof oidcToken !== "string" || oidcToken.length === 0) {
+ throw new Error("VERCEL_OIDC_TOKEN is required for package publishing.");
+ }
+ if (typeof storeId !== "string" || storeId.length === 0) {
+ throw new Error("BLOB_STORE_ID is required for package publishing.");
+ }
+ return { oidcToken, storeId };
+}
+
+async function resolvePullRequest(env, sourceSha, fetchImplementation) {
+ const pullRequest = env.VERCEL_GIT_PULL_REQUEST_ID;
+ if (PULL_REQUEST_PATTERN.test(pullRequest ?? "")) {
+ const pull = await githubRequest(
+ `/repos/${REPOSITORY}/pulls/${pullRequest}`,
+ fetchImplementation,
+ );
+ return isCurrentPullRequest(pull, sourceSha, env.VERCEL_GIT_COMMIT_REF)
+ ? pullRequest
+ : undefined;
+ }
+
+ const branch = env.VERCEL_GIT_COMMIT_REF;
+ if (typeof branch !== "string" || branch.length === 0) return undefined;
+ const query = new URLSearchParams({
+ base: "main",
+ head: `${REPOSITORY_OWNER}:${branch}`,
+ state: "open",
+ });
+ const pulls = await githubRequest(`/repos/${REPOSITORY}/pulls?${query}`, fetchImplementation);
+ const matches = pulls.filter((pull) => isCurrentPullRequest(pull, sourceSha, branch));
+ return matches.length === 1 ? String(matches[0].number) : undefined;
+}
+
+function isCurrentPullRequest(pull, sourceSha, branch) {
+ return (
+ pull.state === "open" &&
+ pull.base?.ref === "main" &&
+ pull.head?.ref === branch &&
+ pull.head?.repo?.full_name === REPOSITORY &&
+ pull.head?.sha === sourceSha
+ );
+}
+
+async function githubRequest(pathname, fetchImplementation) {
+ const response = await fetchImplementation(`https://api.github.com${pathname}`, {
+ headers: {
+ Accept: "application/vnd.github+json",
+ "X-GitHub-Api-Version": "2022-11-28",
+ },
+ });
+ if (!response.ok) {
+ throw new Error(`GitHub returned ${response.status} while resolving package publication.`);
+ }
+ return response.json();
+}
diff --git a/apps/package-artifacts/lib/deployment-target.test.mjs b/apps/package-artifacts/lib/deployment-target.test.mjs
new file mode 100644
index 000000000..8f4380dea
--- /dev/null
+++ b/apps/package-artifacts/lib/deployment-target.test.mjs
@@ -0,0 +1,108 @@
+import { describe, expect, test, vi } from "vitest";
+
+import { resolveDeploymentTarget, vercelOidcCredentials } from "./deployment-target.mjs";
+
+const sha = "a".repeat(40);
+const baseEnv = {
+ VERCEL_ENV: "preview",
+ VERCEL_GIT_COMMIT_REF: "feature/package",
+ VERCEL_GIT_COMMIT_SHA: sha,
+ VERCEL_GIT_PULL_REQUEST_ID: "123",
+ VERCEL_GIT_REPO_OWNER: "vercel",
+ VERCEL_GIT_REPO_SLUG: "eve",
+ VERCEL_PROJECT_PRODUCTION_URL: "pkg.eve.dev",
+};
+const currentPull = {
+ number: 123,
+ state: "open",
+ base: { ref: "main" },
+ head: { ref: "feature/package", repo: { full_name: "vercel/eve" }, sha },
+};
+
+function githubResponse(value, status = 200) {
+ return {
+ ok: status >= 200 && status < 300,
+ status,
+ json: vi.fn().mockResolvedValue(value),
+ };
+}
+
+describe("Vercel package deployment target", () => {
+ test("publishes production main", async () => {
+ await expect(
+ resolveDeploymentTarget({
+ ...baseEnv,
+ VERCEL_ENV: "production",
+ VERCEL_GIT_COMMIT_REF: "main",
+ VERCEL_GIT_PULL_REQUEST_ID: "",
+ }),
+ ).resolves.toEqual({ sourceSha: sha, ref: "main", origin: "https://pkg.eve.dev" });
+ });
+
+ test("publishes same-repository pull requests from the system PR ID", async () => {
+ const fetchImplementation = vi.fn().mockResolvedValue(githubResponse(currentPull));
+ await expect(resolveDeploymentTarget(baseEnv, fetchImplementation)).resolves.toEqual({
+ sourceSha: sha,
+ ref: "123",
+ origin: "https://pkg.eve.dev",
+ });
+ expect(fetchImplementation).toHaveBeenCalledWith(
+ "https://api.github.com/repos/vercel/eve/pulls/123",
+ expect.any(Object),
+ );
+ });
+
+ test("resolves a PR when its branch deployment started before the PR existed", async () => {
+ const fetchImplementation = vi.fn().mockResolvedValue(githubResponse([currentPull]));
+ await expect(
+ resolveDeploymentTarget({ ...baseEnv, VERCEL_GIT_PULL_REQUEST_ID: "" }, fetchImplementation),
+ ).resolves.toEqual({ sourceSha: sha, ref: "123", origin: "https://pkg.eve.dev" });
+ const [url] = fetchImplementation.mock.calls[0];
+ expect(url).toContain("head=vercel%3Afeature%2Fpackage");
+ });
+
+ test("rejects local, fork, direct branch, stale, and non-main PR deployments", async () => {
+ await expect(resolveDeploymentTarget({})).resolves.toBeUndefined();
+ await expect(
+ resolveDeploymentTarget({ ...baseEnv, VERCEL_GIT_REPO_OWNER: "alice" }),
+ ).resolves.toBeUndefined();
+ await expect(
+ resolveDeploymentTarget(
+ { ...baseEnv, VERCEL_GIT_PULL_REQUEST_ID: "" },
+ vi.fn().mockResolvedValue(githubResponse([])),
+ ),
+ ).resolves.toBeUndefined();
+
+ for (const pull of [
+ { ...currentPull, state: "closed" },
+ { ...currentPull, base: { ref: "release" } },
+ { ...currentPull, head: { ...currentPull.head, sha: "b".repeat(40) } },
+ ]) {
+ await expect(
+ resolveDeploymentTarget(baseEnv, vi.fn().mockResolvedValue(githubResponse(pull))),
+ ).resolves.toBeUndefined();
+ }
+ });
+
+ test("requires valid publishing coordinates", async () => {
+ await expect(
+ resolveDeploymentTarget({ ...baseEnv, VERCEL_GIT_COMMIT_SHA: "bad" }),
+ ).rejects.toThrow("40-character Git commit SHA");
+ await expect(
+ resolveDeploymentTarget({ ...baseEnv, VERCEL_PROJECT_PRODUCTION_URL: "" }),
+ ).rejects.toThrow("VERCEL_PROJECT_PRODUCTION_URL");
+ await expect(
+ resolveDeploymentTarget(baseEnv, vi.fn().mockResolvedValue(githubResponse({}, 503))),
+ ).rejects.toThrow("GitHub returned 503");
+ });
+
+ test("requires explicit Vercel OIDC credentials", () => {
+ expect(
+ vercelOidcCredentials({ VERCEL_OIDC_TOKEN: "oidc", BLOB_STORE_ID: "store_123" }),
+ ).toEqual({ oidcToken: "oidc", storeId: "store_123" });
+ expect(() => vercelOidcCredentials({ BLOB_STORE_ID: "store_123" })).toThrow(
+ "VERCEL_OIDC_TOKEN",
+ );
+ expect(() => vercelOidcCredentials({ VERCEL_OIDC_TOKEN: "oidc" })).toThrow("BLOB_STORE_ID");
+ });
+});
diff --git a/apps/package-artifacts/lib/publication-current.mjs b/apps/package-artifacts/lib/publication-current.mjs
index d21d42509..1a9393fcf 100644
--- a/apps/package-artifacts/lib/publication-current.mjs
+++ b/apps/package-artifacts/lib/publication-current.mjs
@@ -10,10 +10,6 @@ export async function assertCurrentPublicationTarget(
if (!SHA_PATTERN.test(sourceSha ?? "")) {
throw new Error("Publication source SHA must be a 40-character Git commit SHA.");
}
- if (typeof token !== "string" || token.length === 0) {
- throw new Error("GITHUB_TOKEN is required to revalidate package publication.");
- }
-
if (ref === "main") {
const branch = await githubRequest(
`/repos/${repository}/branches/main`,
@@ -36,13 +32,13 @@ export async function assertCurrentPublicationTarget(
}
async function githubRequest(pathname, token, fetchImplementation) {
- const response = await fetchImplementation(`https://api.github.com${pathname}`, {
- headers: {
- Accept: "application/vnd.github+json",
- Authorization: `Bearer ${token}`,
- "X-GitHub-Api-Version": "2022-11-28",
- },
- });
+ const headers = {
+ Accept: "application/vnd.github+json",
+ "X-GitHub-Api-Version": "2022-11-28",
+ };
+ if (typeof token === "string" && token.length > 0) headers.Authorization = `Bearer ${token}`;
+
+ const response = await fetchImplementation(`https://api.github.com${pathname}`, { headers });
if (!response.ok) {
throw new Error(`GitHub returned ${response.status} while revalidating package publication.`);
}
diff --git a/apps/package-artifacts/lib/publication-current.test.mjs b/apps/package-artifacts/lib/publication-current.test.mjs
index e25c37185..ee236b57a 100644
--- a/apps/package-artifacts/lib/publication-current.test.mjs
+++ b/apps/package-artifacts/lib/publication-current.test.mjs
@@ -36,16 +36,18 @@ describe("current package publication target", () => {
).rejects.toThrow("stale build");
});
- test("accepts the current open pull request build", async () => {
+ test("accepts the current open pull request build without a GitHub token", async () => {
const fetchImplementation = vi
.fn()
.mockResolvedValue(response({ state: "open", base: { ref: "main" }, head: { sha } }));
await expect(
- assertCurrentPublicationTarget(input, fetchImplementation),
+ assertCurrentPublicationTarget({ ...input, token: undefined }, fetchImplementation),
).resolves.toBeUndefined();
expect(fetchImplementation).toHaveBeenCalledWith(
"https://api.github.com/repos/vercel/eve/pulls/123",
- expect.any(Object),
+ expect.objectContaining({
+ headers: expect.not.objectContaining({ Authorization: expect.anything() }),
+ }),
);
});
@@ -67,9 +69,6 @@ describe("current package publication target", () => {
await expect(
assertCurrentPublicationTarget({ ...input, repository: "eve" }, vi.fn()),
).rejects.toThrow("GITHUB_REPOSITORY");
- await expect(assertCurrentPublicationTarget({ ...input, token: "" }, vi.fn())).rejects.toThrow(
- "GITHUB_TOKEN",
- );
await expect(
assertCurrentPublicationTarget(input, vi.fn().mockResolvedValue(response({}, 503))),
).rejects.toThrow("GitHub returned 503");
diff --git a/apps/package-artifacts/lib/publication-target.mjs b/apps/package-artifacts/lib/publication-target.mjs
deleted file mode 100644
index 03ad95810..000000000
--- a/apps/package-artifacts/lib/publication-target.mjs
+++ /dev/null
@@ -1,54 +0,0 @@
-export async function resolvePublicationTarget(github, context) {
- const run = context.payload.workflow_run;
- if (run?.conclusion !== "success") {
- throw new Error("Package publication requires a successful package build.");
- }
-
- if (run.event === "push") return resolveMainTarget(github, context.repo, run);
- if (run.event === "pull_request") return resolvePullRequestTarget(github, context.repo, run);
- throw new Error(`Unsupported package build event: ${String(run.event)}.`);
-}
-
-async function resolveMainTarget(github, repo, run) {
- if (run.head_branch !== "main") throw new Error("Only main pushes may publish a main package.");
-
- const { data: branch } = await github.rest.repos.getBranch({ ...repo, branch: "main" });
- if (branch.commit.sha !== run.head_sha) {
- throw new Error(
- `Refusing to publish stale main build ${run.head_sha}; main is ${branch.commit.sha}.`,
- );
- }
- return { runId: String(run.id), sha: run.head_sha, ref: "main" };
-}
-
-async function resolvePullRequestTarget(github, repo, run) {
- const headRepository = run.head_repository?.full_name;
- const baseRepository = `${repo.owner}/${repo.repo}`;
- if (headRepository !== baseRepository) {
- throw new Error(`Refusing to publish package artifacts from ${String(headRepository)}.`);
- }
-
- const [headOwner] = headRepository.split("/");
- const pulls = await github.paginate(github.rest.pulls.list, {
- ...repo,
- base: "main",
- head: `${headOwner}:${run.head_branch}`,
- state: "open",
- per_page: 100,
- });
- const matches = pulls.filter(
- (pull) =>
- pull.state === "open" &&
- pull.base.ref === "main" &&
- pull.head.sha === run.head_sha &&
- pull.head.ref === run.head_branch &&
- pull.head.repo?.full_name === headRepository,
- );
- if (matches.length !== 1) {
- throw new Error(
- `Expected one open pull request for package build ${run.id} at ${run.head_sha}; found ${matches.length}.`,
- );
- }
-
- return { runId: String(run.id), sha: run.head_sha, ref: String(matches[0].number) };
-}
diff --git a/apps/package-artifacts/lib/publication-target.test.mjs b/apps/package-artifacts/lib/publication-target.test.mjs
deleted file mode 100644
index f34d5b9c7..000000000
--- a/apps/package-artifacts/lib/publication-target.test.mjs
+++ /dev/null
@@ -1,123 +0,0 @@
-import { describe, expect, test, vi } from "vitest";
-
-import { resolvePublicationTarget } from "./publication-target.mjs";
-
-const sha = "a".repeat(40);
-const repo = { owner: "vercel", repo: "eve" };
-
-function workflowRun(overrides = {}) {
- return {
- id: 123,
- conclusion: "success",
- event: "pull_request",
- head_branch: "feature/package",
- head_repository: { full_name: "vercel/eve" },
- head_sha: sha,
- ...overrides,
- };
-}
-
-function pull(overrides = {}) {
- return {
- number: 456,
- state: "open",
- base: { ref: "main" },
- head: {
- ref: "feature/package",
- repo: { full_name: "vercel/eve" },
- sha,
- },
- ...overrides,
- };
-}
-
-function context(run) {
- return { repo, payload: { workflow_run: run } };
-}
-
-function github({ branchSha = sha, pulls = [] } = {}) {
- const listPullRequests = vi.fn();
- return {
- paginate: vi.fn().mockResolvedValue(pulls),
- rest: {
- pulls: { list: listPullRequests },
- repos: {
- getBranch: vi.fn().mockResolvedValue({ data: { commit: { sha: branchSha } } }),
- },
- },
- };
-}
-
-describe("package publication target", () => {
- test("publishes the current main build", async () => {
- const api = github();
- await expect(
- resolvePublicationTarget(api, context(workflowRun({ event: "push", head_branch: "main" }))),
- ).resolves.toEqual({ runId: "123", sha, ref: "main" });
- expect(api.rest.repos.getBranch).toHaveBeenCalledWith({ ...repo, branch: "main" });
- });
-
- test("rejects stale main builds", async () => {
- const api = github({ branchSha: "b".repeat(40) });
- await expect(
- resolvePublicationTarget(api, context(workflowRun({ event: "push", head_branch: "main" }))),
- ).rejects.toThrow("Refusing to publish stale main build");
- });
-
- test("publishes the current pull request build", async () => {
- const api = github({ pulls: [pull()] });
- await expect(resolvePublicationTarget(api, context(workflowRun()))).resolves.toEqual({
- runId: "123",
- sha,
- ref: "456",
- });
- expect(api.paginate).toHaveBeenCalledWith(api.rest.pulls.list, {
- ...repo,
- base: "main",
- head: "vercel:feature/package",
- state: "open",
- per_page: 100,
- });
- });
-
- test("rejects pull requests from forks", async () => {
- const run = workflowRun({ head_repository: { full_name: "alice/eve" } });
- const api = github();
- await expect(resolvePublicationTarget(api, context(run))).rejects.toThrow(
- "Refusing to publish package artifacts from alice/eve",
- );
- expect(api.paginate).not.toHaveBeenCalled();
- });
-
- test("rejects stale, closed, mismatched, and ambiguous pull requests", async () => {
- const stale = pull({
- head: { ref: "feature/package", repo: { full_name: "vercel/eve" }, sha: "b".repeat(40) },
- });
- const closed = pull({ state: "closed" });
- const wrongBase = pull({ base: { ref: "release" } });
- const wrongRepository = pull({
- head: { ref: "feature/package", repo: { full_name: "alice/eve" }, sha },
- });
-
- for (const pulls of [
- [stale],
- [closed],
- [wrongBase],
- [wrongRepository],
- [pull(), pull({ number: 789 })],
- ]) {
- await expect(
- resolvePublicationTarget(github({ pulls }), context(workflowRun())),
- ).rejects.toThrow("Expected one open pull request");
- }
- });
-
- test("rejects failed and unsupported builds", async () => {
- await expect(
- resolvePublicationTarget(github(), context(workflowRun({ conclusion: "failure" }))),
- ).rejects.toThrow("requires a successful package build");
- await expect(
- resolvePublicationTarget(github(), context(workflowRun({ event: "workflow_dispatch" }))),
- ).rejects.toThrow("Unsupported package build event");
- });
-});
diff --git a/apps/package-artifacts/package.json b/apps/package-artifacts/package.json
index 0cb2aa9d8..bae5f96e9 100644
--- a/apps/package-artifacts/package.json
+++ b/apps/package-artifacts/package.json
@@ -5,8 +5,6 @@
"type": "module",
"scripts": {
"build": "node scripts/build.mjs",
- "package:prepare": "node scripts/prepare.mjs",
- "package:publish": "node scripts/publish.mjs",
"test": "vitest run --config vitest.config.ts",
"test:smoke": "node scripts/check-smoke.mjs"
},
diff --git a/apps/package-artifacts/scripts/build.mjs b/apps/package-artifacts/scripts/build.mjs
index 449b96752..e688d228a 100644
--- a/apps/package-artifacts/scripts/build.mjs
+++ b/apps/package-artifacts/scripts/build.mjs
@@ -1,13 +1,165 @@
-import { mkdir, writeFile } from "node:fs/promises";
+import { createHash } from "node:crypto";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
+import { BlobPreconditionFailedError, get, put } from "@vercel/blob";
+
+import { resolveDeploymentTarget, vercelOidcCredentials } from "../lib/deployment-target.mjs";
+import {
+ packageArtifactPath,
+ packageDependencyUrl,
+ packageManifestPath,
+ packagePointerPath,
+ preparePackageJson,
+} from "../lib/package.mjs";
+import { packPackage } from "../lib/pack.mjs";
+import { assertCurrentPublicationTarget } from "../lib/publication-current.mjs";
+
const appRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const repoRoot = resolve(appRoot, "../..");
+const packageRoot = join(repoRoot, "packages/eve");
+const packageJsonPath = join(packageRoot, "package.json");
+const artifactDirectory = join(appRoot, ".artifacts");
+const target = await resolveDeploymentTarget(process.env);
+
+if (target === undefined) {
+ await writeLandingPage();
+} else {
+ await publishPackage(target);
+}
+
+async function publishPackage({ sourceSha, ref, origin }) {
+ const credentials = vercelOidcCredentials(process.env);
+ const originalPackageJson = await readFile(packageJsonPath, "utf8");
+ const preparedPackageJson = preparePackageJson(JSON.parse(originalPackageJson), sourceSha, "git");
+ const dependencyUrl = packageDependencyUrl(origin, sourceSha);
+
+ try {
+ await rm(artifactDirectory, { force: true, recursive: true });
+ await writeFile(packageJsonPath, `${JSON.stringify(preparedPackageJson, null, 2)}\n`);
+ const tarball = await packPackage(packageRoot, preparedPackageJson.version, {
+ ...process.env,
+ EVE_PACKAGE_DEPENDENCY_URL: dependencyUrl,
+ });
+ const sha256 = createHash("sha256").update(tarball).digest("hex");
+ const manifest = {
+ sourceSha,
+ version: preparedPackageJson.version,
+ tarball: dependencyUrl,
+ sha256,
+ };
+
+ await assertTargetIsCurrent(ref, sourceSha);
+ await putImmutableArtifact(packageArtifactPath(sourceSha), tarball, sha256, credentials);
+ await putImmutableManifest(packageManifestPath(sourceSha), manifest, credentials);
+ await updatePointer(packagePointerPath(ref), manifest, ref, sourceSha, credentials);
+
+ await writeLandingPage();
+ process.stdout.write(`${JSON.stringify(manifest)}\n`);
+ } finally {
+ await writeFile(packageJsonPath, originalPackageJson);
+ await rm(artifactDirectory, { force: true, recursive: true });
+ }
+}
+
+async function assertTargetIsCurrent(ref, sourceSha) {
+ await assertCurrentPublicationTarget({
+ repository: "vercel/eve",
+ ref,
+ sourceSha,
+ token: process.env.GITHUB_TOKEN,
+ });
+}
+
+async function updatePointer(pathname, manifest, ref, sourceSha, credentials) {
+ for (let attempt = 0; attempt < 3; attempt += 1) {
+ const existing = await get(pathname, {
+ ...credentials,
+ access: "private",
+ useCache: false,
+ });
+ const etag = existing?.blob.etag;
+ await existing?.stream.cancel();
+ await assertTargetIsCurrent(ref, sourceSha);
+
+ try {
+ await put(pathname, JSON.stringify(manifest), {
+ ...credentials,
+ access: "private",
+ addRandomSuffix: false,
+ allowOverwrite: etag !== undefined,
+ cacheControlMaxAge: 60,
+ contentType: "application/json",
+ ...(etag === undefined ? {} : { ifMatch: etag }),
+ });
+ return;
+ } catch (error) {
+ const retryable =
+ error instanceof BlobPreconditionFailedError ||
+ (error instanceof Error && error.message.includes("already exists"));
+ if (!retryable || attempt === 2) throw error;
+ }
+ }
+}
+
+async function putImmutableArtifact(pathname, bytes, expectedHash, credentials) {
+ try {
+ await put(pathname, bytes, immutableOptions("application/gzip", credentials));
+ } catch (error) {
+ if (!(error instanceof Error) || !error.message.includes("already exists")) throw error;
+ const published = await get(pathname, {
+ ...credentials,
+ access: "private",
+ useCache: false,
+ });
+ if (published === null) throw new Error("Published package artifact could not be read.");
+ const publishedHash = createHash("sha256")
+ .update(Buffer.from(await new Response(published.stream).arrayBuffer()))
+ .digest("hex");
+ if (publishedHash !== expectedHash) {
+ throw new Error(
+ `Commit ${process.env.VERCEL_GIT_COMMIT_SHA} was already published with different package contents.`,
+ );
+ }
+ }
+}
+
+async function putImmutableManifest(pathname, value, credentials) {
+ try {
+ await put(pathname, JSON.stringify(value), immutableOptions("application/json", credentials));
+ } catch (error) {
+ if (!(error instanceof Error) || !error.message.includes("already exists")) throw error;
+ const published = await get(pathname, {
+ ...credentials,
+ access: "private",
+ useCache: false,
+ });
+ if (published === null) throw new Error("Published package manifest could not be read.");
+ if (JSON.parse(await new Response(published.stream).text()).sha256 !== value.sha256) {
+ throw new Error(
+ `Commit ${process.env.VERCEL_GIT_COMMIT_SHA} was already published with different package contents.`,
+ );
+ }
+ }
+}
+
+function immutableOptions(contentType, credentials) {
+ return {
+ ...credentials,
+ access: "private",
+ addRandomSuffix: false,
+ allowOverwrite: false,
+ cacheControlMaxAge: 31_536_000,
+ contentType,
+ };
+}
-await mkdir(join(appRoot, "public"), { recursive: true });
-await writeFile(
- join(appRoot, "public", "index.html"),
- `<!doctype html>
+async function writeLandingPage() {
+ await mkdir(join(appRoot, "public"), { recursive: true });
+ await writeFile(
+ join(appRoot, "public", "index.html"),
+ `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
@@ -27,4 +179,5 @@ await writeFile(
<body><main><div class="mark" aria-hidden="true"></div><h1>eve packages</h1><p>Package artifacts for eve development.</p></main></body>
</html>
`,
-);
+ );
+}
diff --git a/apps/package-artifacts/scripts/prepare.mjs b/apps/package-artifacts/scripts/prepare.mjs
deleted file mode 100644
index 262495222..000000000
--- a/apps/package-artifacts/scripts/prepare.mjs
+++ /dev/null
@@ -1,58 +0,0 @@
-import { createHash } from "node:crypto";
-import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
-import { dirname, join, resolve } from "node:path";
-import { fileURLToPath } from "node:url";
-
-import {
- PULL_REQUEST_PATTERN,
- SHA_PATTERN,
- packageDependencyUrl,
- preparePackageJson,
-} from "../lib/package.mjs";
-import { packPackage } from "../lib/pack.mjs";
-
-const appRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
-const repoRoot = resolve(appRoot, "../..");
-const packageRoot = join(repoRoot, "packages/eve");
-const packageJsonPath = join(packageRoot, "package.json");
-const sourceSha = process.env.EVE_PACKAGE_SOURCE_SHA;
-const ref = process.env.EVE_PACKAGE_REF;
-const packageOrigin = process.env.EVE_PACKAGE_PUBLIC_ORIGIN;
-const outputDirectory = resolve(process.env.EVE_PACKAGE_OUTPUT_DIRECTORY ?? "package-artifact");
-
-if (!SHA_PATTERN.test(sourceSha ?? "")) {
- throw new Error("EVE_PACKAGE_SOURCE_SHA must be a 40-character Git commit SHA.");
-}
-if (ref !== "main" && !PULL_REQUEST_PATTERN.test(ref ?? "")) {
- throw new Error("EVE_PACKAGE_REF must be main or a positive pull request number.");
-}
-if (typeof packageOrigin !== "string" || packageOrigin.length === 0) {
- throw new Error("EVE_PACKAGE_PUBLIC_ORIGIN is required.");
-}
-
-const originalPackageJson = await readFile(packageJsonPath, "utf8");
-const preparedPackageJson = preparePackageJson(JSON.parse(originalPackageJson), sourceSha, "git");
-const dependencyUrl = packageDependencyUrl(packageOrigin, sourceSha);
-
-try {
- await writeFile(packageJsonPath, `${JSON.stringify(preparedPackageJson, null, 2)}\n`);
- const tarball = await packPackage(packageRoot, preparedPackageJson.version, {
- ...process.env,
- EVE_PACKAGE_DEPENDENCY_URL: dependencyUrl,
- });
- const metadata = {
- sourceSha,
- ref,
- version: preparedPackageJson.version,
- tarball: dependencyUrl,
- sha256: createHash("sha256").update(tarball).digest("hex"),
- };
-
- await rm(outputDirectory, { force: true, recursive: true });
- await mkdir(outputDirectory, { recursive: true });
- await writeFile(join(outputDirectory, "eve.tgz"), tarball);
- await writeFile(join(outputDirectory, "metadata.json"), `${JSON.stringify(metadata)}\n`);
- process.stdout.write(`${JSON.stringify(metadata)}\n`);
-} finally {
- await writeFile(packageJsonPath, originalPackageJson);
-}
diff --git a/apps/package-artifacts/scripts/publish.mjs b/apps/package-artifacts/scripts/publish.mjs
deleted file mode 100644
index 0d6562f84..000000000
--- a/apps/package-artifacts/scripts/publish.mjs
+++ /dev/null
@@ -1,115 +0,0 @@
-import { createHash } from "node:crypto";
-import { readFile } from "node:fs/promises";
-import { join, resolve } from "node:path";
-
-import { get, put } from "@vercel/blob";
-
-import { assertCurrentPublicationTarget } from "../lib/publication-current.mjs";
-import {
- PULL_REQUEST_PATTERN,
- SHA_PATTERN,
- packageArtifactPath,
- packageManifestPath,
- packagePointerPath,
-} from "../lib/package.mjs";
-
-const inputDirectory = resolve(process.env.EVE_PACKAGE_INPUT_DIRECTORY ?? "package-artifact");
-const expectedSha = process.env.EVE_PACKAGE_SOURCE_SHA;
-const expectedRef = process.env.EVE_PACKAGE_REF;
-const token = process.env.EVE_PACKAGE_BLOB_READ_WRITE_TOKEN;
-
-if (!SHA_PATTERN.test(expectedSha ?? "")) {
- throw new Error("EVE_PACKAGE_SOURCE_SHA must be a 40-character Git commit SHA.");
-}
-if (expectedRef !== "main" && !PULL_REQUEST_PATTERN.test(expectedRef ?? "")) {
- throw new Error("EVE_PACKAGE_REF must be main or a positive pull request number.");
-}
-if (typeof token !== "string" || token.length === 0) {
- throw new Error("EVE_PACKAGE_BLOB_READ_WRITE_TOKEN is required.");
-}
-
-const tarball = await readFile(join(inputDirectory, "eve.tgz"));
-const metadata = JSON.parse(await readFile(join(inputDirectory, "metadata.json"), "utf8"));
-const sha256 = createHash("sha256").update(tarball).digest("hex");
-if (
- metadata.sourceSha !== expectedSha ||
- metadata.ref !== expectedRef ||
- metadata.sha256 !== sha256 ||
- !/^\d+\.\d+\.\d+\+git\.[0-9a-f]{16}$/i.test(metadata.version ?? "") ||
- !metadata.version.endsWith(`+git.${expectedSha.slice(0, 16)}`) ||
- typeof metadata.tarball !== "string"
-) {
- throw new Error("Package artifact metadata does not match the trusted publication target.");
-}
-const expectedUrl = `https://pkg.eve.dev/${expectedSha}/eve.tgz`;
-if (metadata.tarball !== expectedUrl) {
- throw new Error(`Package artifact URL must be ${expectedUrl}.`);
-}
-
-const manifest = {
- sourceSha: expectedSha,
- version: metadata.version,
- tarball: expectedUrl,
- sha256,
-};
-await putImmutableArtifact(packageArtifactPath(expectedSha), tarball, sha256);
-await putImmutableManifest(packageManifestPath(expectedSha), manifest);
-await assertCurrentPublicationTarget({
- repository: process.env.GITHUB_REPOSITORY,
- ref: expectedRef,
- sourceSha: expectedSha,
- token: process.env.GITHUB_TOKEN,
-});
-await put(packagePointerPath(expectedRef), JSON.stringify(manifest), {
- access: "private",
- addRandomSuffix: false,
- allowOverwrite: true,
- cacheControlMaxAge: 60,
- contentType: "application/json",
- token,
-});
-process.stdout.write(`${JSON.stringify(manifest)}\n`);
-
-async function putImmutableArtifact(pathname, bytes, expectedHash) {
- try {
- await put(pathname, bytes, immutableOptions("application/gzip"));
- } catch (error) {
- if (!(error instanceof Error) || !error.message.includes("already exists")) throw error;
- const published = await get(pathname, { access: "private", useCache: false, token });
- if (published === null) throw new Error("Published package artifact could not be read.");
- const publishedHash = createHash("sha256")
- .update(Buffer.from(await new Response(published.stream).arrayBuffer()))
- .digest("hex");
- if (publishedHash !== expectedHash) {
- throw new Error(
- `Commit ${expectedSha} was already published with different package contents.`,
- );
- }
- }
-}
-
-async function putImmutableManifest(pathname, value) {
- try {
- await put(pathname, JSON.stringify(value), immutableOptions("application/json"));
- } catch (error) {
- if (!(error instanceof Error) || !error.message.includes("already exists")) throw error;
- const published = await get(pathname, { access: "private", useCache: false, token });
- if (published === null) throw new Error("Published package manifest could not be read.");
- if (JSON.parse(await new Response(published.stream).text()).sha256 !== value.sha256) {
- throw new Error(
- `Commit ${expectedSha} was already published with different package contents.`,
- );
- }
- }
-}
-
-function immutableOptions(contentType) {
- return {
- access: "private",
- addRandomSuffix: false,
- allowOverwrite: false,
- cacheControlMaxAge: 31_536_000,
- contentType,
- token,
- };
-}