Example dotnet pipeline with private NuGet feeds and ACA deployment

Mon, September 30, 2024 - 2 min read
Dockerfile
 
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /apps/projectName
 
RUN curl -L https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | sh
 
# Copy csproj and restore as distinct layers
COPY src/Site/Site.csproj ./
COPY NuGet.config ./
 
#Takes access token from ADO pipeline step
ARG FEED_ACCESSTOKEN
 
#Sets up the nuget feeds
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{\"endpointCredentials\": [{\"endpoint\":\"https://pkgs.dev.azure.com/organisationName/_packaging/feedName/nuget/v3/index.json\", \"username\":\"docker\", \"password\":\"${FEED_ACCESSTOKEN}\"},{\"endpoint\":\"https://pkgs.dev.azure.com/organisationName/_packaging/feedName/nuget/v3/index.json\", \"username\":\"docker\", \"password\":\"${FEED_ACCESSTOKEN}\"}]}"
 
#Restore the nuget packages
RUN dotnet restore "Site.csproj" --configfile "NuGet.config" --ignore-failed-sources
 
COPY src/Site/ ./
RUN dotnet publish -c Release -o out --verbosity n
 
FROM mcr.microsoft.com/dotnet/sdk:8.0
WORKDIR /apps/projectName
COPY --from=build-env /apps/projectName/out . 
 
EXPOSE 80
 
ENTRYPOINT ["dotnet", "projectName.Internet.Site.dll"]s
 
Azure DevOps Pipeline
 
parameters:
  - name: serviceConnectionName
  - name: solution
  - name: acrName
  - name: environment
  - name: dockerFilePath
  - name: acaRG
  - name: acaName
 
stages:
  - stage: 'BuildAndPush'
    variables:
      DOCKER_BUILDKIT: 1
    jobs:
      - deployment:  Deploy
        displayName: 'Build and Push Docker Image'
        environment: '${{parameters.environment}}'
        strategy:
          runOnce:
            deploy:
              steps:
              - checkout: self
                displayName: 'Checkout Repo'
 
              - task: UseDotNet@2
                displayName: 'Install .NET SDK 8.x.x'
                inputs:
                  packageType: 'sdk'
 
              - task: NuGetAuthenticate@1
 
              - task: Docker@2
                displayName: 'Build Docker Image'
                inputs:
                  repository: ${{parameters.solution}}
                  command: build
                  Dockerfile: ${{parameters.dockerFilePath}}
                  tags: $(Build.BuildId)
                  arguments: '--build-arg FEED_ACCESSTOKEN=$(VSS_NUGET_ACCESSTOKEN)'
                  
              - task: AzureCLI@2
                displayName: 'Push Docker Image to ACR'
                inputs:
                  azureSubscription: ${{parameters.serviceConnectionName}}
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                        az acr login --name '${{parameters.acrName}}'
                        docker tag 'docker.io/library/${{parameters.solution}}:$(Build.BuildId)'  '${{parameters.acrName}}.azurecr.io/${{parameters.solution}}:latest'
                        docker push  '${{parameters.acrName}}.azurecr.io/${{parameters.solution}}:latest'
 
              - task: AzureCLI@2
                displayName: 'Update image in ACA'
                inputs:
                  azureSubscription: '${{parameters.serviceConnectionName}}'
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  InlineScript: |
                        az containerapp update -n '${{parameters.acaName}}' \
                        -g '${{parameters.acaRG}}' \
                        --image '${{parameters.acrName}}.azurecr.io/${{parameters.solution}}:latest' \
                        --revision-suffix '$(Build.BuildId)'