#!/usr/bin/env bash set -eu log() { echo ">> [local]" $@ } cleanup() { set +e log "Killing ssh agent." ssh-agent -k log "Removing workspace archive." rm -f /tmp/workspace.tar.bz2 } trap cleanup EXIT unlock() { log "Unlocking encrypted git repository" echo "${GIT_CRYPT_KEY}" | base64 -d > ./git-crypt-key git-crypt unlock ./git-crypt-key rm ./git-crypt-key } compress_workdir() { log "Packing workspace into archive to transfer to remove machine." tar jcvf /tmp/workspace.tar.bz2 --exclude .git --exclude vendor . } start_ssh_agent() { log "Launching SSH agent" eval $(ssh-agent -s) ssh-add <(echo "$SSH_PRIVATE_KEY") } compose_up() { echo "set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace\" -xjv ; log 'Launching docker compose...' ; cd \"\$HOME/workspace\" ; docker compose -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PROJECT\" pull ; docker compose -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PROJECT\" up -d --remove-orphans --build" } compose_down() { echo "set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace\" -xjv ; log 'Launching docker compose...' ; cd \"\$HOME/workspace\" ; docker compose -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PROJECT\" down" } stack_up() { echo "set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" -xjv ; log 'Launching docker stack deploy...' ; cd \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" ; docker stack deploy -c \"$DOCKER_COMPOSE_FILENAME\" --prune \"$DOCKER_COMPOSE_PROJECT\"" } stack_down() { # It is not at all necessary to transfer and unpack the workspace here, but I'll do it anyway for simplicity's sake. echo "set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" -xjv ; log 'Launching docker stack rm...' ; cd \"\$HOME/workspace/$DOCKER_COMPOSE_PROJECT\" ; docker stack rm \"$DOCKER_COMPOSE_PROJECT\"" } custom_action() { echo "set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace\" -xjv ; log 'Launching docker compose...' ; cd \"\$HOME/workspace\" ; docker compose -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PROJECT\" run --rm $CUSTOM_ACTION_TARGET $CUSTOM_ACTION" } deploy() { local remote_command if $SWARM_MODE; then if [ "$DEPLOY_ACTION" == "up" ]; then log "Deploying docker swarm stack ${DOCKER_COMPOSE_PROJECT}." remote_command=$(stack_up) else log "Removing docker swarm stack ${DOCKER_COMPOSE_PROJECT}" remote_command=$(stack_down) fi else if [ "$DEPLOY_ACTION" == "up" ]; then log "Deploying docker compose project ${DOCKER_COMPOSE_PROJECT}." remote_command=$(compose_up) elif [ "$DEPLOY_ACTION" == "down" ]; then log "Removing docker compose project ${DOCKER_COMPOSE_PROJECT}." remote_command=$(compose_down) elif [ "$DEPLOY_ACTION" == "custom" ]; then if [ -z "$CUSTOM_ACTION" ] || [ -z "$CUSTOM_ACTION_TARGET" ]; then echo "ERROR: You must set custom_container_action and custom_container_action_target" exit 1 fi log "Running custom action on project ${DOCKER_COMPOSE_PROJECT}:" log "Action: ${CUSTOM_ACTION}" log "Target: ${CUSTOM_ACTION_TARGET}" remote_command=$(custom_action) fi fi ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ "$SSH_USER@$SSH_HOST" -p "$SSH_PORT" \ "$remote_command" \ < /tmp/workspace.tar.bz2 } if [ -n "${RUN_DIR:-}" ]; then cd ${RUN_DIR} log "Running action from ${RUN_DIR}" fi log "Starting deployment main function." unlock compress_workdir start_ssh_agent deploy