diff --git a/src/content/changelog/workers/2025-11-03-wrangler-output-file.mdx b/src/content/changelog/workers/2025-11-03-wrangler-output-file.mdx new file mode 100644 index 000000000000000..2f7ce3b8d7e5756 --- /dev/null +++ b/src/content/changelog/workers/2025-11-03-wrangler-output-file.mdx @@ -0,0 +1,9 @@ +--- +title: Capture Wrangler command output in structured format +description: New environment variables allow CI/CD pipelines to programmatically access deployment information +products: + - workers +date: 2025-11-03 +--- + +You can now capture Wrangler command output in a structured [ND-JSON](https://github.com/ndjson/ndjson-spec) format by setting the [`WRANGLER_OUTPUT_FILE_PATH`](/workers/wrangler/system-environment-variables/#supported-environment-variables) or [`WRANGLER_OUTPUT_FILE_DIRECTORY`](/workers/wrangler/system-environment-variables/#supported-environment-variables) environment variables. This feature is particularly useful for CI/CD pipelines and automation tools that need programmatic access to deployment information such as worker names, version IDs, deployment URLs, and error details. Commands that support this feature include [`wrangler deploy`](/workers/wrangler/commands/#deploy), [`wrangler versions upload`](/workers/wrangler/commands/#versions), [`wrangler versions deploy`](/workers/wrangler/commands/#versions), and [`wrangler pages deploy`](/workers/wrangler/commands/#deploy-1). diff --git a/src/content/docs/workers/wrangler/commands.mdx b/src/content/docs/workers/wrangler/commands.mdx index 0e3826b2462046f..00a6e6986670ed8 100644 --- a/src/content/docs/workers/wrangler/commands.mdx +++ b/src/content/docs/workers/wrangler/commands.mdx @@ -328,6 +328,10 @@ None of the options for this command are required. Also, many can be set in your +### Capturing deployment output + +When you deploy a Worker, you can capture deployment information in a structured format by setting the [`WRANGLER_OUTPUT_FILE_PATH`](/workers/wrangler/system-environment-variables/#supported-environment-variables) or [`WRANGLER_OUTPUT_FILE_DIRECTORY`](/workers/wrangler/system-environment-variables/#supported-environment-variables) environment variables. This writes deployment metadata (including worker name, version ID, and deployment URLs) to a file in [ND-JSON](https://github.com/ndjson/ndjson-spec) format, which is useful for CI/CD pipelines and automation tools. + --- ## `delete` @@ -1097,6 +1101,10 @@ Your site is deployed to `.pages.dev`. If you do not provide the ` ::: +#### Capturing deployment output + +When you deploy a Pages project, you can capture deployment information in a structured format by setting the [`WRANGLER_OUTPUT_FILE_PATH`](/workers/wrangler/system-environment-variables/#supported-environment-variables) or [`WRANGLER_OUTPUT_FILE_DIRECTORY`](/workers/wrangler/system-environment-variables/#supported-environment-variables) environment variables. This writes deployment metadata (including project name, deployment ID, and deployment URL) to a file in [ND-JSON](https://github.com/ndjson/ndjson-spec) format, which is useful for CI/CD pipelines and automation tools. + ### `secret put` Create or update a secret for a Pages project. @@ -1323,6 +1331,10 @@ description="Retrieve details for the 10 most recent versions. Details include ` /> +### Capturing version output + +When you upload or deploy a version, you can capture version information in a structured format by setting the [`WRANGLER_OUTPUT_FILE_PATH`](/workers/wrangler/system-environment-variables/#supported-environment-variables) or [`WRANGLER_OUTPUT_FILE_DIRECTORY`](/workers/wrangler/system-environment-variables/#supported-environment-variables) environment variables. This writes version metadata (including worker name, version ID, and preview URLs for uploads, or deployment information for deploys) to a file in [ND-JSON](https://github.com/ndjson/ndjson-spec) format, which is useful for CI/CD pipelines and automation tools. + ### `secret put` Create or replace a secret for a Worker. Creates a new [version](/workers/configuration/versions-and-deployments/#versions) with modified secrets without [deploying](/workers/configuration/versions-and-deployments/#deployments) the Worker. diff --git a/src/content/docs/workers/wrangler/system-environment-variables.mdx b/src/content/docs/workers/wrangler/system-environment-variables.mdx index c24339d23823ffd..aa07266fe998c39 100644 --- a/src/content/docs/workers/wrangler/system-environment-variables.mdx +++ b/src/content/docs/workers/wrangler/system-environment-variables.mdx @@ -86,6 +86,32 @@ Wrangler supports the following environment variables: * `WRANGLER_R2_SQL_AUTH_TOKEN` - API token used for executing queries with [R2 SQL](/r2-sql). + +- `WRANGLER_OUTPUT_FILE_PATH` + + - Specifies a file path where Wrangler will write output data in [ND-JSON](https://github.com/ndjson/ndjson-spec) (newline-delimited JSON) format. Each line in the file is a separate JSON object containing information about Wrangler operations such as deployments, version uploads, and errors. This is useful for CI/CD pipelines and automation tools that need to programmatically access deployment information. If both `WRANGLER_OUTPUT_FILE_PATH` and `WRANGLER_OUTPUT_FILE_DIRECTORY` are set, `WRANGLER_OUTPUT_FILE_PATH` takes precedence. + +- `WRANGLER_OUTPUT_FILE_DIRECTORY` + + - Specifies a directory where Wrangler will create a randomly-named file (format: `wrangler-output--.json`) to write output data in [ND-JSON](https://github.com/ndjson/ndjson-spec) format. This is useful when you want to keep output files organized in a specific directory but don't need to control the exact filename. If both `WRANGLER_OUTPUT_FILE_PATH` and `WRANGLER_OUTPUT_FILE_DIRECTORY` are set, `WRANGLER_OUTPUT_FILE_PATH` takes precedence. + +### Example output file + +When these environment variables are set, Wrangler writes one JSON object per line to the output file. Each entry includes a `timestamp` field and a `type` field indicating the kind of operation. Here's an example of what the file might contain after running `wrangler deploy`: + +```json +{"type":"wrangler-session","version":1,"wrangler_version":"3.78.0","command_line_args":["deploy"],"log_file_path":"/path/to/logs/wrangler-2024-11-03_12-00-00_abc.log","timestamp":"2024-11-03T12:00:00.000Z"} +{"type":"deploy","version":1,"worker_name":"my-worker","worker_tag":"abc123def456","version_id":"v1-abc123","targets":["https://my-worker.example.workers.dev"],"worker_name_overridden":false,"wrangler_environment":"production","timestamp":"2024-11-03T12:00:05.000Z"} +``` + +The `wrangler-session` entry is written when Wrangler starts and contains information about the command being run. The `deploy` entry is written when a deployment completes successfully and includes the worker name, version ID, and deployment URLs. + +Other entry types include: +- `version-upload` - Written by `wrangler versions upload` with version ID and preview URLs +- `version-deploy` - Written by `wrangler versions deploy` with deployment information +- `pages-deploy` - Written by `wrangler pages deploy` with Pages deployment details +- `command-failed` - Written when a command fails, including error code and message + ## Example `.env` file The following is an example `.env` file: