Skip to content

feat: add source code and GitHub Actions CI #1

feat: add source code and GitHub Actions CI

feat: add source code and GitHub Actions CI #1

# This YAML file is refered to
# https://youtu.be/RgZyX-e6W9E?si=iISxsuXEZKma4SVv
# https://youtu.be/Tx1UElRhELg?si=MrbyhgWCwlGVtgyD
name: Docker Image CI for GitHub Container Registry (GHCR)
on:
push:
tags:
- '*' # Triggered on new tag
env:
# Use docker.io for Docker Hub if empty
# Here we use GitHub Container Registry (GHCR) to store our images
REGISTRY: ghcr.io
jobs:
build_and_publish:
runs-on: ubuntu-latest
name: Build and Push Docker Image
steps:
# This is going to pull our code from the repository
# into the actions runner in order to build the image
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
# Add support for more platforms with QEMU (optional)
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
# Choose one method
# 1. Use login-action
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# 2. Use command line
# run: |
# docker login ${{ env.REGISTRY }} --username ${{ github.actor }} --password ${{ secrets.DOCKER_PAT }}
# Set image name to lowercase repository name
- name: Set image name
run: |
IMAGE_NAME=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
# Choose one method
# 1. Use build-push-action
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} , ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
# TODO: Improve pipeline with https://docs.docker.com/build/ci/github-actions/multi-platform/
platforms: linux/arm64,linux/amd64 # ,linux/arm/v7
# 2. Use command lines
# run: docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG }} -f ./Dockerfile --push .