Deploy .NET Core & React to Heroku using Github Actions

Tags: dotnet, react, docker, devops
Published: 2022-01-09 23:14:53


## Deploy .NET Core & React to Heroku using Github Actions

![Main image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh3c4sg9hmnkan8dn7f2.png)
 

[Heroku](https://www.heroku.com/) is a cloud Platform-as-a-Service (PaaS) used to build and deploy applications of [different languages](https://www.heroku.com/languages#languages) on the cloud.

.NET Core is not officially supported by Heroku which means we cannot deploy/run our C# code directly on it. Fortunately, Heroku does support Docker containers, so in this tutorial, we are going to explain how to deploy a containerized .NET Core application on Heroku.

We can do this process locally on our machine by using Docker CLI and Heroku CLI but we need to repeat the entire flow every time we do a change even a small one. To avoid this we have to use and implement CI/CD workflow and for doing that we are going to use [Github Actions](https://docs.github.com/en/free-pro-team@latest/actions).

## Adding Dockerfile

I assume you’ve already created the application, if not you can follow this [tutorial](https://www.roundthecode.com/dotnet/add-a-react-app-to-your-asp-net-core-application) on how to create a .NET Core application with React.

After we’ve created our application we can add Dockerfile to it. Our application structure will be like this.

![Application structure](https://cdn-images-1.medium.com/max/2000/1*iZb90rqOqKIZEnavqcxgSw.png)

As you can see we’ve added Dockerfile to our solution and this is how it initially looks like
{% gist https://gist.github.com/BetimShala/92d5886512364fe582a74658d271a627.js %}
As you can see in our solution we have ClientApp folder which contains all files of React application and the current Dockerfile builds only .NET Core solution so we need to modify it in order to build the React app.

In line **1** we’ve declared .NET Core base image but besides that, we also need to add a Node.js base image (line **16**— for building React app) and copy the build folder (line **30**)
{% gist https://gist.github.com/BetimShala/6e59fa56565e5e225c77bc0449548277.js %}
By default .NET Core application runs on port 5000 and 5001, on the other side Heroku provides a *single* port for you to use and expects your app to run on the port Heroku gives, so basically that means our app should listen to only HTTP connections on that port and Heroku will take care of HTTPS part, so we have to replace the line:

    ENTRYPOINT ["dotnet", "NetCoreReactHeroku.dll"]

with this:

    CMD ASPNETCORE_URLS=http://*:$PORT dotnet NetCoreReactHeroku.dll

By using the initial template of Dockerfile we usually face the error **npm: not found** while building the Docker image because NodeJs don’t exist on the Docker image of the SDK, and to get rid of this error we need to prevent the **PublishRunWebpack** task of csproj to be executed. To do that we replace this line:

    <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">

with this one:

    <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish" Condition=" '$(BuildingDocker)' == '' ">

As you can see we’ve added a **Condition** to csproj which accepts the parameter BuildingDocker and this parameter should be on Dockerfile as well as an environment variable (line **8**).

Now we are all set with Dockerizing our solution, and if you want to test/build the solution and see if Dockerfile works simply run this command.

    docker build . -t $YOUR_APP_NAME -f Dockerfile

![Successful Docker image build](https://cdn-images-1.medium.com/max/2000/1*YTT_RAGR6pTzrUC30oomHg.png)

## Create a Heroku App

First of all, you need to have an account on Heroku, and then you go to your dashboard ([dashboard.heroku.com/new-app](http://dashboar.heroku.com/new-app)) to create your application.

![New Heroku App](https://cdn-images-1.medium.com/max/2000/1*DucR2KquxzW0vPJ6P69siw.png)

After we create our Heroku app we need to get an API key which we’ll use later on our Github Actions. We can get this key by going to *[Account Settings](https://dashboard.heroku.com/account).*

![api-key-settings](https://cdn-images-1.medium.com/max/2428/1*56NeoSV9RxN5vR5YtGVFQg.png)

## Setting up Github Actions

After we added Dockerfile and created the Heroku application now we are ready to deploy our image to the Heroku container and release our web application. To make this happens we are going to use Github Actions. Github Actions provides an automated way to trigger custom workflows in response to events on Github (push, pull_request, etc).

Go to the main directory of your application and add .github folder and inside it also create another folder called workflows as shown in the screenshot below.

![Project files](https://cdn-images-1.medium.com/max/2000/1*_n8Q5P6WSo_jVOOM_m1NlA.png)

Now inside workflows folder create workflow file which is ayml file.
{% gist https://gist.github.com/BetimShala/169aeae85c0f36d12f9ff973e4b5c377.js %}
This workflow should have one name (line **1**) and should define the events that are going to be triggered. In our case, we’ve told the workflow to be triggered only when we push to **master** branch. In line **9** you can see the Heroku API key env variable which is used to login into our Heroku registry, and you can notice the keyword secrets which means we should go to our repository secrets and add this variable (see screenshot below).

![Secrets](https://cdn-images-1.medium.com/max/2556/1*lXNCSVV61XSeh13NecToFw.png)

Every workflow is made up of **jobs** (line **12**) and every job is composed of individual steps. In our case, we’ve declared only **build** job which has 3 **steps**

* Cloning the repository using **checkout** action (line **20**) that checks-out our repository under `$GITHUB_WORKSPACE`, so our workflow can access it.

* Building our Docker image by running the command on runner (line **25**).

* Pushing the Docker image to Heroku Container by running a set of Heroku commands (lines **30** and **31**).

Finally, you just have to commit and push those changes and go to the Actions tab under your Github repository where you can see the build process going on. When the build complete successfully you can visit your deployed app on Heroku *(https://{YOUR_APP_NAME}.herokuapp.com)*.

*Note: The complete solution of this tutorial can be found in this [Github repository](https://github.com/BetimShala/NetCoreReactHeroku).*

Thank you for reading. I hope you enjoyed this tutorial and learned something new 😊.

If you liked this article and want to see more you can find me at

[https://www.linkedin.com/in/betimshala](https://www.linkedin.com/in/betimshala/)