GitHub Actions ve Docker ile .NET 8 API'si İçin Tam Otomatik CI/CD Pipeline
Her push'ta otomatik test, build ve Docker image yayını. Sıfır downtime deploy ile production ortamına güvenli geçiş sağlayan CI/CD pipeline'ı nasıl kuruyoruz?
Hedef: Her Commit Güvenli Bir Deploy Olsun
"Çalışıyor mu bilmiyorum, deneyelim" kültüründen çıkmanın tek yolu: otomatize edilmiş, tekrarlanabilir, izlenebilir bir CI/CD süreci. NWTsoft projelerinde kullandığımız pipeline'ı paylaşıyoruz.
Pipeline Genel Akışı
Push to main
│
├─► CI: Build & Test
│ ├── dotnet restore
│ ├── dotnet build --no-restore
│ └── dotnet test --no-build --verbosity normal
│
├─► Docker: Build & Push
│ ├── docker build -t ghcr.io/nwtsoft/api:$SHA .
│ └── docker push ghcr.io/nwtsoft/api:$SHA
│
└─► Deploy: SSH → Server
├── docker pull ghcr.io/nwtsoft/api:$SHA
├── docker stop api-prod || true
└── docker run -d --name api-prod ...
GitHub Actions Workflow
name: CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: nwtsoft/api
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- run: dotnet restore
- run: dotnet build --no-restore -c Release
- run: dotnet test --no-build -c Release --logger trx
build-and-push:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v5
with:
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker pull ghcr.io/nwtsoft/api:${{ github.sha }}
docker stop api-prod 2>/dev/null || true
docker rm api-prod 2>/dev/null || true
docker run -d \
--name api-prod \
--restart unless-stopped \
-p 5008:8080 \
--env-file /opt/nwtsoft/.env \
ghcr.io/nwtsoft/api:${{ github.sha }}
Çok Aşamalı Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["src/NWTsoft.API/NWTsoft.API.csproj", "src/NWTsoft.API/"]
RUN dotnet restore "src/NWTsoft.API/NWTsoft.API.csproj"
COPY . .
RUN dotnet publish "src/NWTsoft.API/NWTsoft.API.csproj" \
-c Release -o /app/publish --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
# Sağlık kontrolü
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
ENTRYPOINT ["dotnet", "NWTsoft.API.dll"]
Kritik Noktalar
- Secrets yönetimi: Database bağlantı stringi ve API anahtarları GitHub Secrets'ta, image'a gömülmez
- Health check: Deploy sonrası container sağlıklı değilse eski image'a otomatik dönülür
- PR'da sadece test: Push to main olmadan docker push tetiklenmiyor
- Image tagging: SHA ile etiketleme sayesinde her deploy izlenebilir ve geri alınabilir