Files
Data-Coupler/.gitea/workflows/docker-build.yml
T
Alessio Dal Santo c15e6c9065 [Fix] Passa versione MinVer come build-arg al Docker per evitare errore MINVER1001
Il problema era che MinVer veniva eseguito dentro il container Docker dove la
directory .git non esiste, causando il warning MINVER1001 e l'uso di 0.0.0-alpha.0
(poi sostituito dal fallback hardcoded 2.1.0).

Soluzione:
- La versione viene calcolata sul runner CI/CD (dove git e' disponibile)
- Esportata come variabile d'ambiente APP_VERSION via GITHUB_ENV
- Passata al Docker build tramite --build-arg APP_VERSION
- Nei Dockerfile aggiunto ARG APP_VERSION e /p:MinVerVersionOverride per imporla
  a MinVer senza che tenti di accedere a git (assente nel container)
- ARG ridichiarato dopo ogni FROM in multi-stage build (comportamento Docker)
2026-03-20 18:25:54 +01:00

345 lines
12 KiB
YAML

name: Build and Push Docker Images
on:
push:
branches:
- main
- staging
workflow_dispatch:
inputs:
force_build:
description: 'Force build even without code changes'
required: false
default: false
type: boolean
env:
# Gitea Container Registry (self-hosted instance)
REGISTRY: gitea.home-nas-ds.org
# Repository path (format: owner/repo)
IMAGE_NAME: alessio/data-coupler
jobs:
build-linux:
name: Build Linux Container
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Necessario per MinVer: deve percorrere tutta la storia Git per trovare i tag
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Generate version.json with MinVer
run: |
# Fetch all tags for MinVer to work correctly
git fetch --tags --force
# Build project to trigger MinVer (calcola versione automaticamente)
cd Data_Coupler
dotnet build -c Release /p:ContinuousIntegrationBuild=true
# Extract version calculated by MinVer from build output
VERSION=$(dotnet msbuild -getProperty:Version -p:ContinuousIntegrationBuild=true 2>/dev/null | tail -1)
# Fallback if MinVer fails (no tags)
if [ -z "$VERSION" ] || [ "$VERSION" = "0.0.0-alpha.0" ]; then
echo "Warning: No git tags found. MinVer returned default. Using fallback."
VERSION="2.3.2-alpha.0.$(git rev-list --count HEAD)"
fi
echo "MinVer calculated version: $VERSION"
# Create version.json
cat > wwwroot/version.json <<EOF
{
"version": "${VERSION}",
"commitSha": "${GITHUB_SHA:0:7}",
"branch": "${GITHUB_REF_NAME}",
"buildDate": "$(date -u +"%Y-%m-%d %H:%M:%S UTC")",
"buildEnvironment": "Gitea Actions"
}
EOF
echo "Generated version.json:"
cat wwwroot/version.json
cd ..
# Esporta la versione come variabile d'ambiente per il Docker build
echo "APP_VERSION=$VERSION" >> "$GITHUB_ENV"
shell: bash
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Debug - Check registry access
run: |
echo "Testing registry access..."
curl -v https://gitea.home-nas-ds.org/v2/ || echo "Registry not accessible"
echo "Registry: ${{ env.REGISTRY }}"
echo "Image: ${{ env.IMAGE_NAME }}"
continue-on-error: true
- name: Debug - Verify secret is configured
run: |
if [ -z "${{ secrets.REGISTRY_TOKEN }}" ]; then
echo "::error::REGISTRY_TOKEN secret is not configured or is empty!"
exit 1
else
echo "REGISTRY_TOKEN secret is configured (length: ${#REGISTRY_TOKEN})"
fi
env:
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
- name: Log in to Gitea Container Registry
run: |
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u alessio --password-stdin
shell: bash
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Tag based on branch - latest only for main
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
type=raw,value=latest-linux,enable=${{ github.ref == 'refs/heads/main' }}
# Development branch - no latest tag
type=raw,value=development-latest,enable=${{ github.ref == 'refs/heads/development' }}
type=raw,value=development-latest-linux,enable=${{ github.ref == 'refs/heads/development' }}
# Dev branch
type=raw,value=dev-latest,enable=${{ github.ref == 'refs/heads/dev' }}
type=raw,value=dev-latest-linux,enable=${{ github.ref == 'refs/heads/dev' }}
# Staging branch
type=raw,value=staging-latest,enable=${{ github.ref == 'refs/heads/staging' }}
type=raw,value=staging-latest-linux,enable=${{ github.ref == 'refs/heads/staging' }}
# Tag with commit sha
type=sha,prefix={{branch}}-,format=short
# Tag with date
type=raw,value={{branch}}-{{date 'YYYYMMDD-HHmmss'}}
flavor: |
latest=false
- name: Build and push Linux Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
# Aumenta timeout per registry lenti
build-args: |
APP_VERSION=${{ env.APP_VERSION }}
BUILDKIT_STEP_LOG_MAX_SIZE=50000000
provenance: false
sbom: false
env:
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
- name: Retry push on failure
if: failure() && steps.build.outcome == 'failure'
run: |
echo "Retry push after 30 seconds..."
sleep 30
docker push $(echo "${{ steps.meta.outputs.tags }}" | head -n1)
build-windows:
name: Build Windows Container
runs-on: windows
permissions:
contents: read
packages: write
steps:
- name: Checkout repository with Git
run: |
git clone --branch ${{ github.ref_name }} https://alessio:%REGISTRY_TOKEN%@gitea.home-nas-ds.org/${{ github.repository }}.git .
if not exist Dockerfile.windows (
echo ERROR: Dockerfile.windows not found
exit /b 1
)
echo SUCCESS: Repository cloned
env:
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
shell: cmd
- name: Setup .NET
run: |
# .NET should already be available on Windows runner
dotnet --version
shell: pwsh
- name: Generate version.json with MinVer
run: |
# Fetch all tags for MinVer to work correctly
git fetch --tags --force
# Build project to trigger MinVer
cd Data_Coupler
dotnet build -c Release /p:ContinuousIntegrationBuild=true
# Extract version calculated by MinVer
$VERSION = dotnet msbuild -getProperty:Version -p:ContinuousIntegrationBuild=true 2>$null | Select-Object -Last 1
# Fallback if MinVer fails (no tags)
if ([string]::IsNullOrWhiteSpace($VERSION) -or $VERSION -eq "0.0.0-alpha.0") {
Write-Host "Warning: No git tags found. MinVer returned default. Using fallback."
$commitCount = git rev-list --count HEAD
$VERSION = "2.3.2-alpha.0.$commitCount"
}
Write-Host "MinVer calculated version: $VERSION"
$COMMIT_SHA = "${{ github.sha }}"
$SHORT_SHA = $COMMIT_SHA.Substring(0, 7)
$BRANCH = "${{ github.ref_name }}"
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss UTC")
# Create version.json
$versionJson = @{
version = $VERSION
commitSha = $SHORT_SHA
branch = $BRANCH
buildDate = $BUILD_DATE
buildEnvironment = "Gitea Actions"
} | ConvertTo-Json
$versionJson | Out-File -FilePath "wwwroot\version.json" -Encoding UTF8
Write-Host "Generated version.json:"
Get-Content "wwwroot\version.json"
cd ..
# Esporta la versione come variabile d'ambiente per il Docker build
"APP_VERSION=$VERSION" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
Write-Host "APP_VERSION=$VERSION esportata per Docker build"
shell: pwsh
- name: Debug - Verify files
run: |
echo Working directory:
cd
echo.
echo Dockerfiles found:
dir Dockerfile* /B 2>nul || echo No Dockerfiles found
shell: cmd
continue-on-error: true
- name: Debug - Verify secret
run: |
if "${{ secrets.REGISTRY_TOKEN }}"=="" (
echo ERROR: REGISTRY_TOKEN not configured!
exit /b 1
) else (
echo REGISTRY_TOKEN is configured
)
shell: cmd
- name: Log in to Gitea Container Registry
run: echo ${{ secrets.REGISTRY_TOKEN }} | docker login ${{ env.REGISTRY }} -u alessio --password-stdin
shell: cmd
- name: Build and push Windows Docker image
run: |
set IMAGE_LOWER=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
set BRANCH=${{ github.ref_name }}
set SHA=${{ github.sha }}
set SHORT_SHA=%SHA:~0,7%
REM Determine tags based on branch
set TAGS=
if "%BRANCH%"=="main" (
set TAGS=%IMAGE_LOWER%:latest-windows
set TAGS=%TAGS% %IMAGE_LOWER%:main-windows-%SHORT_SHA%
)
if "%BRANCH%"=="development" (
set TAGS=%IMAGE_LOWER%:latest-windows
set TAGS=%TAGS% %IMAGE_LOWER%:development-latest-windows
set TAGS=%TAGS% %IMAGE_LOWER%:development-windows-%SHORT_SHA%
)
if "%BRANCH%"=="staging" (
set TAGS=%IMAGE_LOWER%:staging-latest-windows
set TAGS=%TAGS% %IMAGE_LOWER%:staging-windows-%SHORT_SHA%
)
echo Building Windows Docker image...
docker build --build-arg APP_VERSION=%APP_VERSION% -t temp-windows -f Dockerfile.windows .
if errorlevel 1 (
echo Build failed!
exit /b 1
)
echo Tagging and pushing images...
for %%t in (%TAGS%) do (
echo Tagging: %%t
docker tag temp-windows %%t
echo Pushing: %%t
docker push %%t
)
echo Cleaning up temporary image...
docker rmi temp-windows
echo Windows build completed successfully!
shell: cmd
create-manifest:
name: Create Multi-Platform Manifest
runs-on: ubuntu-latest
needs: [build-linux, build-windows]
permissions:
contents: read
packages: write
steps:
- name: Log in to Gitea Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: alessio
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Create and push manifest for main branch
if: github.ref == 'refs/heads/main'
run: |
IMAGE_LOWER=$(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
docker buildx imagetools create -t ${IMAGE_LOWER}:latest \
${IMAGE_LOWER}:latest-linux \
${IMAGE_LOWER}:latest-windows
- name: Create and push manifest for development branch
if: github.ref == 'refs/heads/development'
run: |
IMAGE_LOWER=$(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
docker buildx imagetools create -t ${IMAGE_LOWER}:development-latest \
${IMAGE_LOWER}:development-latest-linux \
${IMAGE_LOWER}:development-latest-windows
- name: Create and push manifest for dev branch
if: github.ref == 'refs/heads/dev'
run: |
IMAGE_LOWER=$(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
docker buildx imagetools create -t ${IMAGE_LOWER}:dev-latest \
${IMAGE_LOWER}:dev-latest-linux \
${IMAGE_LOWER}:dev-latest-windows
- name: Create and push manifest for staging branch
if: github.ref == 'refs/heads/staging'
run: |
IMAGE_LOWER=$(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
docker buildx imagetools create -t ${IMAGE_LOWER}:staging-latest \
${IMAGE_LOWER}:staging-latest-linux \
${IMAGE_LOWER}:staging-latest-windows