Files
2025-10-31 11:35:48 +11:00

114 lines
3.9 KiB
YAML

name: Build and Push Docker Image
on:
push:
branches:
- main
- develop
- master
tags:
- 'v*'
pull_request:
branches:
- main
- master
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write # Changed from 'read' to 'write' to allow commits
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate image version
id: version
run: |
# Generate version from run number
IMAGE_VERSION="${{ github.ref_name }}-${{ github.run_number }}"
echo "version=${IMAGE_VERSION}" >> $GITHUB_OUTPUT
echo "Generated version: ${IMAGE_VERSION}"
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=${{ steps.version.outputs.version }},enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Update Kubernetes manifest
if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
run: |
set -e
IMAGE_VERSION="${{ steps.version.outputs.version }}"
# Replace {{IMAGE_VERSION}} in template with actual version
cat manifest-template.yaml | sed "s/{{IMAGE_VERSION}}/${IMAGE_VERSION}/g" > k8s/manifest.yaml
echo "✅ Updated manifest with version: ${IMAGE_VERSION}"
- name: Commit manifest changes
if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
uses: EndBug/add-and-commit@v9
with:
author_name: youmusic-bot
author_email: bot@youmusic.local
message: 'chore: Update k8s manifest to version ${{ steps.version.outputs.version }}'
add: 'k8s/manifest.yaml'
push: true
- name: Summary
if: github.event_name != 'pull_request'
run: |
echo "### Docker Image Built and Pushed :rocket:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Registry:** ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY
echo "**Image:** ${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Tags:**" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
if [[ "${{ github.ref }}" == "refs/heads/main" || "${{ github.ref }}" == "refs/heads/master" ]]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Kubernetes Manifest Updated:** :white_check_mark:" >> $GITHUB_STEP_SUMMARY
echo "Version: \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
fi