This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Image | |
on: | |
push: | |
branches: | |
- '**' | |
tags: ["v*.*.*"] | |
pull_request: | |
branches: | |
- '**' | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
CACHE_FROM: type=registry,ref=${{ github.repository }}:buildcache | |
CACHE_TO: type=registry,ref=${{ github.repository }}:buildcache,mode=max | |
jobs: | |
free-disk-space: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Free Disk Space (Ubuntu) | |
uses: jlumbroso/free-disk-space@main | |
with: | |
tool-cache: false | |
android: true | |
dotnet: true | |
haskell: true | |
large-packages: true | |
docker-images: true | |
swap-storage: true | |
build-and-push: | |
runs-on: ubuntu-latest | |
needs: free-disk-space # Ensure this job runs after freeing disk space | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
submodules: recursive | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
version: latest | |
driver-opts: | | |
image=moby/buildkit:latest | |
- name: Log into registry ${{ env.REGISTRY }} | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# Cache Python dependencies | |
- name: Cache pip packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/pip | |
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/pyproject.toml') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
# Set image name based on branch | |
- name: Set image name and tags | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
# For main branch | |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} | |
type=sha,format=short,prefix=,enable=${{ github.ref == 'refs/heads/main' }} | |
# For other branches | |
type=raw,value=dev-latest,enable=${{ github.ref != 'refs/heads/main' }} | |
type=sha,format=short,prefix=dev-,enable=${{ github.ref != 'refs/heads/main' }} | |
# For tags | |
type=ref,event=tag | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=semver,pattern={{major}} | |
- name: Build and push Docker image | |
id: build-and-push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: | | |
type=gha | |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache | |
cache-to: | | |
type=gha,mode=max | |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max | |
platforms: linux/amd64 | |
build-args: | | |
BUILDKIT_INLINE_CACHE=1 |