Skip to content

Instantly share code, notes, and snippets.

@guilhermegazzinelli
Last active January 31, 2023 21:53
Show Gist options
  • Save guilhermegazzinelli/badc3d6ab7ea77c2cec8b71338573a93 to your computer and use it in GitHub Desktop.
Save guilhermegazzinelli/badc3d6ab7ea77c2cec8b71338573a93 to your computer and use it in GitHub Desktop.
Deploy script to push files using rsync and update app based on docker-compose with migrations
#!/bin/bash
#### Script important variables
USR="user"
SERVER="0.0.0.0"
DEPLOY_PATH="/home/user/application_home"
LOCAL_PATH="<Current code path>"
# Color output
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo -n "$text"
}
#### GET CURRENT STATES
CURRENT=`pwd`
BASENAME=`basename "$CURRENT"`
PROD_BRANCH="master"
CURRENT_BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
cecho y "Verifica diretório: "
if [ "$BASENAME" != proma-psm ]; then
echo "Deve ser executado a partir da pasta raiz ( $LOCAL_PATH )"
exit 0;
fi
cecho g " OK\n"
cecho y "Verifica execução no branch '$PROD_BRANCH': "
if [ "$CURRENT_BRANCH" != "$PROD_BRANCH" ]; then
echo "Este script deve ser apenas executado no branch de produção ( '$PROD_BRANCH' )"
exit 0;
fi
cecho g " OK\n"
cecho g "______________________________________\n"
cecho y " Iniciando DEPLOY\n"
cecho g "======================================\n\n"
cecho b " ====>> Copia arquivos locais para o servidor\n\n\n"
rsync -cravz -P ./ "$USR@$SERVER:$DEPLOY_PATH"
cecho b "\n\n ====>> Run docker-compose\n\n "
ssh -tt $USR@$SERVER "cd $DEPLOY_PATH && DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose up -d --build && exit"
cecho b "\n\n ====>> Run MIGRATIONS\n\n "
ssh -tt $USR@$SERVER "cd $DEPLOY_PATH && DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose --profile migrate up migrate && exit"
version: '3.9'
volumes:
postgres_data:
redis:
services:
base: &base
image: ${APP_IMAGE:-app:latest}
build:
context: ./
args:
- RAILS_ENV=${RAILS_ENV:-production}
- RUBY_VERSION=${RUBY_VERSION:-27}
- bundle_without="development test"
env_file:
- "env/db.env"
- "env/app.env"
environment:
- RAILS_MASTER_KEY=${RAILS_MASTER_KEY}
links:
- db:db
depends_on:
- db
- redis
command: "echo 'Base Image - Not suposed to run'"
networks:
- app_private
profiles: ['disabled']
app:
<<: *base
command: "/sbin/my_init"
volumes:
- ../app_storage/:/home/app/storage/
ports:
- ${APP_EXT_PORT-8080}:80
restart: always
networks:
- public
- psm_private
profiles: []
sidekiq:
extends:
service: base
entrypoint: ""
command: "/sbin/my_init --skip-runit --skip-startup-files bundle exec sidekiq"
healthcheck:
test: sidekiqmon | grep $$(hostname) || exit 1
timeout: 45s
interval: 20s
retries: 10
migrate:
extends:
service: base
entrypoint: ""
command: "/sbin/my_init --skip-runit --skip-startup-files bundle exec rails db:migrate"
profiles: ["migrate"]
db:
image: postgres:${POSTGRES_VERSION:-14}
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- ${DB_EXT_PORT-5434}:5432
env_file:
- "env/db.env"
networks:
- app_private
restart: always
redis:
image: "redis:4.0-alpine"
command: redis-server
volumes:
- redis:/data
restart: always
networks:
- app_private
healthcheck:
test: redis-cli ping
interval: 1s
timeout: 3s
retries: 30
networks:
public:
external: true
psm_private:
driver: overlay
name: app_private
@guilhermegazzinelli
Copy link
Author

guilhermegazzinelli commented Jan 31, 2023

Based on a Ruby on rails app and Passenger/Nginx as webserver

  • Sidekiq and migrations disabled.
  • Need to run migration command, or do as follows
migrate:
  <<: *base
  entrypoint: ""
  command: "/sbin/my_init --skip-runit --skip-startup-files bundle exec rails db:migrate"
  profiles: []

This is necessary since extends always aggregate tags. For more info see: Adding and overriding configuration

To execute migrations:
docker-compose --profile migrate up migrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment