Continuous Deployment #20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Continuous Deployment | |
| on: | |
| # Auto-deploy to dev after successful CI | |
| workflow_run: | |
| workflows: ["CI"] | |
| types: [completed] | |
| branches: [main] | |
| # Manual deployment to any environment | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: 'Environment to deploy to' | |
| required: true | |
| type: choice | |
| options: | |
| - dev | |
| - stage | |
| - prod | |
| default: dev | |
| concurrency: | |
| group: deploy-${{ github.event.inputs.environment || vars.ENVIRONMENT_DEV || 'dev' }} | |
| cancel-in-progress: false # Don't cancel in-progress deployments | |
| env: | |
| DOTNET_VERSION: '10.0.x' | |
| BUILD_CONFIGURATION: 'Release' | |
| DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true | |
| DOTNET_CLI_TELEMETRY_OPTOUT: true | |
| APPLICATION_PROJECT_PATH: 'src/SmartCafe.Menu.API/SmartCafe.Menu.API.csproj' | |
| ENVIRONMENT: ${{ github.event.inputs.environment || vars.ENVIRONMENT_DEV || 'dev' }} | |
| jobs: | |
| # Build and publish the application | |
| publish: | |
| name: Publish Application | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha || github.sha }} | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v5 | |
| with: | |
| dotnet-version: ${{ env.DOTNET_VERSION }} | |
| - name: Cache NuGet packages | |
| uses: actions/cache@v5 | |
| with: | |
| path: ~/.nuget/packages | |
| key: nuget-${{ hashFiles('**/*.csproj', '**/*.slnx', '**/Directory.Build.props', '**/Directory.Packages.props') }} | |
| restore-keys: | | |
| nuget- | |
| - name: Restore dependencies | |
| run: dotnet restore ${{ env.APPLICATION_PROJECT_PATH }} | |
| - name: Build solution | |
| run: dotnet build ${{ env.APPLICATION_PROJECT_PATH }} --configuration ${{ env.BUILD_CONFIGURATION }} --no-restore | |
| - name: Publish application | |
| run: | | |
| dotnet publish ${{ env.APPLICATION_PROJECT_PATH }} \ | |
| --configuration ${{ env.BUILD_CONFIGURATION }} \ | |
| --no-build \ | |
| --output ./publish | |
| - name: Upload publish artifact | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: app-package | |
| path: ./publish | |
| retention-days: 7 | |
| # Deploy to Azure App Service | |
| deploy: | |
| name: Deploy to ${{ github.event.inputs.environment || vars.ENVIRONMENT_DEV || 'dev' }} | |
| runs-on: ubuntu-latest | |
| needs: publish | |
| if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} | |
| environment: ${{ github.event.inputs.environment || vars.ENVIRONMENT_DEV || 'dev' }} | |
| permissions: | |
| id-token: write # Required for OIDC authentication | |
| contents: read | |
| steps: | |
| - name: Download publish artifact | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: app-package | |
| path: ./publish | |
| - name: Azure Login (OIDC) | |
| uses: azure/login@v2 | |
| with: | |
| client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
| tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
| subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
| - name: Deploy to Azure App Service | |
| uses: azure/webapps-deploy@v3 | |
| with: | |
| app-name: ${{ vars.AZURE_APP_SERVICE_NAME }} | |
| package: ./publish | |
| - name: Azure Logout | |
| if: always() | |
| run: az logout |