Skip to content

Build and Push Docker Image #11

Build and Push Docker Image

Build and Push Docker Image #11

Workflow file for this run

name: Build and Push Docker Image
on:
push:
branches:
- "**"
schedule:
- cron: "0 0 * * 0" # 00:00 every Sunday
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} # 'owner/repo'
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: "10.0.x"
- name: Run Unit Tests
run: dotnet test --configuration Release
- name: Login container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Logic:
# 1. If branch is main, set tag to 'latest'
# 2. If branch is NOT main, use the branch name (sanitized)
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
type=ref,event=branch,enable=${{ github.ref != 'refs/heads/main' }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}