Skip to content

Merge branch 'main' of https://github.com/d3mocide/MeshRF #93

Merge branch 'main' of https://github.com/d3mocide/MeshRF

Merge branch 'main' of https://github.com/d3mocide/MeshRF #93

name: Docker
on:
push:
branches: ["dev", "main"]
tags: ["v*.*.*"]
pull_request:
branches: ["dev", "main"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
has-changes: ${{ steps.set-matrix.outputs.has-changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive
- name: Detect changed paths
id: set-matrix
run: |
# Initialize empty matrix
MATRIX_INCLUDE="[]"
HAS_CHANGES=false
# Function to add to matrix
add_to_matrix() {
local context=$1
local suffix=$2
local dockerfile=$3
local component=$4
# Use jq to robustly add the object to the array
MATRIX_INCLUDE=$(echo "$MATRIX_INCLUDE" | jq -c ". + [{\"context\":\"$context\",\"image-suffix\":\"$suffix\",\"dockerfile\":\"$dockerfile\",\"component\":\"$component\"}]")
HAS_CHANGES=true
}
# Check if we should build everything (tags, PRs, or workflow change)
FORCE_BUILD=false
if [[ "${{ github.ref }}" == refs/tags/* ]] || [[ "${{ github.event_name }}" == "pull_request" ]]; then
FORCE_BUILD=true
elif git diff --name-only HEAD~1 | grep -q "\.github/workflows/docker-publish\.yml"; then
FORCE_BUILD=true
fi
if [ "$FORCE_BUILD" = true ]; then
add_to_matrix "." "" "Dockerfile" "core"
add_to_matrix "./rf-engine" "-rf-engine" "./rf-engine/Dockerfile" "rf-engine"
add_to_matrix "opentopodata" "-opentopodata" "opentopodata/docker/Dockerfile" "opentopodata"
else
# Core/root image changes
if git diff --name-only HEAD~1 | grep -E '(Dockerfile|\.go$|\.py$|requirements|package\.json|\.ts$|\.js$)' | grep -qv "rf-engine\|opentopodata"; then
add_to_matrix "." "" "Dockerfile" "core"
fi
# rf-engine changes
if git diff --name-only HEAD~1 | grep -q "^rf-engine/"; then
add_to_matrix "./rf-engine" "-rf-engine" "./rf-engine/Dockerfile" "rf-engine"
fi
# opentopodata changes
if git diff --name-only HEAD~1 | grep -q "^opentopodata/"; then
add_to_matrix "opentopodata" "-opentopodata" "opentopodata/docker/Dockerfile" "opentopodata"
fi
fi
# finalize matrix JSON
MATRIX=$(echo "{}" | jq -c ".include = $MATRIX_INCLUDE")
echo "Detected changes: $HAS_CHANGES"
echo "Matrix: $MATRIX"
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
echo "has-changes=$HAS_CHANGES" >> $GITHUB_OUTPUT
build:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.has-changes == 'true' }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
- name: Verify Submodules
run: |
git submodule update --init --recursive
ls -R opentopodata
- name: Lowercase Image Name
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image-suffix }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image-suffix }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}${{ matrix.image-suffix }}:buildcache,mode=max