Files
Data-Coupler/.github/workflows/docker-build.yml
T
Alessio Dal Santo 46fc21bf7b [Fix] GitHub Actions: aggiunge generazione version.json prima del Docker build
Il workflow GitHub non generava version.json sul runner prima del build,
quindi Docker copiava il file statico del repository (con versione vecchia 2.1.0).

La Gitea Actions usava gia questo approccio correttamente.

Fix applicato: lo step 'Calcola versione' ora genera anche version.json in
Data_Coupler/wwwroot/version.json per entrambi i job Linux e Windows,
con versione, commit SHA, branch, data build e ambiente (GitHub Actions).

Il VersionService legge version.json all'avvio per display nell'UI.
2026-03-22 16:11:30 +01:00

271 lines
9.5 KiB
YAML

name: Build and Push Docker Images
on:
push:
branches:
- main
- development
- staging
workflow_dispatch:
inputs:
force_build:
description: 'Force build even without code changes'
required: false
default: false
type: boolean
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-linux:
name: Build Linux Container
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
attestations: 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: Calcola versione e genera version.json
run: |
git fetch --tags --force
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LATEST_TAG" ]; then
echo "Warning: Nessun tag Git trovato su questo remote. Uso fallback."
VERSION="2.3.2"
else
VERSION="${LATEST_TAG#v}"
fi
echo "Versione calcolata: $VERSION (da tag: $LATEST_TAG)"
# Genera version.json
cat > Data_Coupler/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": "GitHub Actions"
}
EOF
echo "Generated version.json:"
cat Data_Coupler/wwwroot/version.json
echo "APP_VERSION=$VERSION" >> "$GITHUB_ENV"
shell: bash
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- 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' }}
# Development branch - no latest tag
type=raw,value=development-latest,enable=${{ github.ref == 'refs/heads/development' }}
# Dev branch
type=raw,value=dev-latest,enable=${{ github.ref == 'refs/heads/dev' }}
# Staging branch
type=raw,value=staging-latest,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 }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
build-args: |
APP_VERSION=${{ env.APP_VERSION }}
- name: Generate artifact attestation
if: github.event_name != 'pull_request'
continue-on-error: true
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
subject-digest: ${{ steps.build.outputs.digest }}
push-to-registry: true
build-windows:
name: Build Windows Container
runs-on: windows-2022
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: Calcola versione e genera version.json
run: |
git fetch --tags --force
$LATEST_TAG = git describe --tags --abbrev=0 2>$null
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($LATEST_TAG)) {
Write-Host "Warning: Nessun tag Git trovato su questo remote. Uso fallback."
$VERSION = "2.3.2"
} else {
$VERSION = $LATEST_TAG -replace '^v', ''
}
Write-Host "Versione calcolata: $VERSION (da tag: $LATEST_TAG)"
$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")
# Genera version.json
$versionJson = @{
version = $VERSION
commitSha = $SHORT_SHA
branch = $BRANCH
buildDate = $BUILD_DATE
buildEnvironment = "GitHub Actions"
} | ConvertTo-Json
$versionJson | Out-File -FilePath "Data_Coupler\wwwroot\version.json" -Encoding UTF8
Write-Host "Generated version.json:"
Get-Content "Data_Coupler\wwwroot\version.json"
"APP_VERSION=$VERSION" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
shell: pwsh
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Tag based on branch with windows suffix
type=raw,value=latest-windows,enable=${{ github.ref == 'refs/heads/main' }}
type=raw,value=latest-windows,enable=${{ github.ref == 'refs/heads/development' }}
type=raw,value=development-latest-windows,enable=${{ github.ref == 'refs/heads/development' }}
type=raw,value=dev-latest-windows,enable=${{ github.ref == 'refs/heads/dev' }}
type=raw,value=staging-latest-windows,enable=${{ github.ref == 'refs/heads/staging' }}
# Tag with commit sha
type=sha,prefix={{branch}}-windows-,format=short
# Tag with date
type=raw,value={{branch}}-windows-{{date 'YYYYMMDD-HHmmss'}}
flavor: |
latest=false
- name: Build and push Windows Docker image
run: |
# Convert repository name to lowercase
$imageName = "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}".ToLower()
# Build with temporary tag
docker build --build-arg "APP_VERSION=$env:APP_VERSION" -t "${imageName}:temp-windows" -f Dockerfile.windows .
# Parse and push all tags
$tags = "${{ steps.meta.outputs.tags }}" -split "`n"
foreach ($tag in $tags) {
$tag = $tag.Trim()
if ($tag) {
# Ensure tag is lowercase
$tag = $tag.ToLower()
Write-Host "Tagging and pushing: $tag"
docker tag "${imageName}:temp-windows" $tag
docker push $tag
}
}
# Remove temporary tag
docker rmi "${imageName}:temp-windows"
shell: pwsh
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 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_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 \
${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 \
${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 \
${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 \
${IMAGE_LOWER}:staging-latest-windows