For developers
CI/CD configuration
3 min
the approach recommended by hosty to deploy the code and manage environemnts is through ci/cd platforms like circleci, travisci, github actions and similar hosty has a special user role called release manager who has permissions to create, update and delete environments on the hosting it was designed specifically for use in ci/cd platforms the recommended deployment workflow consists of several subsequent steps checkout the git branch and install project dependencies install and configure hosty cli install vpn client and connect to the cluster's vpn build and deploy the code run post deploy scripts disconnect from vpn example this is simplified example is made using syntax for circleci platform, however, similar steps can be applied to any other ci/cd platform in this example ci uses compose yaml file in a project root, which will be used by docker compose to perform operations via hosty cli services hosty cli \# we highly encourage you to use a stable tag \# instead of the "latest" list of tags can be found at \# https //hub docker com/r/systemseed/hosty/tags image systemseed/hosty\ latest volumes \# required the root folder of the project must be mapped to \# the /src/project inside the hosty container \# it is used to build images, track changes, etc \# below is an example mapping assumed that compose yml file \# is in the project root (where git folder is) \ / /src/project \# in order to run docker commands inside of the hosty \# container we need to have access to the docker socket of \# the host machine \# the location of docker socket can be different, depending on \# your os \ /var/run/docker sock /var/run/docker sock \# mapping the folder with aws credentials received from \# the hosting administrators note that the folder does not \# have to be relative to your home folder and can be located \# in any other place, as long as it contains credentials file \# with the correct aws configuration for the hosty user \# profile \ / aws\ /src/ aws in order to store aws access key, secret key and vpn configuration profile we use environmental variables access keys of a dedicated user with release manager role are stored in aws access key id and aws secret access key variables the vpn configuration profile file is encoded using base64 ( base64 i ovpn o encoded txt ) and stored in aws vpn client config variable here are the contents of circleci/config yml file version 2 1 jobs build and deploy machine image ubuntu 2204 2024 05 1 steps \ checkout \# install openvpn \ run name install openvpn client required to connect to the hosting command | sudo apt get update sudo apt get install openvpn \# use base64 to decode vpn configuration profile \# and connect to vpn \ run name open vpn connection background true command | echo $aws vpn client config | base64 decode > /tmp/config ovpn sudo openvpn config /tmp/config ovpn > /tmp/openvpn log \# make sure vpn connection is established \ run name wait for vpn connection to establish command | counter=1 until \[ f /tmp/openvpn log ] && \[ "$(grep c "initialization sequence completed" /tmp/openvpn log)" != 0 ] || \[ "$counter" ge 5 ]; do ((counter++)) echo "attempting to connect to vpn server " sleep 1; done if \[ ! f /tmp/openvpn log ] || (! grep iq "initialization sequence completed" /tmp/openvpn log); then printf "\nunable to establish connection within the allocated time > giving up \n" exit 1; else printf "\nvpn connected\n" fi \# pull hosty image from docker hub \ run name pull docker image with hosty cli command docker compose pull hosty cli \# configure aws access keys \ run name set aws access & secret keys command | docker compose run rm hosty cli aws configure set aws access key id $aws access key id profile hosty user docker compose run rm hosty cli aws configure set aws secret access key $aws secret access key profile hosty user \# determine environment name based on the branch name, where \# `main` branch is production and for other branches use git branch name as is \ run name determine the correct environment name for hosty deployments command | \[\[ $circle branch = "main" ]] && env name="production" || env name="$circle branch" echo "export env name='$env name'" >> $bash env \ run name install external dependencies, build assets & run code quality checks command | echo "checking if aws already contains docker image with the current build " result=$(docker compose run rm hosty hosty build exists $env name) if \[ $result = "exists" ]; then echo "docker images for the current build already exist no need to proceed with building assets " else echo "docker images for the current build do not exist starting to build assets " \# here it a perfect place to download app dependencies \# and build app assets if build doesn't exist \# i e composer install, npm install and npm run build \# it is important to delete the files not required by \# application to run for example as "node modules" \# folder, for building assets fi \# build docker image(s) for the environment \ run name build & push project docker image(s) to the hosting command docker compose run rm hosty cli hosty build push $env name v \# create or update environment on the hosting \ run name deploy the environment to the hosting no output timeout 1800 command docker compose run rm hosty cli hosty deploy $env name v \# perform post deployment commands for the deployed environment \ run name execute post deploy commands command docker compose run rm hosty cli hosty exec $env name command "ls la" \# get environment url \ run name add url of the deployed environment to the local environment variable command docker compose run rm hosty cli hosty domains $env name | sed 's/\[^ ] /https //&/g' | awk '{print "backend url="$1}' >> $bash env \# display success message \ run name display deployed message command echo "deployment completed ($circle branch) url $backend url" \# disconnect from vpn \ run name disconnect from vpn command sudo killall openvpn || true when always \# store logs in case vpn connection failed \ store artifacts path /tmp/openvpn log when on fail workflows version 2 deploy jobs \ build and deploy