CI/CD configuration
Environments are built and deployed by CI, such as CircleCI or GitHub Actions. Only users with the release manager role can push images and deploy, so people don't deploy from their computers. This page shows how to set up a CI job, with a complete example for CircleCI.

What you need from the admins
- Access keys of a release manager: an AWS user for CI, with the release manager role in your project. It works without two-factor authentication, and has no access to backups.
- The VPN profile of that user (<user>.<cluster>.ovpn). The Kubernetes API of the cluster is private, so deploy, exec, run, logs and domains work only through the VPN.
Store them as secret variables of your CI project:
Variable | Value |
|---|---|
AWS_ACCESS_KEY_ID | Access key of the release manager. |
AWS_SECRET_ACCESS_KEY | Secret key of the release manager. |
AWS_VPN_CLIENT_CONFIG | The VPN profile in base64: base64 -i <user>.<cluster>.ovpn. |
Add the secrets of your application too: see Sensitive environment variables.
Pin the version of Hosty CLI
Run Hosty CLI from its Docker image with a fixed release, so that every build uses the same version. Keep the version in .env in the root of the project, which Docker Compose reads:
# Releases: https://hub.docker.com/r/systemseed/hosty/tags
HOSTY_TAG=1.5.2And add the service to compose.yml in the root of the project:
services:
hosty:
image: systemseed/hosty:v${HOSTY_TAG}
volumes:
# The project. Hosty CLI reads .hosty/hosty.yaml and builds the images from it.
- ./:/src/project
# The Docker of the machine, which builds the images.
- /var/run/docker.sock:/var/run/docker.sock
# AWS access keys (the hosty.user profile).
- ~/.aws:/src/.aws
# Access to the cluster, kept between commands.
- ~/.kube:/root/.kubeUpdate HOSTY_TAG when Hosty releases a new version. A new minor release, i.e. 1.6.0 after 1.5.2, stops the commands of older versions until you update. A new patch release is only announced. For an urgent fix of production before the update, you can skip the check once:
docker compose run --rm -e HOSTY_SKIP_VERSION_CHECK=1 hosty hosty deploy productionSteps of a deployment
- Check out the code and write the secrets into their files.
- Name the environment after the branch, i.e. production for main.
- Save the access keys in the hosty.user profile of AWS CLI.
- Check whether the images of this commit already exist (hosty build --exists). If not, build the assets of your application that the build hook doesn't, and run hosty build --push.
- Connect the VPN.
- Deploy with hosty deploy.
- Run the post-deployment commands, i.e. drush deploy, with hosty exec or hosty run.
- Disconnect the VPN, even when a step fails.
Names of environments
Hosty names an environment by the argument of the commands. A name is up to 24 characters: letters, digits, - and _, starting and ending with a letter or a digit. Hosty turns it into lowercase and _ into -. Only production is production.
Branch names often have / (feature/login), so turn them into valid names in CI. Don't turn them into lowercase yourself: Hosty refuses a name like Production, which would otherwise deploy to production.
Timeouts
A deployment waits up to 30 minutes. The first deployment of an environment imports the backups and waits up to import.timeout from hosty.yaml. Hosty prints nothing while it waits, so give the deploy step of your CI a timeout without output that is longer than that.
When a deployment fails
If a job is cancelled during a deployment, Helm keeps the environment locked, and the next deployment fails. If the first deployment of an environment fails, its backups may not be imported. In both cases the error of Hosty CLI shows the command that fixes it.
CircleCI example
The job builds and deploys every branch: main to production, other branches to development environments.
version: 2.1
commands:
vpn_connect:
steps:
- run:
name: Install OpenVPN
command: |
sudo apt-get update
sudo apt-get install -y openvpn
- run:
name: Connect the VPN
background: true
command: |
echo "$AWS_VPN_CLIENT_CONFIG" | base64 --decode > /tmp/config.ovpn
sudo openvpn --config /tmp/config.ovpn > /tmp/openvpn.log
- run:
name: Wait for the VPN connection
command: |
for i in $(seq 1 30); do
grep -q "Initialization Sequence Completed" /tmp/openvpn.log 2>/dev/null && exit 0
sleep 1
done
echo "The VPN didn't connect in 30 seconds."
exit 1
jobs:
build_and_deploy:
machine:
image: ubuntu-2404:current
steps:
- checkout
- run:
name: Name the environment
command: |
if [ "$CIRCLE_BRANCH" = "main" ]; then
ENV_NAME=production
else
# Letters, digits, "-" and "_", up to 24 characters.
ENV_NAME=$(echo "$CIRCLE_BRANCH" | sed -E 's/[^A-Za-z0-9_-]+/-/g' | cut -c1-24 | sed -E 's/^[-_]+|[-_]+$//g')
if [ "$ENV_NAME" = "production" ]; then
echo "Only main deploys to production."
exit 1
fi
fi
echo "export ENV_NAME=$ENV_NAME" >> "$BASH_ENV"
- run:
name: Write the secrets into .env.secrets
command: |
printf '%s=%s\n' \
API_KEY "$API_KEY" \
> .env.secrets
- run:
name: Set up Hosty CLI
command: |
docker compose pull hosty
mkdir -p ~/.aws ~/.kube
docker compose run --rm hosty aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile hosty.user
docker compose run --rm hosty aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile hosty.user
- run:
name: Build and push the images
command: |
if docker compose run --rm hosty hosty build --exists "$ENV_NAME"; then
echo "The images of this commit exist already."
else
# Build the assets the build hook doesn't, i.e. "npm ci && npm run build",
# and remove what the application doesn't need to run, i.e. node_modules.
docker compose run --rm hosty hosty build --push "$ENV_NAME" -v
fi
- vpn_connect
- run:
name: Deploy
no_output_timeout: 60m
command: docker compose run --rm hosty hosty deploy "$ENV_NAME" -v
- run:
name: Run the post-deployment commands
command: docker compose run --rm hosty hosty exec "$ENV_NAME" --no-tty --command "drush deploy"
- run:
name: Show the domains
command: echo "Deployed to https://$(docker compose run --rm hosty hosty domains "$ENV_NAME" | awk '{print $1}')"
- run:
name: Disconnect the VPN
command: sudo killall openvpn || true
when: always
- store_artifacts:
path: /tmp/openvpn.log
when: on_fail
workflows:
deploy:
jobs:
- build_and_deployNotes on the example:
- hosty build --exists exits with 0 when all images exist, so the job skips the build of the assets for a commit that is already built, i.e. when the job is run again.
- hosty build needs the files of environmentFiles, so write the secrets before it.
- hosty exec runs the command in a running pod of the application. For a long command, hosty run <environment> -- drush deploy runs it in a pod of its own instead and exits with its exit code.
- hosty domains prints the domains of the environment, separated by spaces.
- To keep the caches of Composer and npm between builds, turn on Docker layer caching of the machine (docker_layer_caching: true).
- To delete the development environment of a merged branch, run hosty delete <environment> --yes from a job. Production can't be deleted with Hosty CLI.