# CI

Deploy on push: write a GitHub Actions workflow for [`velastack/action`](/docs/helpers/github-action), check what it needs, and optionally set up the deploy key.

### Syntax

```
$ vela enable ci
```

```
$ vela deploy --server root@your-server --domain example.com   # once, from your machine
$ vela enable ci
$ git add .vela/project.json .github/workflows/deploy.yml && git commit -m "Deploy on push"
```

The command writes `.github/workflows/deploy.yml`, at the root of the git repository. From then on every push to the branch deploys `production`, the same way [`vela deploy`](/docs/deploy) does from your machine. The branch is the one checked out when you run the command, `main` when there is none, or the one named with `--branch`.

### The workflow

```yaml
name: Deploy

on:
  # Every push to `main` deploys production.
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

# Two pushes to the same branch queue instead of deploying over each other.
concurrency:
  group: deploy-production
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v6

      # Which server, and which domain, come from the committed
      # .vela/project.json. The target defaults to production.
      - uses: velastack/action@v1
        with:
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
          # api-key: ${{ secrets.VELA_API_KEY }}
        env:
          # Placeholders, not credentials.
          POCKETBASE_SUPERUSER_EMAIL: ci@example.com
          POCKETBASE_SUPERUSER_PASSWORD: ci-build-only-password
```

This is what a project with a backend gets, with the longer comments trimmed. The workflow names no server, no domain and no target: the action reads the server and domain from the committed `.vela/project.json` and deploys `production` unless told otherwise. `workflow_dispatch` lets you run it by hand from the Actions tab.

The two `POCKETBASE_SUPERUSER_*` values are placeholders, and only written for a project with a backend. They are the login of the throwaway database a build starts on the runner when the target has none yet. A build against the deployed database uses the real ones, which stay on the server. See [Secrets](/docs/helpers/github-action).

A project that lives in a subdirectory of the repository gets a `working-directory` input, and `--ssh-port` adds `ssh-port`. The file is yours to edit afterwards. An existing workflow that differs from what would be written is left alone and the command stops; pass `--force` to overwrite it.

### What it checks

After writing the file, the command lists what the workflow needs before its first run, each marked done or to do:

- **A lockfile.** The action installs with `npm ci` from a `package-lock.json`, or with `pnpm install --frozen-lockfile` when a `pnpm-lock.yaml` is the only lockfile. With both it uses npm, as the CLI does. A project with only a `yarn.lock` or `bun.lock` is told to commit one of the two, or to install in a step of its own.
- **A deployed target.** `production` has to be bound to a server in `.vela/project.json`, which the first `vela deploy --server ...` does.
- **`.vela/project.json` in git.** The action refuses to deploy without it.
- **A GitHub remote.** The workflow only runs on GitHub.
- **The `SSH_PRIVATE_KEY` secret.** See below.

A check that fails stops nothing: the workflow file is still written, and the item is listed as to do. So the command can be run before the first deploy, to see what is missing.

### The deploy key

The workflow needs one secret, `SSH_PRIVATE_KEY`: a private key the server accepts. `--setup-key` makes it:

```
$ vela enable ci --setup-key
```

1. A new ed25519 keypair is generated for CI, separate from your own key.
2. The public half is appended to the server's `authorized_keys`, with a comment that names the app.
3. The private half is stored as the repository's `SSH_PRIVATE_KEY` secret with the [`gh`](https://cli.github.com) CLI, which has to be installed and signed in. A secret of that name that is already there is replaced.

The plan is shown first and nothing happens until you agree. `--yes` goes ahead without asking, and is required off a terminal. The keypair is not kept on your machine. Running it again makes a new key and leaves the old line on the server.

Without the flag, at a terminal, the command offers to do this when the server and the repository are known. Decline, and it prints the same three steps to run by hand. With `--setup-key` an existing workflow is left as it is, so the flag is also the way to make a key for a workflow you wrote yourself.

### Previews

```
$ vela enable ci --previews
```

`--previews` writes a workflow that also deploys a [preview](/docs/deploy/previews) of every pull request and removes it when the pull request closes. It adds the `pull_request` trigger, `pull-requests: write` for the comment that carries the preview URL, a `concurrency` group per branch, a condition that skips pull requests from forks (they have no access to the secrets), and the `api-key` input.

Previews are named by velastack.dev, so they need the `VELA_API_KEY` repository secret and a [linked](/docs/link) project. Create a key at [velastack.dev/api-keys/new](https://velastack.dev/api-keys/new) and store it with `gh secret set VELA_API_KEY`.

### Options

- `--branch <name>` - Branch whose pushes deploy production. Defaults to the one checked out, else `main`
- `--api-key-secret` - Pass the `VELA_API_KEY` repository secret to the action, so deploys are recorded on velastack.dev. Without it the line is written commented out
- `--previews` - Also deploy a preview of every pull request and remove it on close. Needs the `VELA_API_KEY` secret
- `--force` - Overwrite an existing `.github/workflows/deploy.yml`
- `--setup-key` - Create a deploy key for CI: add its public half to the server and store its private half as the `SSH_PRIVATE_KEY` secret with `gh`
- `-y, --yes` - With `--setup-key`, go ahead without asking
- `-i, --identity <file>` - SSH private key to authenticate with
- `--ssh-port <port>` - SSH port
- `--accept-host-keys` - Trust an unknown host key on first connect (CI)

### Environment

The app's environment lives on the server, not in GitHub secrets. Set it once with [`vela env`](/docs/env):

```
$ vela env import .env.production -t production
```

Only what the _build_ reads at build time has to be on the runner too. Add it under the action step's `env` in the workflow.

There is no `vela disable ci`. To stop deploying on push, delete the workflow file, remove the `SSH_PRIVATE_KEY` secret, and remove the CI key's line from the server's `~/.ssh/authorized_keys`.