Skip to content

Containerize

Containerize #116

Workflow file for this run

---
name: Application
"on":
push:
branches:
- main
paths:
- .github/workflows/application.yml
- src/**
- tests/**
pull_request:
branches:
- main
paths:
- .github/workflows/application.yml
- src/**
- tests/**
workflow_dispatch:
permissions:
contents: read
jobs:
build:
name: Build
runs-on: ubuntu-20.04 # Test containers fails on ubuntu-latest as of 2024-10-02
environment: Build
env:
CONFIGURATION: Release
permissions:
checks: write
id-token: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4.2.0
- name: Setup .NET
uses: actions/setup-dotnet@v4.0.1
with:
dotnet-version: 9.0.x
- name: .NET Restore
run: dotnet restore
- name: .NET Build
run: dotnet build --no-restore --configuration ${{ env.CONFIGURATION }}
- name: .NET Test
run: dotnet test --no-build --configuration ${{ env.CONFIGURATION }} --logger:"xunit;LogFilePath=TestResult.xml"
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2.17.1
if: always()
with:
files: |
**/TestResult.xml
- name: Build Container Image
run: dotnet publish src/MovieApi/ --no-restore --configuration ${{ env.CONFIGURATION }} /t:PublishContainer -p ContainerImageTags="${{ github.sha }}"
- name: Azure Login
uses: azure/login@v2.2.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Push Image to Azure Container Registry
run: |
az acr login --name ${{ vars.CONTAINER_REGISTRY }}
IMAGE="${{ vars.CONTAINER_REGISTRY }}.azurecr.io/ondfisk-githubdemo"
docker tag "ondfisk-githubdemo:${{ github.sha }}" "$IMAGE:1.0"
docker tag "ondfisk-githubdemo:${{ github.sha }}" "$IMAGE:${{ github.sha }}"
docker push $IMAGE --all-tags
- name: Install EF Tool
run: |
echo "/github/home/.dotnet/tools" >> "$GITHUB_PATH"
dotnet tool install --global dotnet-ef --prerelease
- name: Generate EF Migration Script
run: dotnet ef migrations script --idempotent --project src/MovieApi/ --startup-project src/MovieApi/ --no-build --output migrate.sql
- name: Upload EF Migration Script
uses: actions/upload-artifact@v4.4.0
with:
name: migrate.sql
path: migrate.sql
staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
environment: Staging
env:
CONNECTION_STRING:
permissions:
id-token: write
steps:
- name: Download EF Migration Script
uses: actions/download-artifact@v4.1.8
with:
name: migrate.sql
path: .
- name: Azure Login
uses: azure/login@v2.2.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Get Connection String
uses: azure/cli@v2.1.0
with:
inlineScript: |
CONNECTION_STRING=$(az webapp config appsettings list --resource-group ${{ vars.RESOURCE_GROUP }} --name ${{ vars.WEBAPP }} --slot ${{ vars.DEPLOYMENT_SLOT }} --query "[?name=='AZURE_SQL_CONNECTIONSTRING'].value" --output tsv)
echo "CONNECTION_STRING=${CONNECTION_STRING/Authentication=ActiveDirectoryManagedIdentity/Authentication=\"Active Directory Default\"}" >> "$GITHUB_ENV"
- name: Apply EF migration script
uses: Azure/sql-action@v2.3
with:
connection-string: ${{ env.CONNECTION_STRING }}
path: migrate.sql
- name: Deploy Web App Container
uses: azure/webapps-deploy@v3.0.2
with:
app-name: ${{ vars.WEBAPP }}
slot-name: ${{ vars.DEPLOYMENT_SLOT }}
images: ${{ vars.CONTAINER_REGISTRY }}.azurecr.io/ondfisk-githubdemo:${{ github.sha }}
production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: staging
if: github.ref == 'refs/heads/main'
environment: Production
env:
CONNECTION_STRING:
VERSION: "1.0"
permissions:
id-token: write
steps:
- name: Download EF Migration Script
uses: actions/download-artifact@v4.1.8
with:
name: migrate.sql
path: .
- name: Azure Login
uses: azure/login@v2.2.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Get Connection String
uses: azure/cli@v2.1.0
with:
inlineScript: |
CONNECTION_STRING=$(az webapp config appsettings list --resource-group ${{ vars.RESOURCE_GROUP }} --name ${{ vars.WEBAPP }} --query "[?name=='AZURE_SQL_CONNECTIONSTRING'].value" --output tsv)
echo "CONNECTION_STRING=${CONNECTION_STRING/Authentication=ActiveDirectoryManagedIdentity/Authentication=\"Active Directory Default\"}" >> "$GITHUB_ENV"
- name: Apply EF migration script
uses: Azure/sql-action@v2.3
with:
connection-string: ${{ env.CONNECTION_STRING }}
path: migrate.sql
- name: Tag Image with latest
run: |
az acr login --name ${{ vars.CONTAINER_REGISTRY }}
IMAGE="${{ vars.CONTAINER_REGISTRY }}.azurecr.io/ondfisk-githubdemo"
docker pull "$IMAGE:${{ github.sha }}"
docker tag "$IMAGE:${{ github.sha }}" "$IMAGE:latest"
docker tag "$IMAGE:${{ github.sha }}" "$IMAGE:${{ env.VERSION }}"
docker push $IMAGE --all-tags
- name: Swap Staging With Production
uses: azure/cli@v2.1.0
with:
inlineScript: |
az webapp deployment slot swap --resource-group ${{ vars.RESOURCE_GROUP }} --name ${{ vars.WEBAPP }} --slot ${{ vars.DEPLOYMENT_SLOT }}