Deploy containerized .NET 5 app to Heroku from Azure DevOps

Ngo Thien Sinh
4 min readJan 14, 2021

I recently was asked by a student about how to access his website outside of his local computer, from the internet. He wants to share the site that he developed with his friends and teacher
This is likely the first question that related to my major rather than some silly question: Could you fix the TV? Could you fix the refrigerator? Why not???? Are you learning IT? 😓

Let helps him out 💪💪💪

I’ve made some quick research and Heroku come into the first free-tier option. Unfortunately, It does not support the .NET app. We have to deploy our app along with the docker. Luckily Heroku supports us that way, totally free.

Because I don’t have docker installed on my local machine, so let Microsoft hosted agent do that for us, via Azure DevOps.

Just head up: You can skip the tutorials and check out my git repository here and my demo web here

1, Create a new .NET Core App

Sign in to Azure DevOps and create your repository

mnbm

Clone your git repository

git clone https://dandynamite.visualstudio.com/heroku-docker-deploy-net5/_git/my-test-mvc-app

Go to the folder of the local repository

cd my-test-mvc-app

Create your .NET Core MVC web app using the below command

dotnet new mvc

2, Add Docker file and build your containerized app

Add *Dockerfile* file

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build-envWORKDIR /app# Copy csproj and restore as distinct layersCOPY *.csproj ./RUN dotnet restore# Copy everything else and buildCOPY . ./RUN dotnet publish -c Release -o out# Build runtime imageFROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slimWORKDIR /appCOPY --from=build-env /app/out .# ENTRYPOINT ["dotnet", "my-test-mvc-app.dll"]CMD ASPNETCORE_URLS=http://*:$PORT dotnet my-test-mvc-app.dll

Add .gitignore file if you want

obj/
bin/

Add .dockerignore file if you want

bin/
obj/

Now let's try to build your app if you have docker installed on your local machine

docker build -t my-test-app .

Now commit and push your code

git add .

git commit -m "Init project"

git push

3, Create your Heroku app and get the Heroku API key

Login to the Heroku dashboard, click create a new app, remember your app name

At the dashboard, click on the avatar > account settings > API Key > rev

4, Setup a pipeline to tell Microsoft Hosted Agent to build your containerized app and deploy it to Heroku

Go to Azure DevOps, create a pipeline

  • At the Connect step, choose Azure Repos Git YAML
  • At the Select step, choose your repository: my-test-mvc-app
  • At the Configure step, select Starter pipeline
  • At the Review step, the pipeline definition will be shown
trigger:- masterpool:vmImage: 'ubuntu-latest'steps:- script: |# npm install -global Herokusudo snap install --classic herokudisplayName: 'i heroku cli'- script: |echo CONTAINER LOGINheroku container:loginecho PUSHheroku container:push -a $HEROKU_APP_NAME webecho RELEASEheroku container:release -a $HEROKU_APP_NAME webdisplayName: 'heroku push'env:HEROKU_API_KEY: $(HEROKU_API_KEY)HEROKU_APP_NAME: $(HEROKU_APP_NAME)
  • At the Review step, add two environment variables: HEROKU_API_KEY, HEROKU_APP_NAME
After that, hit the Save button to complete add a new variable
After that, hit the Save button to complete add a new variable
  • At the Review step, click on Save and run button to save the pipeline and run right after that (Be noted that this pipeline definition will be committed to your repository as well as your code)

That’s it, now back to your Heroku app and reload the page

Your app is now up and running

You now can benefit from CI/CD. Once we push a new commit, the pipeline will kick in immediately. With all steps finished successfully, our Heroku app is up and running. Great!

I hope you have found something interested here.

Thanks for reading.

--

--

Ngo Thien Sinh

Azure Cloud Architect, Software Engineer, DevOps Engineer. Love to automate things, involve to the biz and contribute to build a successful product.