Giter VIP home page Giter VIP logo

docker-stack-deploy's Introduction

GitHub Pages Deploy Action Logo

Docker Stack Deploy Tool

GitHub Actions Marketplace Release version badge

GitHub Repo stars license badge

GitHub Action and Docker image used to deploy a Docker stack on a Docker Swarm.

Configuration options

GitHub Action Input Environment Variable Summary Required Default Value
registry REGISTRY Specify which container registry to login to.
username USERNAME Container registry username.
password PASSWORD Container registry password.
remote_host REMOTE_HOST Hostname or address of the machine running the Docker Swarm manager node
remote_port REMOTE_PORT SSH port to connect on the the machine running the Docker Swarm manager node. 22
remote_user REMOTE_USER User with SSH and Docker privileges on the machine running the Docker Swarm manager node.
remote_private_key REMOTE_PRIVATE_KEY Private key used for ssh authentication.
deploy_timeout DEPLOY_TIMEOUT Seconds, to wait until the deploy finishes 600
stack_file STACK_FILE Path to the stack file used in the deploy.
stack_name STACK_NAME Name of the stack to be deployed.
stack_param STACK_PARAM Additional parameter (env var) to be passed to the stack.
env_file ENV_FILE Additional environment variables to be passed to the stack.
debug DEBUG Verbose logging 0

Using the GitHub Action

Add, or edit an existing, yaml file inside .github/actions and use the configuration options listed above.

Examples

Deploying public images

name: Deploy Staging

on:
  push:
    branches:
      - main

jobs:

  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout codebase
        uses: actions/checkout@v2

      - name: Deploy
        uses: kitconcept/[email protected]
        with:
          remote_host: ${{ secrets.REMOTE_HOST }}
          remote_user: ${{ secrets.REMOTE_USER }}
          remote_private_key: ${{ secrets.REMOTE_PRIVATE_KEY }}
          stack_file: "stacks/plone.yml"
          stack_name: "plone-staging"

Deploying private images from GitHub Container Registry

First, follow the steps to create a Personal Access Token.

name: Deploy Live

on:
  push:
    tags:
      - '*.*.*'

jobs:

  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout codebase
        uses: actions/checkout@v2

      - name: Deploy
        uses: kitconcept/[email protected]
        with:
          registry: "ghcr.io"
          username: ${{ secrets.GHCR_USERNAME }}
          password: ${{ secrets.GHCR_TOKEN }}
          remote_host: ${{ secrets.REMOTE_HOST }}
          remote_user: ${{ secrets.REMOTE_USER }}
          remote_private_key: ${{ secrets.REMOTE_PRIVATE_KEY }}
          stack_file: "stacks/plone.yml"
          stack_name: "plone-live"
          stack_param: "foo"

Using the Docker Image

It is possible to directly use the ghcr.io/kitconcept/docker-stack-deploy Docker image, passing the configuration options as environment variables.

Examples

Local machine

Considering you have a local file named .env_deploy with content:

REGISTRY=hub.docker.com
USERNAME=foo_usr
PASSWORD=averylargepasswordortoken
REMOTE_HOST=192.168.17.2
REMOTE_PORT=22
REMOTE_USER=user
STACK_FILE=path/to/stack.yml
STACK_NAME=mystack
DEBUG=1

Run the following command:

docker run --rm
  -v "$(pwd)":/github/workspace
  -v /var/run/docker.sock:/var/run/docker.sock
  --env-file=.env_deploy
  -e REMOTE_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"
  ghcr.io/kitconcept/docker-stack-deploy:latest

GitLab CI

On your GitLab project, go to Settings -> CI/CD and add the environment variables under Variables.

Then edit your .gitlab-cy.yml to include the deploy step:

image: busybox:latest

services:
  - docker:20.10.16-dind

before_script:
  - docker info

deploy:
  stage: deploy
  varibles:
    REGISTRY: ${REGISTRY}
    USERNAME: ${REGISTRY_USER}
    PASSWORD: ${REGISTRY_PASSWORD}
    REMOTE_HOST: ${DEPLOY_HOST}
    REMOTE_PORT: 22
    REMOTE_USER: ${DEPLOY_USER}
    REMOTE_PRIVATE_KEY: "${DEPLOY_KEY}"
    STACK_FILE: stacks/app.yml
    STACK_NAME: app
    DEPLOY_IMAGE: ghcr.io/kitconcept/docker-stack-deploy:latest
  script:
    - docker pull ${DEPLOY_IMAGE}
    - docker run --rm
       -v "$(pwd)":/github/workspace
       -v /var/run/docker.sock:/var/run/docker.sock
       -e REGISTRY=${REGISTRY}
       -e USERNAME=${USERNAME}
       -e PASSWORD=${PASSWORD}
       -e REMOTE_HOST=${REMOTE_HOST}
       -e REMOTE_PORT=${REMOTE_PORT}
       -e REMOTE_USER=${REMOTE_USER}
       -e REMOTE_PRIVATE_KEY="${REMOTE_PRIVATE_KEY}"
       -e STACK_FILE=${STACK_FILE}
       -e STACK_NAME=${STACK_NAME}
       -e DEBUG=1
       ${DEPLOY_IMAGE}

Contribute

Please DO NOT commit to version branches directly. Even for the smallest and most trivial fix.

ALWAYS open a pull request and ask somebody else to merge your code. NEVER merge it yourself.

Credits

kitconcept GmbH

This repository also uses the docker-stack-wait script, available at GitHub.

The logo is based on rocket icon.

License

The project is licensed under MIT License

docker-stack-deploy's People

Contributors

ericof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

docker-stack-deploy's Issues

If remote_host has a newline, deployment fails with ssh: Could not resolve hostname

This is an easy error to make and hard to catch. Only if you enable debug on the run, you see that the remote_host variable only has a starting ' quote, but not an ending quote like the other variables.

In the github environment UI it is very easy and invisible if the variable has a newline. Can we add somethiing like:

https://stackoverflow.com/questions/19345872/how-to-remove-a-newline-from-a-string-in-bash

when the remote_host variable is used?

Alternatively we could also catch it in the GHA workflow file when the remote_host variable is assigned in the with: arguments, but that's repetitive boiler plate every time you use the action.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.