diff --git a/.craft.yml b/.craft.yml index bb8a6f71f..c925e2472 100644 --- a/.craft.yml +++ b/.craft.yml @@ -133,6 +133,18 @@ workspaces: assert_match version.to_s, shell_output("#{bin}/sentry --version").chomp end end + # The Docker image: cli-build.yml pushes it to GHCR, tagged with the + # release commit, and these copy it to Docker Hub. + - name: docker + id: Docker Hub (release) + source: ghcr.io/getsentry/toolkit/sentry-cli + target: getsentry/sentry-cli + - name: docker + id: Docker Hub (latest) + source: ghcr.io/getsentry/toolkit/sentry-cli + target: + image: getsentry/sentry-cli + format: "{{{image}}}:latest" # Standalone npm libraries. Each is released on its own, with # `@` tags and `release//` branches, and # `merge-jobs.yml` packs every one of them. To publish another package diff --git a/.github/workflows/cli-build.yml b/.github/workflows/cli-build.yml index b7a50aa04..6f5f97838 100644 --- a/.github/workflows/cli-build.yml +++ b/.github/workflows/cli-build.yml @@ -257,6 +257,57 @@ jobs: ' <<< "$manifest" > /dev/null done + # Pushes the multi-arch CLI image, tagged with the commit. At release, the + # Craft docker targets copy the release commit's image to Docker Hub. + build-docker: + name: Build Docker Image + needs: build-binary + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + packages: write + env: + IMAGE: ghcr.io/getsentry/toolkit/sentry-cli:${{ github.sha }} + PLATFORMS: linux/amd64,linux/arm64 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - name: Download Linux x64 binary + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: sentry-linux-x64 + path: packages/cli/dist-bin + - name: Download Linux arm64 binary + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: sentry-linux-arm64 + path: packages/cli/dist-bin + # Node calls the architecture x64; Docker's TARGETARCH calls it amd64. + - name: Rename the x64 binary for Docker + run: mv packages/cli/dist-bin/sentry-linux-x64 packages/cli/dist-bin/sentry-linux-amd64 + - name: Login to GHCR + uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # The arm64 image runs apt-get at build time and the smoke test runs it. + - name: Set up QEMU + uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + - name: Build and push + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: packages/cli + platforms: ${{ env.PLATFORMS }} + push: true + tags: ${{ env.IMAGE }} + - name: Smoke test + run: node scripts/cli-docker-smoke-test.mjs "$IMAGE" "$PLATFORMS" + build-npm: name: Build npm Package runs-on: ubuntu-latest diff --git a/apps/cli-docs/src/content/docs/getting-started.mdx b/apps/cli-docs/src/content/docs/getting-started.mdx index 2dfa943e9..728f3ffc1 100644 --- a/apps/cli-docs/src/content/docs/getting-started.mdx +++ b/apps/cli-docs/src/content/docs/getting-started.mdx @@ -150,6 +150,35 @@ Homebrew installs shell completions but can't install agent skills into your hom sentry cli setup --no-modify-path --no-completions ``` +## Docker + +The [`getsentry/sentry-cli`](https://hub.docker.com/r/getsentry/sentry-cli) +image runs the CLI on amd64 and arm64 Linux. Its entrypoint is `sentry` and its +working directory is `/work`, so mount your project there and pass a token: + +```bash +docker run --rm -v "$PWD:/work" -e SENTRY_AUTH_TOKEN getsentry/sentry-cli sourcemap upload ./dist +``` + +Each release is tagged with its version, and `latest` follows the newest +release. Pin a version tag in CI. The image doesn't check for updates; pull a +newer tag to upgrade. + +The image includes `git`, so commands such as `release set-commits --auto` and +`release propose-version` read a checkout mounted at `/work`. Credentials and +caches are not kept between runs, so use `SENTRY_AUTH_TOKEN` rather than +`sentry auth login`. + +The image is built on [distroless](https://github.com/GoogleContainerTools/distroless) +and has no shell, so CI systems that run a script inside the job's image, such +as GitLab CI, can't use it as the job image. Install the CLI in the job instead, +or copy the binary into your own glibc-based image. The binary also needs +`libatomic1`: + +```dockerfile +COPY --from=getsentry/sentry-cli /usr/local/bin/sentry /usr/local/bin/sentry +``` + ## Package Managers Install globally with your preferred package manager (the npm packages require **Node.js 20+**; on **22.15+** the built-in `node:sqlite` is used, on 20–22.14 a bundled WASM fallback is used transparently): diff --git a/apps/cli-docs/src/content/docs/migrating-from-v3.md b/apps/cli-docs/src/content/docs/migrating-from-v3.md index 83ff0121a..dbfc432a9 100644 --- a/apps/cli-docs/src/content/docs/migrating-from-v3.md +++ b/apps/cli-docs/src/content/docs/migrating-from-v3.md @@ -62,6 +62,24 @@ sentry --version sentry auth status ``` +### Docker + +The v4 image keeps the `getsentry/sentry-cli` name and the `/work` working +directory, so `docker run --rm -v "$PWD:/work" getsentry/sentry-cli ` +keeps working once you update the [command names](#command-changes). v3 +releases stay available under their version tags. What changed: + +- The entrypoint runs `sentry` directly. The v3 entrypoint dropped a leading + `sentry-cli` argument and ran `sh` when asked; v4 passes every argument to + `sentry`. +- The base image is distroless instead of Alpine, so there is no shell. Jobs + that clear the entrypoint and run a script in the image, such as GitLab CI + jobs, need another way to install the CLI. +- The binary is `/usr/local/bin/sentry`; there is no `sentry-cli`. The image + includes `git`. + +See [Docker](../getting-started/#docker) for usage. + ## The `sentry-cli` → `sentry` rename If you only ever call the top-level binary, the simplest bridge is a plain diff --git a/docs/cli/README.md b/docs/cli/README.md index b00096715..602d6afd4 100644 --- a/docs/cli/README.md +++ b/docs/cli/README.md @@ -14,8 +14,9 @@ CLI releases build it from that workspace. Start here for repository guides: [command conventions](../../packages/cli/CONTRIBUTING.md) — implementation details and command design. - [CLI build workflow](../../.github/workflows/cli-build.yml) and - [release configuration](../../.craft.yml) — binaries, npm packaging, and - release-gated website artifacts. + [release configuration](../../.craft.yml) — binaries, npm packaging, the + [Docker image](../../packages/cli/Dockerfile), and release-gated website + artifacts. - [Website packaging](../../apps/cli-docs/vercel/build-output.mjs) — the Vercel routes for `/install`, and the `/install/` function that serves the install script with `SENTRY_HANDOFF` set. diff --git a/package.json b/package.json index 86bbc0bf0..1bd0a37d9 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "scripts": { "docs:check": "node scripts/check-doc-links.mjs", - "test:ci-projects": "node --test scripts/ci-projects.test.mjs scripts/check-workspace-deps.test.mjs scripts/cli-nightly-version.test.mjs scripts/cli-nightly-watchdog.test.mjs scripts/platform-setup-pr.test.mjs scripts/codemod-version-changed.test.mjs scripts/npm-library-workspaces.test.mjs scripts/cloudflare-deployment.test.mjs scripts/deploy-workflow.test.mjs scripts/migrate-cloudflare-token.test.mjs scripts/oxc-config.test.mjs scripts/cli-lint-rules.test.mjs scripts/workflow-policy.test.mjs scripts/tsconfig-policy.test.mjs", + "test:ci-projects": "node --test scripts/ci-projects.test.mjs scripts/check-workspace-deps.test.mjs scripts/cli-docker-smoke-test.test.mjs scripts/cli-nightly-version.test.mjs scripts/cli-nightly-watchdog.test.mjs scripts/platform-setup-pr.test.mjs scripts/codemod-version-changed.test.mjs scripts/npm-library-workspaces.test.mjs scripts/cloudflare-deployment.test.mjs scripts/deploy-workflow.test.mjs scripts/migrate-cloudflare-token.test.mjs scripts/oxc-config.test.mjs scripts/cli-lint-rules.test.mjs scripts/workflow-policy.test.mjs scripts/tsconfig-policy.test.mjs", "dev": "pnpm --filter '@sentry/mcp-cloudflare...' --if-present run build && dotenv -e .env -e .env.local -- pnpm --parallel --filter @sentry/mcp-cloudflare --filter @sentry/mcp-core --filter @sentry/mcp-server-mocks --if-present run dev", "dev:stdio": "pnpm --filter '@sentry/mcp-core...' --if-present run build 1>&2 && dotenv -e .env -e .env.local -- pnpm --silent --dir packages/cli run cli mcp", "build": "dotenv -e .env -e .env.local -- pnpm -r --filter '!sentry' --filter '!sentry-cli-docs' --if-present run build", diff --git a/packages/cli/.dockerignore b/packages/cli/.dockerignore new file mode 100644 index 000000000..96bfae373 --- /dev/null +++ b/packages/cli/.dockerignore @@ -0,0 +1,3 @@ +** +!dist-bin/sentry-linux-amd64 +!dist-bin/sentry-linux-arm64 diff --git a/packages/cli/Dockerfile b/packages/cli/Dockerfile new file mode 100644 index 000000000..2956ae003 --- /dev/null +++ b/packages/cli/Dockerfile @@ -0,0 +1,39 @@ +# The getsentry/sentry-cli image: the standalone Linux binary on distroless. +# cli-build.yml builds it from this directory after copying in +# dist-bin/sentry-linux-amd64 and dist-bin/sentry-linux-arm64 (the +# `script/build.ts` outputs, renamed from x64 to Docker's amd64). + +# distroless has no package manager, so take the extra files from Debian 12, +# which distroless/cc-debian12 is built from. The Node runtime in the binary +# links libatomic. `release set-commits --auto`, `release propose-version`, and +# `code-mappings upload` read the mounted checkout with git, which needs +# libpcre2 and zlib. A checkout mounted from the host belongs to another user, +# so git must trust the /work mount point. +FROM debian:bookworm-slim@sha256:e67eeead736ff7bbe258ef088bff683635c561b1fcfefad0806c1df4ab49ce0e AS deps +RUN apt-get update \ + && apt-get install -y --no-install-recommends git libatomic1 \ + && git config --system --add safe.directory /work \ + && mkdir /deps \ + && cp -L --parents /etc/gitconfig /usr/bin/git \ + /usr/lib/*-linux-gnu/libatomic.so.1 \ + /lib/*-linux-gnu/libpcre2-8.so.0 \ + /lib/*-linux-gnu/libz.so.1 \ + /deps/ + +FROM gcr.io/distroless/cc-debian12@sha256:e5d81ddde149641e2a9ba55be4545bc125c67de07508b03ba4c22e6eb0ded5aa + +LABEL org.opencontainers.image.source="https://github.com/getsentry/toolkit" \ + org.opencontainers.image.description="Sentry CLI" \ + org.opencontainers.image.licenses="FSL-1.1-Apache-2.0" + +COPY --from=deps /deps/ / + +ARG TARGETARCH +COPY --chmod=555 dist-bin/sentry-linux-$TARGETARCH /usr/local/bin/sentry + +# A container can't upgrade its own image; pull a newer tag instead. +ENV SENTRY_CLI_NO_UPDATE_CHECK=1 + +# Same as the v3 image: mount the project at /work. +WORKDIR /work +ENTRYPOINT ["/usr/local/bin/sentry"] diff --git a/scripts/cli-docker-smoke-test.mjs b/scripts/cli-docker-smoke-test.mjs new file mode 100644 index 000000000..a67da671c --- /dev/null +++ b/scripts/cli-docker-smoke-test.mjs @@ -0,0 +1,82 @@ +/** + * Smoke-tests the CLI Docker image that cli-build.yml pushes. On each platform + * the CLI starts, and `release propose-version` reads the commit through the + * git copied into the image from the checkout mounted at /work, the image's + * working directory. Run it from the repository root: + * + * node scripts/cli-docker-smoke-test.mjs [,...] + */ +import { execFileSync } from "node:child_process"; +import { pathToFileURL } from "node:url"; + +/** + * Throws when the image fails a check on any platform. + * + * @param {object} options + * @param {string} options.image - Image reference to run. + * @param {string[]} options.platforms - Platforms to run it on, such as + * `linux/arm64`. + * @param {string} options.checkout - Host checkout to mount at /work. + * @param {string} options.head - Commit that checkout is at. + * @param {(args: string[]) => string} options.docker - Runs `docker` with the + * arguments and returns its stdout; throws on a non-zero exit. + * @returns {Map} The CLI version that each platform printed. + */ +export function smokeTestImage({ image, platforms, checkout, head, docker }) { + const versions = new Map(); + for (const platform of platforms) { + const run = ["run", "--rm", "--platform", platform]; + const version = docker([...run, image, "--version"]).trim(); + if (!version) { + throw new Error(`${platform}: sentry --version printed nothing`); + } + // propose-version needs a token but makes no API call. + const proposed = docker([ + ...run, + "--env", + "SENTRY_AUTH_TOKEN=smoke-test", + "--volume", + `${checkout}:/work`, + image, + "release", + "propose-version", + "--json", + ]); + const mountedHead = JSON.parse(proposed).version; + if (mountedHead !== head) { + throw new Error( + `${platform}: propose-version in /work read ${mountedHead}, expected ${head}`, + ); + } + versions.set(platform, version); + } + return versions; +} + +if ( + process.argv[1] && + import.meta.url === pathToFileURL(process.argv[1]).href +) { + const [image, platforms] = process.argv.slice(2); + if (!image || !platforms) { + throw new Error( + "Usage: node scripts/cli-docker-smoke-test.mjs [,...]", + ); + } + const versions = smokeTestImage({ + image, + platforms: platforms.split(","), + checkout: process.cwd(), + head: execFileSync("git", ["rev-parse", "HEAD"], { + encoding: "utf8", + }).trim(), + docker: (args) => + execFileSync("docker", args, { + encoding: "utf8", + stdio: ["ignore", "pipe", "inherit"], + }), + }); + for (const [platform, version] of versions) { + console.log(`${platform}: sentry ${version}; propose-version reads /work`); + } +} diff --git a/scripts/cli-docker-smoke-test.test.mjs b/scripts/cli-docker-smoke-test.test.mjs new file mode 100644 index 000000000..acfb556b3 --- /dev/null +++ b/scripts/cli-docker-smoke-test.test.mjs @@ -0,0 +1,85 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { smokeTestImage } from "./cli-docker-smoke-test.mjs"; + +const IMAGE = "ghcr.io/getsentry/toolkit/sentry-cli:abc123"; +const HEAD = "abc123"; +const PLATFORMS = ["linux/amd64", "linux/arm64"]; + +/** A fake `docker` that records its calls and answers like a working image. */ +function fakeDocker({ version = "4.0.0", mountedHead = HEAD } = {}) { + const calls = []; + const docker = (args) => { + calls.push(args); + return args.includes("--version") + ? `${version}\n` + : JSON.stringify({ version: mountedHead, source: "git" }); + }; + return { calls, docker }; +} + +test("runs the CLI and reads the mounted checkout on every platform", () => { + const { calls, docker } = fakeDocker(); + + const versions = smokeTestImage({ + image: IMAGE, + platforms: PLATFORMS, + checkout: "/repo", + head: HEAD, + docker, + }); + + assert.deepEqual([...versions.keys()], PLATFORMS); + assert.deepEqual( + calls, + PLATFORMS.flatMap((platform) => [ + ["run", "--rm", "--platform", platform, IMAGE, "--version"], + [ + "run", + "--rm", + "--platform", + platform, + "--env", + "SENTRY_AUTH_TOKEN=smoke-test", + "--volume", + "/repo:/work", + IMAGE, + "release", + "propose-version", + "--json", + ], + ]), + ); +}); + +test("fails when propose-version in /work reads another commit", () => { + const { docker } = fakeDocker({ mountedHead: "def456" }); + + assert.throws( + () => + smokeTestImage({ + image: IMAGE, + platforms: PLATFORMS, + checkout: "/repo", + head: HEAD, + docker, + }), + /linux\/amd64: propose-version in \/work read def456, expected abc123/, + ); +}); + +test("fails when the CLI prints no version", () => { + const { docker } = fakeDocker({ version: "" }); + + assert.throws( + () => + smokeTestImage({ + image: IMAGE, + platforms: PLATFORMS, + checkout: "/repo", + head: HEAD, + docker, + }), + /linux\/amd64: sentry --version printed nothing/, + ); +});