Halo, kalau kamu merasa tulisan saya ngebantu kamu, kamu bisa ucapkan terima kasih lewat saweria .

Hello, if you find this article helpful, you can express your gratitude through saweria .



Awalan

  1. Contoh ini di ambil dari jawaban user crazy-max di github issues - build-push-action

  2. Image di push ke ghcr.io

  3. Contoh project saya ambil dari post saya sebelumnya, Docker Custom Environment Dengan Sed & Envsubst

  4. Hanya menggunakan 1 repo, dibedakan oleh tag saja (satu dengan :sed, satu lagi dengan :envsubst)


How To

Tambahkan github workflows

name: Build & publish multiple images
on:
  push:
    branches:
      - main

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    #use matrix to create multiple job easily
    #https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
    strategy:
      fail-fast: false
      matrix:
        include:
          - dockerfile: ./docker-environment-example/Dockerfile
            #to create your own, the format was
            #ghcr.io/username/reponame:prefered-tag
            image: ghcr.io/ipang777/docker-env:sed
            context: ./docker-environment-example
          - dockerfile: ./docker-environment-example-envsubst/Dockerfile
            image: ghcr.io/ipang777/docker-env:envsubst
            context: ./docker-environment-example-envsubst

    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: log in container registry
        uses: docker/login-action@v1
        with:
            registry: ${{ env.REGISTRY }}
            username: ${{ github.actor }}
            password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: ${{ matrix.image }}

      - name: Build and push Docker image 
        uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
        with:
          # call variable on matrix strategy
          context: ${{ matrix.context }}
          file: ${{ matrix.dockerfile }}
          push: true
          tags: ${{ matrix.image }}
          labels: ${{ steps.meta.outputs.labels }}

References

  1. github build-push-action - issues #561