forked from Eisingcloud/gitea-action-git-crypt-ssh-deploy
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad80faf177 | |||
| b7354a0c33 | |||
| 428522f85d | |||
| 683eb2fc8c | |||
| 1ffc07c62f | |||
| 322875b078 | |||
| 07e9b1654c | |||
| 73be248b4c | |||
| 5593d43959 |
@ -6,4 +6,4 @@ WORKDIR /gitea/workspace
|
|||||||
|
|
||||||
COPY entrypoint.sh .
|
COPY entrypoint.sh .
|
||||||
|
|
||||||
ENTRYPOINT ["/gitea/workspace.sh"]
|
ENTRYPOINT ["/gitea/workspace/entrypoint.sh"]
|
||||||
|
|||||||
@ -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:
|
||||||
|
|||||||
24
action.yml
24
action.yml
@ -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
|
||||||
@ -40,6 +51,9 @@ runs:
|
|||||||
SSH_HOST: ${{ inputs.ssh_host }}
|
SSH_HOST: ${{ inputs.ssh_host }}
|
||||||
SSH_PORT: ${{ inputs.ssh_port }}
|
SSH_PORT: ${{ inputs.ssh_port }}
|
||||||
SSH_USER: ${{ inputs.ssh_user }}
|
SSH_USER: ${{ inputs.ssh_user }}
|
||||||
DOCKER_COMPOSE_PRJECT: ${{ 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 }}
|
||||||
|
|||||||
54
entrypoint.sh
Normal file → Executable file
54
entrypoint.sh
Normal file → Executable 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
|
||||||
|
|||||||
Reference in New Issue
Block a user