6 Commits
v1.1.1 ... v4

Author SHA1 Message Date
ad80faf177 Try other variation with quotes 2024-11-03 14:25:46 +01:00
b7354a0c33 Try without quotes 2024-11-03 14:13:49 +01:00
428522f85d Support custom actions 2024-11-03 13:27:34 +01:00
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
322875b078 Fix typo 2024-07-01 14:56:24 +02:00
3 changed files with 69 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. * `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_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_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 ## Getting the git crypt key
The key can be extracted from an unlocked repository with the following command: The key can be extracted from an unlocked repository with the following command:

View File

@ -21,15 +21,26 @@ 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
custom_container_action:
description: "Run a custom action"
required: false
custom_container_action_target:
description: "Target container for action"
required: false
action:
description: Action, i.e., "up" or "down", or "custom"
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 +53,7 @@ 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 }}
CUSTOM_ACTION: ${{ inputs.custom_container_action }}
CUSTOM_ACTION_TARGET: ${{ inputs.custom_container_action_target }}

View File

@ -36,16 +36,60 @@ 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_PREFIX\" 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\""
}
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 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 +101,4 @@ log "Starting deployment main function."
unlock unlock
compress_workdir compress_workdir
start_ssh_agent start_ssh_agent
compose_run deploy