How to Push Docker image to multiple registries
Using GitHub Actions ๐Ÿš€โšก

How to Push Docker image to multiple registries Using GitHub Actions ๐Ÿš€โšก

Aug 2, 2022ยท

3 min read

Play this article

In this article I will show you how to push your docker images to Docker Hub and GitHub Package Manager using GitHub Actions.

Prerequisites

  • You must have a Dockerize application :)
  • You must have a docker hub account and docker hub repository
  • Your GitHub repository should contain a Dockerfile

Step 1 - Create Github Action

  • Click on Actions Tab and create node js workflow template

image.png

image.png

  • Now click on Configure button to set up this workflow

Step 2 โ€” Change workflow file like below

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    environment:
      name: Production
      url: http://bawanthathilan.github.io/
    strategy:
      matrix:
        node-version: [14.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - run: npm ci
      - run: npm run build
      - run: npm run export
      - run: touch ./out/.nojekyll

      - name: Deploy ๐Ÿš€
        uses: JamesIves/github-pages-deploy-action@v4.2.5
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: out # The folder the action should deploy.

  push_to_registries:
    name: Push Docker image to multiple registries
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Log in to the Container registry
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CR_PAT }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: |
            bawantha/portfolio
            ghcr.io/${{ github.repository }}

      - name: Build and push Docker images
        uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Step 3 โ€” Add your DockerHub Secrets to GitHub

  • Go to Settings

image.png

then select secrets

image.png

  • Click on new repository secrets to add your secrets

image.png

  • Now you need to add your docker username and password as github secrets which are going to use in GitHub action.

image.png

  • Now we need GitHub token for authenticate GitHub Package Registry

Lets Create GitHub Personal access token โšก

Go to profile Setting -> Developer Settings -> Personal access tokens ->Generate New Token

Then select write:packages tick โœ… and Generate your token

image.png

After that we need defined our variables in our workflow like this

- name: Log in to Docker Hub
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          username: ${{ secrets.**DOCKERHUB_USERNAME** }}
          password: ${{ secrets.**DOCKERHUB_TOKEN** }}
- name: Log in to the Container registry
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CR_PAT }}
  • Now come back to previous browser tab and commit your GitHub action.

image.png

  • Also you can see your docker image in docker hub repository and Github Package Registry

image.png image.png

Thank you ! Hope you enjoy the article. You can find this GitHub repository and DockerHub repository in below

ย