GitHub Actions Hackathon 2021 - Bump docker image version on a remote host
Tags: actionshackathon21, github, opensource, continuousdeployment
Published: 2021-12-02 22:33:29

---
title: GitHub Actions Hackathon 2021 - Bump docker image version on a remote host
published: true
description: My submission to the GitHub Actions x DEV Hackathon 2021!
tags: actionshackathon21, github, opensource, continuousDeployment
---
### Submission Category: DIY Deployments

We live in a time in which we should pay particular attention to how we deal with our :zap: 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 :chart_with_upwards_trend: for electricity and gas.

### My gas consumption over the last years
![Gas Consumption](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsfp0jncatc8pt1t9x63.png)
### My electricity consumption over the last years
![Electricity Consumption](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/saijse9hbiboelamd2qn.png)

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
```yaml
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
```

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 :tada:

{% github https://github.com/thematchless/bump-image-version-action %}

I've added a table to the [README.md](https://github.com/thematchless/bump-image-version-action/blob/develop/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 :pray: on me, this is my first post :sunglasses: