Skip to content

Merge branch 'optimization' #39

Merge branch 'optimization'

Merge branch 'optimization' #39

name: Build and Push Docker Images
on:
push:
branches:
- main
tags:
- "v*.*.*"
pull_request:
branches:
- main
workflow_dispatch:
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
jobs:
changes:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
root: ${{ steps.filter.outputs.root }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Check for changes
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'backend/**'
- '.github/workflows/**'
frontend:
- 'frontend/**'
- '.github/workflows/**'
root:
- 'Dockerfile'
- 'config.json'
- 'frontend/**'
- 'backend/**'
- '.github/workflows/**'
build-and-push:
needs: changes
runs-on: ubuntu-latest
strategy:
matrix:
include:
- name: minepanel
dockerfile: ./Dockerfile
context: .
image: minepanel
condition: root
- name: minepanel-backend
dockerfile: ./backend/Dockerfile
context: ./backend
image: minepanel-backend
condition: backend
- name: minepanel-frontend
dockerfile: ./frontend/Dockerfile
context: ./frontend
image: minepanel-frontend
condition: frontend
steps:
- name: Checkout code
uses: actions/checkout@v4
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
- name: Read version from config.json
id: version
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
run: |
VERSION=$(jq -r '.version' config.json)
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Version detected: $VERSION"
- name: Set up QEMU
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
if: |
github.event_name != 'pull_request' && (
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
)
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
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
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
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
platforms: linux/amd64,linux/arm64
- name: Image digest
if: |
(matrix.condition == 'backend' && needs.changes.outputs.backend == 'true') ||
(matrix.condition == 'frontend' && needs.changes.outputs.frontend == 'true') ||
(matrix.condition == 'root' && needs.changes.outputs.root == 'true') ||
github.event_name == 'workflow_dispatch'
run: |
echo "Image ${{ matrix.image }} built successfully"
echo "Version: ${{ steps.version.outputs.version }}"
echo " Platforms: linux/amd64, linux/arm64"
echo "Tags: ${{ steps.meta.outputs.tags }}"