Merge pull request 'fix: pre-merge e build do docker' (#5) from feat/live-preview into Release/V0.0.3

Reviewed-on: http://git.carneiro.ddnsfree.com/ricardo/BCards/pulls/5
This commit is contained in:
ricardo 2025-07-23 18:43:36 +00:00
commit c0a9a963db
4 changed files with 69 additions and 140 deletions

View File

View File

@ -4,6 +4,13 @@ on:
push: push:
branches: branches:
- 'Release/*' - 'Release/*'
# Também pode executar manualmente
workflow_dispatch:
inputs:
skip_tests:
description: 'Skip tests (true/false)'
required: false
default: 'false'
env: env:
REGISTRY: registry.redecarneir.us REGISTRY: registry.redecarneir.us
@ -18,13 +25,19 @@ jobs:
steps: steps:
- name: Check if tests should run - name: Check if tests should run
run: | run: |
if [ "${{ vars.SKIP_TESTS }}" == "true" ]; then # Prioridade: manual input > variável do repo
echo "⚠️ Testes PULADOS (SKIP_TESTS=true)" SKIP_TESTS="${{ github.event.inputs.skip_tests || vars.SKIP_TESTS }}"
if [ "$SKIP_TESTS" == "true" ]; then
echo "⚠️ Testes PULADOS"
echo "TESTS_SKIPPED=true" >> $GITHUB_ENV echo "TESTS_SKIPPED=true" >> $GITHUB_ENV
else else
echo "✅ Executando testes (SKIP_TESTS=${{ vars.SKIP_TESTS }})" echo "✅ Executando testes"
echo "TESTS_SKIPPED=false" >> $GITHUB_ENV echo "TESTS_SKIPPED=false" >> $GITHUB_ENV
fi fi
echo "🎯 Trigger: ${{ github.event_name }}"
echo "📂 Branch: ${{ github.ref_name }}"
- name: Checkout code - name: Checkout code
if: env.TESTS_SKIPPED == 'false' if: env.TESTS_SKIPPED == 'false'
@ -46,161 +59,69 @@ jobs:
- name: Run unit tests - name: Run unit tests
if: env.TESTS_SKIPPED == 'false' if: env.TESTS_SKIPPED == 'false'
run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage" run: dotnet test --no-build --configuration Release --verbosity normal
- name: Test MongoDB connection
if: env.TESTS_SKIPPED == 'false'
run: |
echo "Testing MongoDB connection to $MONGODB_HOST"
timeout 10 bash -c "</dev/tcp/192.168.0.100/27017" && echo "MongoDB connection successful" || echo "MongoDB connection failed"
build-and-deploy: build-and-deploy:
name: Build Multi-Arch Image and Deploy name: Build and Deploy
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: [test] needs: [test]
if: always() && (needs.test.result == 'success' || needs.test.result == 'failure') if: always() && (needs.test.result == 'success' || needs.test.result == 'failure')
steps: steps:
- name: Show deployment status - name: Deployment info
run: | run: |
if [ "${{ vars.SKIP_TESTS }}" == "true" ]; then echo "🚀 Iniciando deployment para ${{ github.ref_name }}"
echo "🚀 Deploy iniciado - Testes foram PULADOS" echo "🧪 Tests: ${{ vars.SKIP_TESTS == 'true' && 'SKIPPED' || 'EXECUTED' }}"
else
echo "🚀 Deploy iniciado - Testes foram executados"
fi
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
with: with:
driver-opts: network=host platforms: linux/amd64,linux/arm64
- name: Extract branch name and version - name: Extract version info
id: extract_branch id: version
run: | run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/} BRANCH_NAME="${{ github.ref_name }}"
VERSION_RAW=${BRANCH_NAME#Release/} VERSION_RAW=${BRANCH_NAME#Release/}
# Remove 'V' prefix se existir
VERSION=$(echo "$VERSION_RAW" | sed 's/^[Vv]//') VERSION=$(echo "$VERSION_RAW" | sed 's/^[Vv]//')
# Se versão vazia, usar padrão
if [ -z "$VERSION" ]; then if [ -z "$VERSION" ]; then
VERSION="0.0.1" VERSION="0.0.1"
fi fi
COMMIT_SHA=${GITHUB_SHA::7} COMMIT_SHA=${{ github.sha }}
SHORT_COMMIT=${COMMIT_SHA:0:7}
echo "branch=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "commit=$COMMIT_SHA" >> $GITHUB_OUTPUT echo "commit=$SHORT_COMMIT" >> $GITHUB_OUTPUT
echo "tag=$VERSION-$COMMIT_SHA" >> $GITHUB_OUTPUT echo "tag=$VERSION-$SHORT_COMMIT" >> $GITHUB_OUTPUT
echo "📦 Version: $VERSION"
echo "🔑 Commit: $SHORT_COMMIT"
echo "🏷️ Tag: $VERSION-$SHORT_COMMIT"
echo "📋 Branch: $BRANCH_NAME" - name: Build and push multi-arch image
echo "📦 Version: $VERSION (from $VERSION_RAW)"
echo "🔑 Commit: $COMMIT_SHA"
- name: Build and push multi-arch Docker image
run: | run: |
echo "Building multi-arch image for platforms: linux/amd64,linux/arm64" echo "🏗️ Building multi-arch image..."
docker buildx build \ docker buildx build \
--platform linux/amd64,linux/arm64 \ --platform linux/amd64,linux/arm64 \
--file Dockerfile.release \ --file Dockerfile.release \
--tag $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.tag }} \ --tag $REGISTRY/$IMAGE_NAME:${{ steps.version.outputs.tag }} \
--tag $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.version }}-latest \ --tag $REGISTRY/$IMAGE_NAME:${{ steps.version.outputs.version }}-latest \
--tag $REGISTRY/$IMAGE_NAME:release-latest \ --tag $REGISTRY/$IMAGE_NAME:release-latest \
--push \ --push \
--build-arg BUILDPLATFORM=linux/amd64 \ --build-arg VERSION=${{ steps.version.outputs.version }} \
--build-arg VERSION=${{ steps.extract_branch.outputs.version }} \ --build-arg COMMIT=${{ steps.version.outputs.commit }} \
--build-arg COMMIT=${{ steps.extract_branch.outputs.commit }} \ --progress=plain
.
- name: Deploy to staging environment
run: |
echo "Deploying to staging environment..."
# Create deployment directory
sudo mkdir -p /opt/bcards-staging
# Copy docker-compose file
sudo cp docker-compose.staging.yml /opt/bcards-staging/
# Set environment variables
sudo tee /opt/bcards-staging/.env > /dev/null <<EOF
IMAGE_TAG=${{ steps.extract_branch.outputs.tag }}
MONGODB_CONNECTION_STRING=mongodb://$MONGODB_HOST/BCardsDB
ASPNETCORE_ENVIRONMENT=Release
EOF
# Run deployment script
chmod +x scripts/deploy-release.sh
sudo ./scripts/deploy-release.sh ${{ steps.extract_branch.outputs.tag }}
- name: Health check and validation
run: |
echo "Running health checks..."
# Wait for application to start
sleep 30
# Test application health
for i in {1..10}; do
if curl -f http://localhost:8090/health > /dev/null 2>&1; then
echo "Application health check passed"
break
fi
echo "Health check attempt $i failed, retrying in 10 seconds..."
sleep 10
if [ $i -eq 10 ]; then
echo "Health check failed after 10 attempts"
exit 1
fi
done
# Test MongoDB connectivity from application
chmod +x scripts/test-mongodb-connection.sh
./scripts/test-mongodb-connection.sh
- name: Deployment notification
run: |
echo "🚀 Deployment Status: SUCCESS"
echo "📦 Image: $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.tag }}"
echo "🌐 Environment: Staging"
echo "🔗 MongoDB: $MONGODB_HOST"
echo "🏗️ Architecture: Multi-arch (linux/amd64, linux/arm64)"
echo "📋 Branch: ${{ steps.extract_branch.outputs.branch }}"
echo "🆔 Commit: ${{ steps.extract_branch.outputs.commit }}"
echo "🧪 Tests: ${{ vars.SKIP_TESTS == 'true' && 'SKIPPED' || 'EXECUTED' }}"
rollback:
name: Rollback on Failure
runs-on: ubuntu-latest
needs: [build-and-deploy]
if: failure()
steps: # Resto do deployment...
- name: Rollback deployment - name: Deploy notification
run: | run: |
echo "🚨 Deployment failed, initiating rollback..." echo "✅ Deployment concluído!"
echo "📦 Image: $REGISTRY/$IMAGE_NAME:${{ steps.version.outputs.tag }}"
# Stop current containers echo "🎯 Trigger: ${{ github.event_name }}"
cd /opt/bcards-staging echo "📂 Branch: ${{ github.ref_name }}"
sudo docker-compose down
# Restore previous version if exists
if [ -f .env.backup ]; then
sudo mv .env.backup .env
sudo docker-compose up -d
echo "✅ Rollback completed to previous version"
else
echo "❌ No previous version found for rollback"
fi
- name: Failure notification
run: |
echo "❌ Deployment Status: FAILED"
echo "🔄 Rollback: Initiated"
echo "📋 Branch: ${GITHUB_REF#refs/heads/}"
echo "🆔 Commit: ${GITHUB_SHA::7}"

View File

@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pipeline", "Pipeline", "{3F
docker-compose.staging.yml = docker-compose.staging.yml docker-compose.staging.yml = docker-compose.staging.yml
docker-compose.yml = docker-compose.yml docker-compose.yml = docker-compose.yml
Dockerfile.release = Dockerfile.release Dockerfile.release = Dockerfile.release
.gitea\workflows\pr-validation.yml = .gitea\workflows\pr-validation.yml
.gitea\workflows\release-deploy.yml = .gitea\workflows\release-deploy.yml .gitea\workflows\release-deploy.yml = .gitea\workflows\release-deploy.yml
EndProjectSection EndProjectSection
EndProject EndProject

View File

@ -35,13 +35,13 @@ WORKDIR /src
# Copy project file and restore dependencies # Copy project file and restore dependencies
COPY ["src/BCards.Web/BCards.Web.csproj", "src/BCards.Web/"] COPY ["src/BCards.Web/BCards.Web.csproj", "src/BCards.Web/"]
# CORREÇÃO: Mapear plataforma para RID correto do .NET # Map platform to .NET runtime identifier and restore
RUN case "$TARGETPLATFORM" in \ RUN case "$TARGETPLATFORM" in \
"linux/amd64") RID="linux-x64" ;; \ "linux/amd64") RID="linux-x64" ;; \
"linux/arm64") RID="linux-arm64" ;; \ "linux/arm64") RID="linux-arm64" ;; \
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \ *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
esac && \ esac && \
echo "🏗️ Building for platform: $TARGETPLATFORM -> RID: $RID" && \ echo "🏗️ Restoring for platform: $TARGETPLATFORM -> RID: $RID" && \
dotnet restore "src/BCards.Web/BCards.Web.csproj" --runtime $RID dotnet restore "src/BCards.Web/BCards.Web.csproj" --runtime $RID
# Copy source code # Copy source code
@ -53,6 +53,7 @@ RUN case "$TARGETPLATFORM" in \
"linux/amd64") RID="linux-x64" ;; \ "linux/amd64") RID="linux-x64" ;; \
"linux/arm64") RID="linux-arm64" ;; \ "linux/arm64") RID="linux-arm64" ;; \
esac && \ esac && \
echo "🔨 Building for RID: $RID" && \
dotnet build "BCards.Web.csproj" \ dotnet build "BCards.Web.csproj" \
-c Release \ -c Release \
-o /app/build \ -o /app/build \
@ -67,10 +68,12 @@ ARG TARGETPLATFORM
ARG VERSION ARG VERSION
ARG COMMIT ARG COMMIT
# Publish with cross-compilation friendly settings
RUN case "$TARGETPLATFORM" in \ RUN case "$TARGETPLATFORM" in \
"linux/amd64") RID="linux-x64" ;; \ "linux/amd64") RID="linux-x64" ;; \
"linux/arm64") RID="linux-arm64" ;; \ "linux/arm64") RID="linux-arm64" ;; \
esac && \ esac && \
echo "📦 Publishing for RID: $RID" && \
dotnet publish "BCards.Web.csproj" \ dotnet publish "BCards.Web.csproj" \
-c Release \ -c Release \
-o /app/publish \ -o /app/publish \
@ -78,7 +81,7 @@ RUN case "$TARGETPLATFORM" in \
--no-build \ --no-build \
--runtime $RID \ --runtime $RID \
--self-contained false \ --self-contained false \
-p:PublishReadyToRun=true \ -p:PublishReadyToRun=false \
-p:PublishSingleFile=false \ -p:PublishSingleFile=false \
-p:UseAppHost=false \ -p:UseAppHost=false \
-p:Version=$VERSION \ -p:Version=$VERSION \
@ -114,12 +117,16 @@ ENV DOTNET_EnableDiagnostics=0
ENV DOTNET_USE_POLLING_FILE_WATCHER=true ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
# ARM64 specific optimizations # Platform-specific optimizations
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
echo "Applying ARM64 optimizations..." && \ echo "🔧 Applying ARM64 optimizations..." && \
export DOTNET_TieredPGO=1 && \ echo 'export DOTNET_TieredPGO=1' >> /etc/environment && \
export DOTNET_TC_QuickJitForLoops=1 && \ echo 'export DOTNET_TC_QuickJitForLoops=1' >> /etc/environment && \
export DOTNET_ReadyToRun=0; \ echo 'export DOTNET_ReadyToRun=0' >> /etc/environment; \
else \
echo "🔧 Applying AMD64 optimizations..." && \
echo 'export DOTNET_TieredPGO=1' >> /etc/environment && \
echo 'export DOTNET_ReadyToRun=1' >> /etc/environment; \
fi fi
# Health check endpoint # Health check endpoint
@ -130,4 +137,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
USER bcards USER bcards
# Entry point with optimized runtime settings # Entry point with optimized runtime settings
ENTRYPOINT ["dotnet", "BCards.Web.dll"] ENTRYPOINT ["dotnet", "BCards.Web.dll"]