Skip to content

Build and Publish Docker Image #26

Build and Publish Docker Image

Build and Publish Docker Image #26

Workflow file for this run

name: Build and Publish Docker Image
on:
push:
branches:
- main
tags:
- "v*.*.*" # Match version tags like v1.2.3
pull_request:
branches:
- main
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
name: Build & Push Docker Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
network=host
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: benjlevesque/[email protected]
id: short-sha
with:
length: 7
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Tag with branch name for branch pushes
type=ref,event=branch
# Tag with PR number for pull requests
type=ref,event=pr
# Tag with git tag for version releases
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# Tag with short SHA
type=sha,format=short
# Tag latest only on main branch
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value={{steps.short-sha.outputs.sha}},enable={{is_default_branch}}
labels: |
org.opencontainers.image.title=aigitcommit
org.opencontainers.image.description=AI-powered Git commit message generator
org.opencontainers.image.vendor=Hangzhou Guanwaii Technology Co,.Ltd.
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.short-sha.outputs.sha }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_DATE=${{ github.event.head_commit.timestamp }}
VCS_REF=${{ github.sha }}
VERSION=${{ steps.meta.outputs.version }}
- name: Generate artifact attestation
if: github.event_name != 'pull_request'
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
- name: Generate SBOM
if: github.event_name != 'pull_request'
uses: anchore/[email protected]
with:
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.short-sha.outputs.sha }}
format: spdx-json
output-file: sbom.spdx.json
- name: Upload SBOM as artifact
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@v4
with:
name: ${{ steps.short-sha.outputs.sha }}-sbom
path: sbom.spdx.json
retention-days: 90