From 6d279e413ab685d17c0c57b0c649973df426c2af Mon Sep 17 00:00:00 2001 From: Aleksandr Derbenev Date: Sun, 3 Oct 2021 11:42:51 +0100 Subject: [PATCH] Support docker stack. --- README.md | 31 +++++++++++++++++++++++++++++++ action.yml | 4 ++++ entrypoint.sh | 7 ++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41d3103..8b33648 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ unknown docker-images. It is entirely built from Dockerfile on top of * `docker_compose_prefix` - Project name passed to compose. Each docker container will have this prefix in name. * `docker_compose_file` - Path to the docker-compose file in the repository. + * `use_stack` - Use docker stack instead of docker-compose. # Usage example @@ -93,3 +94,33 @@ jobs: ``` 8. You're all set! + +# Swarm & Stack + +In case you want to use some advanced features like secrets. You'll need to +setup a docker swarm cluster and use docker stack command instead of the plain +docker-compose. To do that just set `use_stack` 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 + use_stack: 'true' +``` + diff --git a/action.yml b/action.yml index e8a5154..1d4002a 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,9 @@ inputs: docker_compose_filename: description: Docker compose file to use default: docker-compose.yaml + use_stack: + description: Use docker stack instead of docker compose ("true" or "false"). + default: 'false' runs: using: docker image: Dockerfile @@ -30,3 +33,4 @@ runs: SSH_PRIVATE_KEY: ${{ inputs.ssh_private_key }} DOCKER_COMPOSE_FILENAME: ${{ inputs.docker_compose_filename }} DOCKER_COMPOSE_PREFIX: ${{ inputs.docker_compose_prefix }} + USE_DOCKER_STACK: ${{ inputs.use_stack }} diff --git a/entrypoint.sh b/entrypoint.sh index 87312b7..9bb1b64 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -20,9 +20,14 @@ tar cjvf /tmp/workspace.tar.bz2 --exclude .git . log "Launching ssh agent." eval `ssh-agent -s` +docker_subcommand="docker-compose --build" +if $USE_DOCKER_STACK ; then + docker_subcommand="docker stack" +fi + ssh-add <(echo "$SSH_PRIVATE_KEY") -remote_command="set -e ; log() { echo '>> [remote]' \$@ ; } ; cleanup() { log 'Removing workspace...'; rm -rf \"\$HOME/workspace\" ; } ; log 'Creating workspace directory...' ; mkdir \"\$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 \"\$HOME/workspace\" ; trap cleanup EXIT ; log 'Unpacking workspace...' ; tar -C \"\$HOME/workspace\" -xjv ; log 'Launching $docker_subcommand...' ; cd \"\$HOME/workspace\" ; $docker_subcommand -f \"$DOCKER_COMPOSE_FILENAME\" -p \"$DOCKER_COMPOSE_PREFIX\" up -d --remove-orphans" echo ">> [local] Connecting to remote host." ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \