DEV Community

Matthias Meyer
Matthias Meyer

Posted on • Updated on

GitHub Actions Hackathon 2021 - Bump docker image version on a remote host

Submission Category: DIY Deployments

We live in a time in which we should pay particular attention to how we deal with our ⚡ energy balance. The first step for this is to know your current personal consumption of electricity and gas.

To address this problem I have a small personal React project in which I log my current consumption values 📈 for electricity and gas.

My gas consumption over the last years

Gas Consumption

My electricity consumption over the last years

Electricity Consumption

At the beginning of the month I take the readings from the electricity and gas meter and add them to a text document in my repository. After that, I want the GitHub actions to take over and do the heavy lifting for me:

  1. Checkout the code and build the project
  2. Build a new docker image with some nice tags
  3. Publish the new image in the awesome GitHub Container Registry
  4. Connect to my remote server and deploy the new image

I was able to successfully implement this with the following workflow:

My Workflow

name: Dockerize React Application
on:
  push:
    branches:
      - 'master'
jobs:
  build-container:
    name: Build Docker image and deploy to production
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout code
        uses: actions/checkout@v2
      -
        name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Publish to GitHub Packages
        uses: docker/build-push-action@v2.3.0
        with:
          push: true
          tags: ghcr.io/${{ github.repository }}:latest, ghcr.io/${{ github.repository }}:${{ github.run_number }}
      -
        name: Deploy on Remote Server
        uses: thematchless/bump-image-version-action@v2
        with:
          remote_docker_host: username@thematchless.de
          ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
          ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
          service_name: electricity-gas-consumption
          deploy_path: /thematchless/electricity-gas-consumption
          args: up -d
          pull_images_first: true
Enter fullscreen mode Exit fullscreen mode

The last step in this workflow is my own created GitHub Action. This GitHub action bumps up a docker image version deployed in a docker-compose stack on a remote server via the SSH protocol. I utilized the GitHub project secrets to add my SSH certificates for the connection.

If you need something similar and simple you can use this action too. Just fork my action or use it as it is 🎉

GitHub logo thematchless / bump-image-version-action

GitHub action to bump up a docker image version deployed in a docker-compose file on a remote server via ssh

Build and publish Docker image

Bump docker image version on a remote host

This repository contains an GitHub action to bump up a docker image version specified in your docker-compose stack.

Requirements for this GitHub Action to work

  • your remote server must be accessible via ssh and is reachable
  • you have a ssh private and public key to authenticate via ssh
  • you have saved your private and public key to the GitHub project secrets

Configuration options for the action

required key example default description
remote_docker_host thematchless@fancyServer.de username@host
ssh_private_key -----BEGIN OPENSSH PRIVATE KEY----
UgAAAAtzc2gtZWQyNTUxOQAAACALBUg
UgAAAAtzc2gtZWQyNTUxOQAAACALBUg
UgAAAAtzc2gtZWQyNTUxOQAAACALBUg
-----END OPENSSH PRIVATE KEY-----
private key in PEM format
ssh_public_key ssh-ed25519 ABCDABCDu027374972309 public key of the PEM
service_name super-fancy-react-app name of the service inside of the compose file
deploy_path /home/thematchless/stack-1 path which contains your compose file on the remote host
args up -d arguments how to start your service
stack_file_name docker-compose.yaml docker-compose.yml name

I've added a table to the README.md with all the flags you can configure in this GitHub action. If anything is unclear, please let me know and I'll try to help.

Have mercy 🙏 on me, this is my first post 😎

Top comments (0)