Add support for docker swarm

This commit is contained in:
2024-07-02 10:12:48 +02:00
parent 322875b078
commit 1ffc07c62f
2 changed files with 46 additions and 9 deletions

View File

@ -21,15 +21,20 @@ inputs:
ssh_user: ssh_user:
description: Remote user name. description: Remote user name.
required: true required: true
swarm_mode:
description: Use swarm mode instead of docker compose
default: "false"
required: true
action:
description: Action, i.e., "up" or "down"
default: "up"
required: true
docker_compose_project: docker_compose_project:
description: Compose project name description: Compose project name
required: true required: true
docker_compose_filename: docker_compose_filename:
description: Compose file to use description: Compose file to use
default: docker-compose.yml default: docker-compose.yml
docker_compose_down:
description: Undeploy project instead of creating it ("true" or "false").
default: "false"
runs: runs:
using: docker using: docker
@ -42,4 +47,5 @@ runs:
SSH_USER: ${{ inputs.ssh_user }} SSH_USER: ${{ inputs.ssh_user }}
DOCKER_COMPOSE_PROJECT: ${{ inputs.docker_compose_project }} DOCKER_COMPOSE_PROJECT: ${{ inputs.docker_compose_project }}
DOCKER_COMPOSE_FILENAME: ${{ inputs.docker_compose_filename }} DOCKER_COMPOSE_FILENAME: ${{ inputs.docker_compose_filename }}
DOCKER_COMPOSE_DOWN: ${{ inputs.docker_compose_down }} DEPLOY_ACTION: ${{ inputs.action }}
SWARM_MODE: ${{ inputs.swarm_mode }}

View File

@ -36,16 +36,47 @@ start_ssh_agent() {
ssh-add <(echo "$SSH_PRIVATE_KEY") ssh-add <(echo "$SSH_PRIVATE_KEY")
} }
compose_run() { compose_up() {
local remote_command="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" 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"
}
if "$DOCKER_COMPOSE_DOWN"; then compose_down() {
remote_command="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" 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\""
}
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)
else
log "Removing docker compose project ${DOCKER_COMPOSE_PROJECT}."
remote_command=$(compose_down)
fi
fi fi
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
"$SSH_USER@$SSH_HOST" -p "$SSH_PORT" \ "$SSH_USER@$SSH_HOST" -p "$SSH_PORT" \
"$remote_command" \ "$remote_command" \
< /tmp/workspace.tar.bz2 < /tmp/workspace.tar.bz2
} }
if [ -n "${RUN_DIR:-}" ]; then if [ -n "${RUN_DIR:-}" ]; then
@ -57,4 +88,4 @@ log "Starting deployment main function."
unlock unlock
compress_workdir compress_workdir
start_ssh_agent start_ssh_agent
compose_run deploy