|  | 
| 1 |  | -# github-action-ssh-docker-compose | 
|  | 1 | +## github-action-ssh-docker-compose | 
| 2 | 2 | Simple github action to run docker-compose on remote host. | 
|  | 3 | + | 
|  | 4 | +This action packs contents of the action workspace into archive. | 
|  | 5 | +Logs into remote host via ssh. Unpacks the workspace there and runs | 
|  | 6 | +`docker-compose up -d` command. | 
|  | 7 | + | 
|  | 8 | +Comparing to other actions with similar behavior this one does not use any | 
|  | 9 | +unknown docker-images. It is entirely built from Dockerfile on top of | 
|  | 10 | +`alpine:3.8`. | 
|  | 11 | + | 
|  | 12 | +## Inputs | 
|  | 13 | + | 
|  | 14 | + * `ssh_private_key` - Private SSH key used for logging into remote system. | 
|  | 15 | +   Please, keep your key securely in github secrets. | 
|  | 16 | + * `ssh_host` - Remote host name. | 
|  | 17 | + * `ssh_port` - Remote port for SSH connection. Default is 22. | 
|  | 18 | + * `ssh_user` - Remote user which should have access to docker. | 
|  | 19 | + * `docker_compose_prefix` - Project name passed to compose. Each docker | 
|  | 20 | +   container will have this prefix in name. | 
|  | 21 | + * `docker_compose_file` - Path to the docker-compose file in the repository. | 
|  | 22 | + | 
|  | 23 | +# Usage example | 
|  | 24 | + | 
|  | 25 | +Let's say we have a repo with single docker-compose file in it and remote | 
|  | 26 | +ubuntu based server with docker and docker-compose installed. | 
|  | 27 | + | 
|  | 28 | +1. Generate key pair, do not use a password here. | 
|  | 29 | + | 
|  | 30 | +``` | 
|  | 31 | +ssh-keygen -t ed25519 deploy_key | 
|  | 32 | +``` | 
|  | 33 | + | 
|  | 34 | +2. Create a user which will deploy containers for you on the remote server, do | 
|  | 35 | +not set password for this user: | 
|  | 36 | + | 
|  | 37 | +``` | 
|  | 38 | +ssh example.com | 
|  | 39 | +$ sudo useradd -m -b /var/lib -G docker docker-deploy | 
|  | 40 | +``` | 
|  | 41 | + | 
|  | 42 | +3. Allow to log into that user with the key you generated on the step one. | 
|  | 43 | + | 
|  | 44 | +``` | 
|  | 45 | +scp deploy_key.pub example.com:~ | 
|  | 46 | +ssh example.com | 
|  | 47 | +$ sudo mkdir /var/lib/docker-deploy/.ssh | 
|  | 48 | +$ sudo chown docker-deploy:docker-deploy /var/lib/docker-deploy/.ssh | 
|  | 49 | +$ sudo install -o docker-deploy -g docker-deploy -m 0600 deploy_key.pub /var/lib/docker-deploy/.ssh/authorized_keys | 
|  | 50 | +$ sudo chmod 0500 /var/lib/deploy/.ssh | 
|  | 51 | +$ rm deploy_key.pub | 
|  | 52 | +``` | 
|  | 53 | + | 
|  | 54 | +4. Test that key works. | 
|  | 55 | + | 
|  | 56 | +``` | 
|  | 57 | +ssh -i deploy_key docker-deploy@example.com | 
|  | 58 | +``` | 
|  | 59 | + | 
|  | 60 | +5. Add private key and user name into secrets for the repository. Let's say that | 
|  | 61 | +names of the secrets are `EXAMPLE_COM_SSH_PRIVATE_KEY` and | 
|  | 62 | +`EXAMPLE_COM_SSH_USER`. | 
|  | 63 | + | 
|  | 64 | +6. Remove your local copy of the ssh key: | 
|  | 65 | + | 
|  | 66 | +``` | 
|  | 67 | +rm deploy_key | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +7. Setup a github-actions workflow (e.g. `.github/workflows/main.yml`): | 
|  | 71 | + | 
|  | 72 | +``` | 
|  | 73 | +name: Deploy | 
|  | 74 | +
 | 
|  | 75 | +on: | 
|  | 76 | +  push: | 
|  | 77 | +    branches: [ master ] | 
|  | 78 | +
 | 
|  | 79 | +jobs: | 
|  | 80 | +  deploy: | 
|  | 81 | +    runs-on: ubuntu-latest | 
|  | 82 | +
 | 
|  | 83 | +    steps: | 
|  | 84 | +    - uses: actions/checkout@v2 | 
|  | 85 | +
 | 
|  | 86 | +    - uses: alex-ac/github-action-ssh-docker-compose@master | 
|  | 87 | +      name: Docker-Compose Remote Deployment | 
|  | 88 | +      with: | 
|  | 89 | +        ssh_host: example.com | 
|  | 90 | +        ssh_private_key: ${{ secrets.EXAMPLE_COM_SSH_PRIVATE_KEY }} | 
|  | 91 | +        ssh_user: ${{ secrets.EXAMPLE_COM_SSH_USER }} | 
|  | 92 | +        docker_compose_prefix: example_com | 
|  | 93 | +``` | 
|  | 94 | + | 
|  | 95 | +8. You're all set! | 
0 commit comments