Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM alpine:3.8
LABEL maintainer="[email protected]"
FROM alpine:latest
RUN apk add --no-cache openssh bash
ADD entrypoint.sh /entrypoint.sh
WORKDIR /github/workspace
Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Simple github action to run docker-compose on remote host.

This action packs contents of the action workspace into archive.
Logs into remote host via ssh. Unpacks the workspace there and runs
`docker-compose up -d` command.
`docker-compose up -d` command.
The connection is attempted to be kept alive using a `ServerAliveInterval`
of 100 with SSH, which may help with long deploys.

Comparing to other actions with similar behavior this one does not use any
unknown docker-images. It is entirely built from Dockerfile on top of
Expand All @@ -20,6 +22,7 @@ unknown docker-images. It is entirely built from Dockerfile on top of
container will have this prefix in name.
* `docker_compose_filename` - Path to the docker-compose file in the repository.
* `use_stack` - Use docker stack instead of docker-compose.
* `docker_compose_down` - Execute docker-compose-down.

# Usage example

Expand Down Expand Up @@ -124,3 +127,28 @@ jobs:
use_stack: 'true'
```

# Down deploy (Docker-compose down)
If you need to run a docker-compose down to do a clean rollback. Only one down of the
services will be executed To do that just set `docker_compose_down` input to `"true"`:
```
name: Deploy
on:
push:
branches: [ master ]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- actions/chockout@v2

- uses: alex-ac/github-action-ssh-docker-compose@master
name: Docker-Stack Remote Deployment
with:
ssh_host: example.com
ssh_private_key: ${{ secrets.EXAMPLE_COM_SSH_PRIVATE_KEY }}
ssh_user: ${{ secrets.EXAMPLE_COM_SSH_USER }}
docker_compose_prefix: example.com
docker_compose_down: 'true'
```
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: SSH-Compose
description: SSH into host and deploy repository with Docker-Compose
branding:
icon: server
color: purple
inputs:
ssh_private_key:
description: Private SSH key used for logging into remote system.
Expand All @@ -23,6 +26,9 @@ inputs:
use_stack:
description: Use docker stack instead of docker compose ("true" or "false").
default: 'false'
docker_compose_down:
description: Execute docker-compose-down ("true" or "false").
default: 'false'
runs:
using: docker
image: Dockerfile
Expand All @@ -34,3 +40,4 @@ runs:
DOCKER_COMPOSE_FILENAME: ${{ inputs.docker_compose_filename }}
DOCKER_COMPOSE_PREFIX: ${{ inputs.docker_compose_prefix }}
USE_DOCKER_STACK: ${{ inputs.use_stack }}
DOCKER_COMPOSE_DOWN: ${{ inputs.docker_compose_down }}
9 changes: 7 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ tar cjvf /tmp/workspace.tar.bz2 --exclude .git .
log "Launching ssh agent."
eval `ssh-agent -s`

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_PREFIX\" up -d --remove-orphans --build"
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_PREFIX\" pull ; docker compose -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PREFIX\" up -d --remove-orphans --build"
if $USE_DOCKER_STACK ; then
remote_command="set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir -p \"\$HOME/workspace/$DOCKER_COMPOSE_PREFIX\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace/$DOCKER_COMPOSE_PREFIX\" -xjv ; log 'Launching docker stack deploy...' ; cd \"\$HOME/workspace/$DOCKER_COMPOSE_PREFIX\" ; docker stack deploy -c \"$DOCKER_COMPOSE_FILENAME\" --prune \"$DOCKER_COMPOSE_PREFIX\""
fi
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_PREFIX\" down"
fi

ssh-add <(echo "$SSH_PRIVATE_KEY")

echo ">> [local] Built command: ${remote_command}"
echo ">> [local] Connecting to remote host."
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=100 \
"$SSH_USER@$SSH_HOST" -p "$SSH_PORT" \
"$remote_command" \
< /tmp/workspace.tar.bz2