2 Commits
v2.0 ... v3

Author SHA1 Message Date
683eb2fc8c Update README 2024-07-11 11:10:33 +02:00
1ffc07c62f Add support for docker swarm 2024-07-02 10:12:48 +02:00
3 changed files with 48 additions and 10 deletions

View File

@ -11,7 +11,8 @@ This action decrypts a repository that has been encrypted with git-crypt, and th
* `ssh_user` - Username to use when connecting to the remote host.
* `docker_compose_project` - Name of the docker compose project. This will be used as the prefix by docker.
* `docker_compose_filename` - The filename of the compose file. Defaults to docker-compose.yml
* `docker_compose_down` - if set to `true`, the action will execute `docker compose down`.
* `action` - defaults to `up`. With `down`, `docker compose down` will be run.
* `swarm_mode` - if true, the project will be deployed as a stack.
## Getting the git crypt key
The key can be extracted from an unlocked repository with the following command:

View File

@ -21,15 +21,20 @@ inputs:
ssh_user:
description: Remote user name.
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:
description: Compose project name
required: true
docker_compose_filename:
description: Compose file to use
default: docker-compose.yml
docker_compose_down:
description: Undeploy project instead of creating it ("true" or "false").
default: "false"
runs:
using: docker
@ -42,4 +47,5 @@ runs:
SSH_USER: ${{ inputs.ssh_user }}
DOCKER_COMPOSE_PROJECT: ${{ inputs.docker_compose_project }}
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")
}
compose_run() {
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"
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"
}
if "$DOCKER_COMPOSE_DOWN"; then
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"
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\""
}
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
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
@ -57,4 +88,4 @@ log "Starting deployment main function."
unlock
compress_workdir
start_ssh_agent
compose_run
deploy