Build, Test and Deploy Docker Image from Github Packages to Digital Ocean Droplet

My aim is to have continuous deployment of a nodejs-express application to my digital ocean droplet whenever a file is checked into the master branch.

I have a dockerized nodejs application, I am building an image and pushing the image into github packages registry using github actions.

Now after the build I want deploy this image into digital ocean droplet using github actions and run the image.How can I do that?

Any examples would be highly helpful as I am new to CI/CD.

The following is my workflow file now.

name: Docker

on:
  push:
    # Publish `master` as Docker `latest` image.
    branches:
      - master

    # Publish `v1.2.3` tags as releases.
    tags:
      - v*

  # Run tests for any PRs.
  pull_request:

env:
  # TODO: Change variable to your image's name.
  IMAGE_NAME: image

jobs:
  # Run tests.
  # See also https://docs.docker.com/docker-hub/builds/automated-testing/
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Run tests
        run: |
          if [ -f docker-compose.test.yml ]; then
            docker-compose --file docker-compose.test.yml build
            docker-compose --file docker-compose.test.yml run sut
          else
            docker build . --file Dockerfile
          fi
  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.
    needs: test

    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin

      - name: Push image
        run: |
          IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          # Use Docker `latest` tag convention
          [ "$VERSION" == "master" ] && VERSION=latest
          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION
          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

Hi @ReghuShanker,

Glad to see you in Github Community Forum!

From your code, docker image is built and tested on hosted runner, and pushed to Github Package registry.

You can push the image from hosted runner to digital oceam droplet in the same way.

      - name: Install doctl                 # install the doctl on the runner
        uses: digitalocean/action-doctl@v2
        with:
          token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
      - name: push image to digitalocean
        run: |
          doctl registry login         # or you can use "docker login -u ZDRhYzzzz -p ZDRhYzzzz registry.digitalocean.com"
          docker tag <my-image> registry.digitalocean.com/<my-registry>/<my-image>
          docker push registry.digitalocean.com/<my-registry>/<my-image>
        

I don’t have digital ocean account hence i cannot try it on my side. But based on the doc below, it should work.


Hope it helps.

Thanks

Which would be a good approach

  1. Push the image to digital ocean registry and then ssh to droplet through github actions and run the docker image through github action ssh.

  2. SSH to the droplet through github actions and SCP the image to droplet.Then run docker through ssh command.

I am planning to use https://github.com/appleboy/ssh-action github action package.

Please let me know your thoughts?

Thanks!

Hi @ReghuShanker,

Thanks for your reply!

I prefer the 1st option since it could use image from registry in droplet.
However, I don’t have digital ocean account so i cannot try it out on my side. Could you please try on your side?

1 Like

@weide-zhou Thanks for the help. I was successfully able to build the image and push it to digital ocean registry!

Now is it possible to pull image into digitalocean droplet from registry and run the image through github-actions itself? Do I have to ssh through github actions to do that?

Hi @ReghuShanker,

Thanks for your reply!

I don’t find related actions to pull image to digitalocean droplet from registry.
You can try to ssh to the droplet and pull image from digitalocean registry. It’s recommened to try on local machine firstly, if it’s successful to pull image to droplet, you can move to the github actions.

Thanks