DEV Community

Paulund
Paulund

Posted on • Originally published at paulund.co.uk

Github Action To Push Docker Image

Github action can be used to run tests on your applications when code has been merged in.

The next step once tests are successful is to build a docker image and push this to the repository.

This will require us to create a new workflow file that will run after the tests are successful to build and push your docker image to a repository.

First create a new file in your workflow folder .github/workflows/docker.yml and paste the following in that file.

name: Docker

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: 'Checkout GitHub Action'
        uses: actions/checkout@v2
    - name: Build and push docker
        uses: docker/build-push-action@v1
        with:
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
          repository: DOCKER_REPOSITORY
          tags: latest
Enter fullscreen mode Exit fullscreen mode

This works by first checking out the current repository on pushed into master, by using the actions/checkout@v2 action. The next step will use the docker build and push action docker/build-push-action@v1. This requires 4 parameters the docker hub username, password, repository and tags.

In the code above you can see we're using the secrets parameter for username and password.

username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
Enter fullscreen mode Exit fullscreen mode

To add secrets to your repository in Github is done by selecting Settings -> Secrets then create a new secret for the username and password.

Ensure your repository has a Dockerfile at the root of your project to build the image.

That's it, now commit this file to your repository and on pushes into master, Github will build a push your docker image to the docker hub.

Top comments (0)