Skip to content

Instantly share code, notes, and snippets.

@seun-beta
Last active September 28, 2022 20:43
Show Gist options
  • Save seun-beta/2970fe8784ac4e5a010d6394ac1f5121 to your computer and use it in GitHub Desktop.
Save seun-beta/2970fe8784ac4e5a010d6394ac1f5121 to your computer and use it in GitHub Desktop.

Using GitHub Actions to build your Docker Image

To sheds more light on using GitHub Actions, this documentation breaks down the commands into simple bits: https://docs.github.com/en/actions

N/B: your file must end with .yml OR .yaml

File structure for using GitHub Actions in your project folder.

├── .github
│   ├── workflows
│       ├── ci.yaml
├── project base folder
│   ├── other folders 
├── .env
├── package.json
├── package-lock.json
├── LICENSE
└── README.md

The code block starts below

name: CI

on:
  push:
    branches: [main]

  workflow_dispatch:

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/build-push-action@v3
        with:
          images: DOCKER_HUB_USERNAME/DOCKER_HUB_REPO_NAME

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

An example to shed more light on this

The code block starts below

name: CI

on:
  push:
    branches: [main]

  workflow_dispatch:

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest

    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/build-push-action@v3
        with:
          images: seunfunmi/node-application

      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: ./node-application
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment