> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bqto.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripts and CI

> Run a command through a bqto session with -- CMD, read the session as JSON with --output json, end it when a parent program dies, and read the exit codes.

## No prompts

When stdin is not a terminal, or with `--output json`, bqto does not prompt. It acts as if you
gave `--no-input`: a missing value is an error.

## Run one command through the session

With `-- CMD`, bqto runs the command once the tunnel is up. When the command exits, bqto ends the
session and deletes the bastion.

```sh theme={null}
bqto connect --service cloudsql --project my-project --instance sql-main \
  --no-input -- sh -c 'psql -U app -c "select 1"'
```

The command gets the address of the session in its environment:

| Variable | Value |
| - | - |
| `BQTO_HOST`, `BQTO_PORT` | `127.0.0.1` and the local port |
| `PGHOST`, `PGPORT` | The same, for Cloud SQL and AlloyDB |
| `BQTO_REMOTE` | The address of the service, `HOST:PORT`: the private IP that the port forwards to |
| `BQTO_TLS` | `1` when your client must use TLS, empty otherwise |
| `BQTO_AUTH` | The credential that the service takes: `password` (an AUTH string), `iam` (an access token), or empty for none |

If the default port is taken, bqto uses the next free one. So two jobs on one host can run at the
same time. Always read the port from the environment.

bqto passes its stdin to the command. To pass a long query, use stdin or a file, not nested quotes.

## A longer session

With `--output json`, bqto prints one JSON object for each service on stdout once the tunnel is
up:

```json theme={null}
{"event":"ready","service":"cloudsql","instance":"sql","host":"127.0.0.1","port":15432,"remote":"10.0.0.5:5432","bastion":"bqto-1a2b3c4d","expires":"2026-10-14T12:30:00Z"}
```

* Use `host` and `port`. `remote` is the address of the service.
* `tls: true` means that your client must use TLS.
* `auth` says which credential the service takes: `password` (an AUTH string) or `iam` (an access
  token).

A script can start bqto in the background, read the line, and stop bqto with SIGINT or SIGTERM when
the script is done. bqto then deletes the bastion. The bastion deletes itself at `expires` in any case.

## When a program starts bqto

A program that starts bqto, such as a menu bar app, can die without a signal. With
`--end-on-stdin-close`, bqto ends the session and deletes the bastion when its stdin closes. The
program keeps a pipe to the stdin of bqto open and never writes to it:

```sh theme={null}
bqto connect --end-on-stdin-close --output json \
  --project my-project --service cloudsql --instance my-db
```

The flag never prompts. It refuses a terminal on stdin, and you cannot use it with `-- CMD`.

## Exit codes

| Code | Meaning |
| - | - |
| 0 | The session ended normally once it was ready: Ctrl-C, SIGTERM, SIGHUP (the terminal closed), the end of stdin with `--end-on-stdin-close`, or its `--duration` ran out |
| 1 | An error: auth, an API call, the bastion, the tunnel, a session that ended early, or a failed delete of the bastion |
| 2 | A usage error: an unknown flag, a missing or bad value, or a feature of bqto Pro. bqto created nothing |
| 126 | The command after `--` exists but cannot run, for example a script without `chmod +x`. bqto checks this before it creates the bastion |
| 127 | The command after `--` was not found. bqto checks this before it creates the bastion |
| 130 | Interrupted (SIGINT, SIGTERM, SIGHUP, or the end of stdin with `--end-on-stdin-close`) before the session was ready, or you quit the wizard |
| 128 + N | With `-- CMD`, a signal N killed the command: 143 for SIGTERM, 137 for SIGKILL |
| Other | With `-- CMD`, the exit code of the command |

With `-- CMD`, a code alone does not say whether bqto or the command failed. Look for the `error:`
line of bqto on stderr first.

## In CI

* In GitHub Actions, use [bqto-action](https://docs.bqto.dev/guides/github-actions.md).
* On GitLab CI, Cloud Build, Jenkins, and other CI systems, install bqto and run
  `bqto connect --no-input ... -- CMD`.
* In CI, run bqto where no one else shares the machine. [Security](https://docs.bqto.dev/reference/security.md#shared-machines-and-ci)
  explains why.
* With bqto Pro in CI, pass the key in `BQTO_LICENSE_KEY`. See
  [License key](https://docs.bqto.dev/pro/license-key.md#in-ci).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.