Section 6 of 6

CI/CD Pipelines

🎯 What You'll Learn

  • What is CI/CD
  • GitHub Actions
  • Azure DevOps Pipelines
  • GitLab CI/CD
  • Best practices

What is CI/CD?

CI/CD (Continuous Integration/Continuous Deployment) automates building, testing, and deploying applications whenever code changes.

  • CI (Continuous Integration): Automatically build and test code
  • CD (Continuous Deployment): Automatically deploy to production

GitHub Actions

.github/workflows/deploy.yml YAML
name: Deploy to Azure

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '8.0.x'
    
    - name: Restore dependencies
      run: dotnet restore
    
    - name: Build
      run: dotnet build --configuration Release --no-restore
    
    - name: Test
      run: dotnet test --no-build --verbosity normal
    
    - name: Publish
      run: dotnet publish -c Release -o ./publish
    
    - name: Deploy to Azure Web App
      uses: azure/webapps-deploy@v2
      with:
        app-name: 'inventtrack'
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ./publish

Azure DevOps Pipelines

azure-pipelines.yml YAML
trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
  inputs:
    version: '8.0.x'

- task: DotNetCoreCLI@2
  displayName: 'Restore'
  inputs:
    command: 'restore'

- task: DotNetCoreCLI@2
  displayName: 'Build'
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'Test'
  inputs:
    command: 'test'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'Publish'
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'

- task: AzureWebApp@1
  displayName: 'Deploy to Azure'
  inputs:
    azureSubscription: 'Azure-Subscription'
    appName: 'inventtrack'
    package: '$(Build.ArtifactStagingDirectory)/**/*.zip'

GitLab CI/CD

.gitlab-ci.yml YAML
stages:
  - build
  - test
  - deploy

variables:
  DOTNET_VERSION: "8.0"

build:
  stage: build
  image: mcr.microsoft.com/dotnet/sdk:8.0
  script:
    - dotnet restore
    - dotnet build --configuration Release
  artifacts:
    paths:
      - bin/Release/

test:
  stage: test
  image: mcr.microsoft.com/dotnet/sdk:8.0
  script:
    - dotnet test --configuration Release

deploy:
  stage: deploy
  image: mcr.microsoft.com/dotnet/sdk:8.0
  script:
    - dotnet publish -c Release -o ./publish
    - scp -r ./publish/* user@server:/var/www/inventtrack
    - ssh user@server "sudo systemctl restart inventtrack"
  only:
    - main

Docker Build and Push

GitHub Actions with Docker YAML
name: Docker Build and Push

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    
    - name: Build and push
      uses: docker/build-push-action@v4
      with:
        context: .
        push: true
        tags: yourusername/inventtrack:latest

Best Practices

  • Run tests: Always run tests before deployment
  • Use secrets: Store credentials securely
  • Deploy to staging first: Test before production
  • Rollback plan: Have a way to revert
  • Monitor deployments: Track success/failure
  • Version tags: Tag Docker images with versions

Key Takeaways

  • CI/CD: Automate build, test, deploy
  • GitHub Actions: .github/workflows/*.yml
  • Azure DevOps: azure-pipelines.yml
  • GitLab CI/CD: .gitlab-ci.yml
  • Stages: Build → Test → Deploy
  • Secrets: Store credentials securely
🎉 Part XVIII Complete!

Congratulations! You've completed Part XVIII: Deployment & Hosting. You now understand:

  • ✅ Kestrel and reverse proxies (Nginx, Apache)
  • ✅ IIS hosting (ASP.NET Core Module, web.config)
  • ✅ Linux hosting (systemd, Let's Encrypt)
  • ✅ Docker containers (Dockerfile, Docker Compose)
  • ✅ Azure App Service (deployment, configuration)
  • ✅ CI/CD pipelines (GitHub Actions, Azure DevOps, GitLab)

Your applications are ready for production! 🚀