Skip to content

Instantly share code, notes, and snippets.

@kazqvaizer
Last active March 16, 2023 12:13
Show Gist options
  • Save kazqvaizer/aeb6951841fa54f0cd61e8d97c329e9a to your computer and use it in GitHub Desktop.
Save kazqvaizer/aeb6951841fa54f0cd61e8d97c329e9a to your computer and use it in GitHub Desktop.
Way to run commands and deploy services in docker swarm stack.
#!/bin/bash
#
# Update whole docker swarm stack from CI job and wait untill success.
# Inits rollback process in case of unsuccessfull deployment.
#
# Requires another script from here: https://github.com/sudo-bmitch/docker-stack-wait
#
# By: Alexey Chudin <kazqvaizer@gmail.com>
# License: MIT
#
# Example:
#
# ssh SERVER "/srv/deploy-and-wait.sh STACK_NAME"
#
set -e # die on error
STACK_NAME=$1
if [ "$STACK_NAME" == "" ]; then
echo "Please, specify first argument as your stack name."
exit
fi
if [ "$CIRCLE_SHA1" == "" ]; then
echo "Environment variable CI_SHA1 is required."
exit
fi
cd /srv/$STACK_NAME
cp docker-compose.prod.yml docker-compose.prod.yml.previous
envsubst < docker-compose.skel.yml > docker-compose.prod.yml
docker stack deploy -c docker-compose.prod.yml $STACK_NAME --with-registry-auth
/srv/docker-stack-wait.sh -t 600 $STACK_NAME
# WARNING! Never edit docker-compose.prod.yml directly, because it could be rewritten.
# Edit docker-compose.skel.yml instead.
#
# That is an example skeleton of stack that works with deploy-and-wait.sh
#
# CI_SHA1 environment variable will be substituted with actual docker image tag and
# stored in example.prod.yml to allow docker-stack-wait.sh to work with rollbacks
#
version: '3.8'
services:
app:
image: DOCKER_IMAGE:${CI_SHA1}
env_file: &env_file
- ./env
# The total time of all checks must be lower then update_config.monitor param
healthcheck: &healthcheck
interval: 15s
timeout: 15s
start_period: 5s
retries: 3
deploy:
replicas: 2
update_config: &update_config
parallelism: 2
delay: 10s
monitor: 60s
failure_action: rollback
order: start-first
max_failure_ratio: 0
resources: &resources_low
reservations:
cpus: '0.1'
memory: 128M
limits:
cpus: '0.25'
memory: 256M
placement: &placement_worker
constraints:
- node.role == worker
#!/bin/bash
#
# Helps to run commands from CI piplene jobs.
#
# By: Alexey Chudin <kazqvaizer@gmail.com>
# License: MIT
#
# Example:
#
# ssh SERVER "/srv/run-command.sh STACK_NAME DOCKER_IMAGE:${CI_SHA1} './manage.py migrate --noinput'"
#
STACK_NAME=$1
if [ "$STACK_NAME" == "" ]; then
echo "Please, specify first argument as your stack directory name."
exit
fi
DOCKER_IMAGE=$2
if [ "$DOCKER_IMAGE" == "" ]; then
echo "Please, specify second argument as docker image with tag."
exit
fi
COMMAND=$3
if [ "$COMMAND" == "" ]; then
echo "Please, specify third argument as command to run inside docker image for stack env file."
exit
fi
NETWORK=${STACK_NAME}_stacknet
cd /srv/$STACK_NAME
docker run --rm -t --network=$NETWORK --env-file env $DOCKER_IMAGE $COMMAND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment