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 <$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 .. 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 -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