diff --git a/Dockerfile b/Dockerfile index 828c180..7bc0544 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ -FROM alpine:3.8 -LABEL maintainer="ya.alex-ac@yandex.com" +FROM alpine:latest RUN apk add --no-cache openssh bash ADD entrypoint.sh /entrypoint.sh WORKDIR /github/workspace diff --git a/README.md b/README.md index b59affb..f63f7fd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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' +``` diff --git a/action.yml b/action.yml index 1d4002a..b99d2a1 100644 --- a/action.yml +++ b/action.yml @@ -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. @@ -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 @@ -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 }} diff --git a/entrypoint.sh b/entrypoint.sh index 60dbc5b..86e6faf 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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