Files
EnergyCalculatorWeb/.github/workflows/docker-publish.yml
2025-07-06 23:42:11 +00:00

69 lines
2.7 KiB
YAML

# .github/workflows/docker-publish.yml
name: Docker Image CI for Electricity Calculator
# Controls when the workflow will run
on:
push:
branches: [ "master" ] # Triggers on pushes to the main branch
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build_and_push_to_ghcr:
name: Build and Push to GitHub Container Registry
runs-on: ubuntu-latest # Use the latest Ubuntu runner
permissions:
contents: read # Needed to checkout the repository
packages: write # Needed to push to GitHub Packages (GHCR)
# id-token: write # Uncomment if using OIDC for authentication with a cloud provider
steps:
- name: Checkout repository
uses: actions/checkout@v4
# This step checks out your repository under $GITHUB_WORKSPACE, so your workflow can access it.
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }} # Your GitHub username or organization name
password: ${{ secrets.GITHUB_TOKEN }}
# This step logs in to GHCR using a GITHUB_TOKEN, which is automatically generated for each job run.
# The GITHUB_TOKEN has permissions to push to GHCR if 'packages: write' is set for the job.
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/electricity-calculator-app
# Generates tags:
# - 'latest' for the default branch
# - Git tag if a tag is pushed
# - Git SHA as a tag
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
latest
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: . # Build context is the root of the repository (where Dockerfile is)
file: ./Dockerfile # Explicitly specify the Dockerfile name if it's not 'Dockerfile' or is in a subdirectory
push: true # Actually push the image after building
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# This step builds the Docker image using the Dockerfile in your repository
# and pushes it to GHCR with the tags generated in the previous step.
- name: Image digest
run: echo ${{ steps.build_and_push.outputs.digest }}
# Optional: Outputs the digest of the pushed image.