CI/CD with GitHub Actions

Review Spica changes as plans on pull requests and apply them to staging and production when they merge, with GitHub Actions and the Spica CLI.

Keep your project's resources in the repository (see Resources as code) and let CI deploy them:

  • On pull requests, spica plan shows what would change.
  • On merge to main, spica apply updates staging, then production after approval.

Repository layout

Text
.
├── spica/
│   ├── bucket/
│   ├── function/
│   ├── policy/
│   ├── env-var/
│   └── secret/
└── .github/workflows/spica.yml

Secrets and variables

GitHub settingValue
Environment staging: secret SPICA_API_KEY, variable SPICA_URLAn API key for the CLI and the staging API URL.
Environment production: same namesProduction values. Add required reviewers to the environment.
Secrets such as STRIPE_KEY per environmentExposed to the CLI as SPICA_VALUE_<KEY>.

The API keys need the full-access policies of the modules you manage: buckets, functions, policies, environment variables and secrets.

Workflow

.github/workflows/spica.yml
name: Spica

on:
  pull_request:
    paths: ["spica/**"]
  push:
    branches: [main]
    paths: ["spica/**"]

jobs:
  plan:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm install -g @spica/cli@0.22.1
      - name: Connect
        run: |
          spica context set --name ci --url "${{ vars.SPICA_URL }}" --apikey "${{ secrets.SPICA_API_KEY }}"
          spica context switch ci
      - name: Plan
        working-directory: spica
        env:
          SPICA_VALUE_STRIPE_KEY: ${{ secrets.STRIPE_KEY }}
        run: |
          set +e
          spica plan --detailed --detailed-exitcode > plan.txt
          code=$?
          sed -i 's/\x1b\[[0-9;]*m//g' plan.txt
          set -e
          { echo '```diff'; cat plan.txt; echo '```'; } >> "$GITHUB_STEP_SUMMARY"
          if [ "$code" -ne 0 ] && [ "$code" -ne 2 ]; then exit "$code"; fi

  deploy:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 1
      matrix:
        environment: [staging, production]
    environment: ${{ matrix.environment }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm install -g @spica/cli@0.22.1
      - name: Connect
        run: |
          spica context set --name ci --url "${{ vars.SPICA_URL }}" --apikey "${{ secrets.SPICA_API_KEY }}"
          spica context switch ci
      - name: Apply
        working-directory: spica
        env:
          SPICA_VALUE_STRIPE_KEY: ${{ secrets.STRIPE_KEY }}
        run: spica apply --auto-approve --abort-on-error

spica plan --detailed-exitcode exits with 2 when there are changes. The plan step above accepts 0 and 2 and fails on real errors, removes terminal color codes, and writes the plan to the job summary for reviewers.

Practices that keep deployments safe

  • Review deletions. A resource missing from the folder is deleted on apply, together with a bucket's documents. Treat - lines in the plan as red flags.
  • Pin versions. Use the same CLI version as your instances.
  • Protect production. Require reviewers on the production environment, so apply waits for approval after staging succeeded.
  • One source of truth. Don't edit managed resources in the production panel. Changes made there are overwritten by the next apply, or show up in the next plan.
  • Values outside git. Pass secret values through SPICA_VALUE_* variables and never commit .spica/.

For full control, for example to approve plans in your own tool before applying them, use @spica-devkit/sync, which plans from a repository archive and applies only the plan that was approved.