CI/CD Experimental

GitHub Actions and GitLab CI pipelines that check, build, push and deploy a velyr-pack app.

A pipeline for a velyr-pack app has four steps:

  1. Check that the stack files match velyr.toml (velyr-pack compose -check).
  2. Build front-end assets, if the app has any.
  3. Build and push the image with pack build --publish.
  4. Deploy the stack and wait for the rollout.

The examples pin every version. Change the release and PHP minor in one place when you upgrade.

Pin the versions#

WhatExampleWhere it comes from
velyr-pack releasev0.1.0-alpha.4GitHub Releases
PHP minor8.5your composer.json
pack0.40.11buildpacks/pack releases

The builder tag is <release>-php<minor>, and the run image must use the same tag.

GitHub Actions#

yaml
# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches: [main]

permissions:
  contents: read
  packages: write

env:
  VELYR_TAG: v0.1.0-alpha.4-php8.5
  VELYR_PACK_VERSION: v0.1.0-alpha.4
  PACK_VERSION: "0.40.11"
  PACK_SHA256: 280a53ff6526f1aa26cc903b5c5aab515afef25e3d7abac977fe193331d3b301
  IMAGE: ghcr.io/${{ github.repository }}

jobs:
  check:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - name: Install velyr-pack
        run: |
          curl -fsSLo velyr-pack "https://github.com/velyrlabs/velyr-pack/releases/download/$VELYR_PACK_VERSION/velyr-pack-linux-amd64"
          curl -fsSLO "https://github.com/velyrlabs/velyr-pack/releases/download/$VELYR_PACK_VERSION/SHA256SUMS"
          grep ' velyr-pack-linux-amd64$' SHA256SUMS | sed 's/velyr-pack-linux-amd64/velyr-pack/' | sha256sum -c -
          sudo install -m 0755 velyr-pack /usr/local/bin/velyr-pack
      - run: velyr-pack compose -check production

  build:
    needs: check
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm ci && npm run build          # public/build must exist before pack build
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Install pack
        run: |
          curl -fsSLo /tmp/pack.tgz "https://github.com/buildpacks/pack/releases/download/v$PACK_VERSION/pack-v$PACK_VERSION-linux.tgz"
          echo "$PACK_SHA256  /tmp/pack.tgz" | sha256sum -c -
          sudo tar -xzf /tmp/pack.tgz -C /usr/local/bin pack
      - name: Build and push
        run: |
          pack build "$IMAGE:${{ github.sha }}" \
            --builder "ghcr.io/velyrlabs/velyr-php-builder:$VELYR_TAG" \
            --cache-image "$IMAGE:cnb-cache" \
            --publish

  deploy:
    needs: build
    runs-on: ubuntu-24.04
    environment: production
    steps:
      - uses: actions/checkout@v4
      - name: Deploy over SSH
        env:
          DOCKER_HOST: ssh://deploy@${{ secrets.SWARM_MANAGER }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          mkdir -p ~/.ssh && echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan "${{ secrets.SWARM_MANAGER }}" >> ~/.ssh/known_hosts
          docker stack deploy --with-registry-auth -c stack.yml shop

GitLab CI#

yaml
# .gitlab-ci.yml
stages: [check, build, deploy]

variables:
  VELYR_TAG: v0.1.0-alpha.4-php8.5
  VELYR_PACK_VERSION: v0.1.0-alpha.4
  PACK_VERSION: "0.40.11"
  PACK_SHA256: 280a53ff6526f1aa26cc903b5c5aab515afef25e3d7abac977fe193331d3b301

check:
  stage: check
  image: alpine:3.24
  script:
    - wget -qO /usr/local/bin/velyr-pack "https://github.com/velyrlabs/velyr-pack/releases/download/$VELYR_PACK_VERSION/velyr-pack-linux-amd64"
    - chmod +x /usr/local/bin/velyr-pack
    - velyr-pack compose -check production

build:
  stage: build
  image: docker:27
  services: [docker:27-dind]
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_VERIFY: "1"
    DOCKER_CERT_PATH: /certs/client
  before_script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
    - wget -qO /tmp/pack.tgz "https://github.com/buildpacks/pack/releases/download/v$PACK_VERSION/pack-v$PACK_VERSION-linux.tgz"
    - echo "$PACK_SHA256  /tmp/pack.tgz" | sha256sum -c -
    - tar -xzf /tmp/pack.tgz -C /usr/local/bin pack
  script:
    - pack build "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
        --builder "ghcr.io/velyrlabs/velyr-php-builder:$VELYR_TAG"
        --cache-image "$CI_REGISTRY_IMAGE:cnb-cache"
        --publish --trust-builder
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

Front-end assets: add a job that runs npm ci && npm run build and passes public/build to the build job as an artifact.

--trust-builder lets the lifecycle run all phases in one container, which is faster under Docker-in-Docker. Only use it with builders you trust, pinned to a release.

Caching#

--cache-image <image>:cnb-cache stores the buildpack's cache layers (the Composer cache) in the registry, so a fresh runner does not download every package again. Keep one cache image per app and branch family; it is overwritten on every build.

--publish pushes the image layers straight to the registry instead of going through the local Docker daemon, and reuses layers that are already there.

Deploy#

velyr-pack does not deploy. Common options:

  • docker stack deploy over SSH, as in the GitHub example. --with-registry-auth forwards the registry login to the nodes so they can pull a private image.
  • A platform webhook, for stacks managed by Dokploy or Portainer: the pipeline pushes the image and calls the webhook, and the platform runs docker stack deploy with the stack file it holds. Keep that stack file in sync with the generated one; compose -check only checks the copy in your repository.

Tag images with the commit SHA and pass it to the stack (image: ghcr.io/you/shop:${IMAGE_TAG}). A moving tag like :prod also works, but the stack spec does not change between deploys, so Swarm only rolls out if the deploy resolves the tag to a new digest.

Wait for the rollout#

docker stack deploy returns before Swarm replaces any task, so a green deploy job does not mean the new version is running. Poll each service's update state until it settles:

sh
#!/bin/sh
# wait-rollout.sh <stack> [timeout-seconds]: fail if any service of the stack rolls back or pauses.
set -eu
stack=$1 deadline=$(( $(date +%s) + ${2:-900} ))
sleep 5   # Swarm sets UpdateStatus asynchronously: give it time to replace the previous deploy's "completed"
while :; do
  pending=0
  for svc in $(docker stack services -q "$stack"); do
    state=$(docker service inspect "$svc" --format '{{if .UpdateStatus}}{{.UpdateStatus.State}}{{end}}')
    case $state in
      ""|completed) ;;
      updating|rollback_started) pending=1 ;;
      *) echo "$(docker service inspect "$svc" --format '{{.Spec.Name}}'): $state" >&2; exit 1 ;;
    esac
  done
  [ "$pending" = 0 ] && exit 0
  [ "$(date +%s)" -lt "$deadline" ] || { echo "rollout timed out" >&2; exit 1; }
  sleep 10
done

Run it right after docker stack deploy with the same DOCKER_HOST. A service the deploy did not change keeps the state of its last update, usually completed. Leave enough time for the slowest web service: start_period plus the monitor window, times the number of replicas.