# Publish from CI

Source: https://dropshelf.ai/docs/agents/ci

Publish a build to a dropshelf drop from GitHub Actions or GitLab CI with the dropshelf CLI and a personal access token.

Publish your build to a drop from a CI pipeline, so every merge to the main branch updates it. This
page is for developers who already build a static site in CI. It covers GitHub Actions, GitLab CI and
a preview drop for each branch.

The job installs the `dropshelf` CLI, links the build folder to the drop and pushes it. The push sends
only the files that changed since the live version.

## Prerequisites [#prerequisites]

- The drop exists, and the person whose token the job uses is its owner or an editor. Create it with
  a first push from your machine, or from the dashboard. See [Publish a drop](/docs/use/publish).
- A build step that writes the site to a folder, such as `dist`, with `index.html` at its top.
- A runner with `curl`, `tar` and `sha256sum` or `shasum`, on Linux or macOS.

## Create a token for the job [#create-a-token-for-the-job]

The job signs in with a personal access token, never with a browser.

1. Create a token on the **Connections** page and name it after the job, such as `ci-team-lunch`. See
   [Personal access tokens](/docs/use/tokens).
2. Store it as a secret in your CI system, named `DROPSHELF_TOKEN`.
3. Note when it expires. Every token expires, so replace the secret before that date.

The job sets two variables, which the CLI reads instead of a stored sign-in:

- `DROPSHELF_URL`, the dashboard's address, such as `https://drop.example.com`.
- `DROPSHELF_TOKEN`, the token. The CLI sends it only to the instance `DROPSHELF_URL` names.

## Install the CLI in the job [#install-the-cli-in-the-job]

The instance serves an install script that downloads the CLI release matching the instance and checks
it against its checksum. To install it without signing in, set `DROPSHELF_NO_LOGIN=1`:

```bash
curl -fsSL https://drop.example.com/install.sh | DROPSHELF_NO_LOGIN=1 sh
```

The script installs `dropshelf` in `~/.local/bin`. Add that folder to the `PATH` for the steps that
follow. To install somewhere else, set `DROPSHELF_INSTALL_DIR`.

## Publish with GitHub Actions [#publish-with-github-actions]

To publish on every push to `main`:

1. Add the token as a repository secret named `DROPSHELF_TOKEN`.
2. Save this workflow as `.github/workflows/dropshelf.yml`, with your drop's slug in place of
   `team-lunch`:

   ```yaml
   name: Publish to dropshelf
   on:
     push:
       branches: [main]

   concurrency: dropshelf-team-lunch

   jobs:
     publish:
       runs-on: ubuntu-latest
       env:
         DROPSHELF_URL: https://drop.example.com
         DROPSHELF_TOKEN: ${{ secrets.DROPSHELF_TOKEN }}
       steps:
         - uses: actions/checkout@v4
         - uses: actions/setup-node@v4
           with:
             node-version: 22
         - run: npm ci
         - run: npm run build
         - name: Install the dropshelf CLI
           run: |
             curl -fsSL "$DROPSHELF_URL/install.sh" | DROPSHELF_NO_LOGIN=1 sh
             echo "$HOME/.local/bin" >> "$GITHUB_PATH"
         - name: Publish
           working-directory: dist
           run: |
             dropshelf link team-lunch
             dropshelf push --publish --note "Build $GITHUB_RUN_NUMBER of ${GITHUB_SHA::7}" --json
   ```

3. Push to `main`. The workflow publishes a new version of the drop.

`concurrency` runs one publish at a time. Two runs that overlap would otherwise both start from the
same live version, and the second would stop with exit code `4`.

## Publish with GitLab CI [#publish-with-gitlab-ci]

To publish from the default branch:

1. Add the token as a CI/CD variable named `DROPSHELF_TOKEN` in the project's settings, protected and
   masked.
2. Add this job to `.gitlab-ci.yml`, with your drop's slug in place of `team-lunch`:

   ```yaml
   publish:
     image: node:22
     rules:
       - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
     resource_group: dropshelf-team-lunch
     variables:
       DROPSHELF_URL: https://drop.example.com
     script:
       - npm ci
       - npm run build
       - curl -fsSL "$DROPSHELF_URL/install.sh" | DROPSHELF_NO_LOGIN=1 sh
       - export PATH="$HOME/.local/bin:$PATH"
       - cd dist
       - dropshelf link team-lunch
       - dropshelf push --publish --note "Pipeline $CI_PIPELINE_IID of $CI_COMMIT_SHORT_SHA" --json
   ```

3. Push to the default branch. The job publishes a new version of the drop.

`resource_group` runs one publish at a time, for the same reason as `concurrency` on GitHub.

## Preview each branch [#preview-each-branch]

A branch can publish to a drop of its own, named after the branch. A slug is 1 to 53 lowercase
letters, digits and hyphens. It cannot start or end with a hyphen or contain `--`.

This script turns the branch name into a slug, then creates the drop on the first run and updates it
on later runs. Run it in the build folder, with `BRANCH` set to the branch name:

```bash
slug="preview-$(printf '%s' "$BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//' | cut -c1-40 | sed -E 's/-+$//')"
if dropshelf get "$slug" > /dev/null 2>&1; then
  dropshelf link "$slug"
  dropshelf push --publish --note "Build of $BRANCH" --json
else
  dropshelf push --type site --slug "$slug" --retention 7d --note "Build of $BRANCH" --json
fi
```

The first push creates the drop and goes live. `--retention 7d` makes a forgotten preview expire on
its own, within your instance's retention limits. Set `BRANCH` from `$GITHUB_REF_NAME` on GitHub, or
from `$CI_COMMIT_REF_NAME` on GitLab.

To delete the preview drop when the branch is deleted, run:

```bash
dropshelf rm "$slug" --yes
```

On GitHub, run this in a workflow triggered `on: delete`, with `BRANCH` set from
`${{ github.event.ref }}`. On GitLab, run it in a stop job for the branch's environment.

<Callout type="warn">
`dropshelf rm` deletes the drop, its versions, stored data and uploaded files, and frees its address.
It cannot be undone.
</Callout>

## Confirm it worked [#confirm-it-worked]

With `--json`, the push prints one JSON document on stdout. It names the drop's `slug`, the new
`version`, and `"published": true`.

Open `https://team-lunch.drop.example.com` to see the new version. A failed push exits with a code
other than `0` and prints an `error` document. [Exit codes](/docs/reference/cli#exit-codes) says what
each code means.

## Next steps [#next-steps]

- [The dropshelf CLI](/docs/agents/cli)
- [CLI reference: push](/docs/reference/cli#push)
- [Personal access tokens](/docs/use/tokens)
- [Versions and drafts](/docs/use/versions)
- [Retention and Forever](/docs/use/retention)
