Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Last active April 6, 2022 17:59
Show Gist options
  • Save seesharprun/d81db9a51fa1868717dad58d0da44548 to your computer and use it in GitHub Desktop.
Save seesharprun/d81db9a51fa1868717dad58d0da44548 to your computer and use it in GitHub Desktop.
Automating the deployment of Azure Resource Manager templates in your DevOps workflow

Demos

Init app

dotnet new webapp
dotnet new gitignore
dotnet run

dotnet publish --configuration Release --output out

dotnet .\out\<name-of-project>.dll

Dockerize

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish --configuration release --output out

FROM mcr.microsoft.com/dotnet/aspnet:6.0

WORKDIR /app

COPY --from=build /app/out ./

ENTRYPOINT [ "dotnet", "<name-of-project>.dll" ]
[Bb]in/
[Oo]bj/
[Oo]ut/
docker build --tag aspnetrazor .
docker run --detach --publish 5550:80 aspnetrazor

Build and push Docker workflow

name: .NET Continuous Delivery
on: push
jobs:
  build-container:
    name: Build and push Docker container image
    runs-on: ubuntu-latest
    steps:
- uses: actions/checkout@v3
  name: Checkout code
- uses: docker/build-push-action@v2
  name: Build Docker container imagte
  with:
    context: .
    push: false
- uses: docker/login-action@v1
  name: Login to container registry
  with:
    registry: <resource-name>.azurecr.io
    username: ${{ secrets.DOCKER_REGISTRY_IDENTITY }}
    password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}
- uses: docker/build-push-action@v2
  name: Build Docker container imagte
  with:
    context: .
    push: true
    tags: |
      <resource-name>.azurecr.io/aspnetrazor:${{ github.run_number }}
      <resource-name>.azurecr.io/aspnetrazor:latest

Push Bicep workflow

deploy-template:
  name: Deploy Bicep template to Azure
  runs-on: ubuntu-latest
  needs: build-container
  steps:
$id=(az group show --name <resource-group-name> --query 'id' --output tsv)
echo $id
az ad sp create-for-rbac --name <aad-app-name> --sdk-auth --role contributor --scopes $id
- uses: azure/login@v1
  name: Login to Azure CLI
  with:
    creds: ${{ secrets.AZURE_CREDENTIALS }}
- run: |
    az account show
  name: Show account details
param location string = resourceGroup().location

resource registry 'Microsoft.ContainerRegistry/registries@2021-12-01-preview' = {
  name: '<name-of-acr-resource>'
  location: location
  sku: {
    name: 'Standard'
  }
  properties: {
    adminUserEnabled: true
  }
}
az deployment group create \
    --resource-group <resource-group-name> \
    --template-file <filename>.bicep
param appName string = uniqueString(resourceGroup().id)

resource container 'Microsoft.ContainerInstance/containerGroups@2021-10-01' = {
  name: appName
  location: location
  properties: {
    osType: 'Linux'
    imageRegistryCredentials: [
      {
        server: '<acr-resource-name>.azurecr.io'
        username: '<acr-resource-name>'
        password: '<acr-password>'
      }
    ]
    containers: [
      {
        name: 'webfrontend'
        properties: {          
          image: '<acr-resource-name>.azurecr.io/aspnetrazor:latest'
          ports: [
            {
              port: 80
              protocol: 'TCP'
            }
          ]
          resources: {
            requests: {
              cpu: 1
              memoryInGB: 2
            }
          }
        }
      }
    ]
    ipAddress: {
      type: 'Public'
      dnsNameLabel: appName
      ports: [
        {
          port: 80
          protocol: 'TCP'
        }
      ]
    }
  }
}
- uses: actions/checkout@v3
  name: Checkout code
  with:
    repository: <organization>/<repository>
- run: |
    az deployment group create \
      --resource-group <resource-group-name> \
      --template-file <template-file>.bicep
  name: Deploy template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment