70 %

Toast is in Beta!Learn more →

Chris Biscardi

Deploying Docker Images to Digital Ocean without a registry via GitHub Actions

We can use an ssh key we created and copied to our Digital Ocean droplit to test ssh access from GitHub Actions using the appleboy/ssh-action workflow.

Make sure to specify the keys in the GH secrets panel so the workflow has access to them.

yaml
name: remote ssh command
on: [push]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: executing remote ssh commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DIGITAL_OCEAN_IP }}
username: ${{ secrets.DIGITAL_OCEAN_USER }}
key: ${{ secrets.DIGITAL_OCEAN_KEY }}
port: 22
script: |
echo "what"
whoami

Successful output will look like this:

======CMD======
echo "what"
whoami
======END======
out: what
out: ***
==============================================
✅ Successfully executed commands to all host.
==============================================

Now that we know everything is working, this is a GitHub Action that

  1. builds a docker image
  2. saves the image to a .tar file
  3. scps the .tar file to the Digital Ocean Droplit
  4. uses docker load to load the tar file as an image
  5. kills the currently running containers (all of them)
  6. runs a new container
  7. prunes unused resources to reclaim space
yaml
name: build and deploy corgo-bot
on: [push]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: build docker image
run: |
docker build -t biscarch/corgo-bot .
docker save -o corgo-bot.tar biscarch/corgo-bot
- name: scp docker image to DO
uses: appleboy/scp-action@master
with:
host: ${{ secrets.DIGITAL_OCEAN_IP }}
username: ${{ secrets.DIGITAL_OCEAN_USER }}
key: ${{ secrets.DIGITAL_OCEAN_KEY }}
port: 22
source: corgo-bot.tar
target: /opt
- name: executing remote ssh commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DIGITAL_OCEAN_IP }}
username: ${{ secrets.DIGITAL_OCEAN_USER }}
key: ${{ secrets.DIGITAL_OCEAN_KEY }}
port: 22
script: |
docker load -i /opt/corgo-bot.tar
rm /opt/corgo-bot.tar
docker kill $(docker ps -q)
docker run --env-file /opt/corgo-bot.env -d biscarch/corgo-bot:latest
docker system prune -f