DevOps for .NET 6.0. Deploy .NET Web App in Azure App… | by Donovan Brown | October, 2022

Deploy .NET Web App to Azure App Service with GitHub Actions

Unsplash. Photo by Max Duziz on

In this post, I will show you how to deploy a .NET 6 web app to Azure App Service with GitHub Actions using Bicep for infrastructure as code.

To go along you will need:

First, create an application to deploy.

dotnet new webapp --output myapp

Change to folder and add a .gitignore file.

cd myapp
dotnet new gitignore

Create a new folder named basic below myapp Folder to hold bicep files. When I use bicep, I create a module to house the different parts of my infrastructure. i always make one main.bicep which calls other modules and collects any output. Other than this main.bicep I will have a webapp.bicep, put both files in basic folder.

On line 1, I apply this deployment to the subscription. This will allow deployment to create any required resource groups which I do on line 6. ResourceGroup requires two values name And location Both are provided by the parameters defined on lines 3 and 4.

On line 11, I call webapp.bicep file which we will discuss in a moment. On line 12, I set the name and on line 13 I scope it, so everything is created in the supplied resource group. webapp.bicep The file also defines the location parameter given on line 15.

Finally, on line 19, I collect and propagate the web app name as output.

After that, I made webapp.bicep file.

To create a web app in Azure App Service, you first need to create a hosting plan. Because the application is built using .Net 6, it can be run on any platform. For this example, we will deploy on Linux.

Hosting plans are defined along lines 3-13. it is necessary that reserve property is set to true, and kind The property is set to ‘linux’. You can select any sku required for your needs.

Lines 15-27 define the web app and set the .NET version to 6.0 on line 24.

With the code, we can initialize the repository and push it to GitHub before adding it to the GitHub workflow.

git init
git add *
git commit -a -m 'Init'

With the initial repository we can use the GitHub CLI to create a public repo and push our code there.

gh repo create myAppRepo --public --source .
git push --set-upstream origin trunk

We can create a GitHub workflow by pushing the created repository and code. Under the root folder a . create .github a. folder with workflows folder inside. In the end, in workflows create folder a ci.yml file.

workflow file in project

ci.yml The file is below.

There are three tasks in this workflow build, deployDevInfrastructureAnd deployDevApp,

In build job i just run the same command i would run on my dev machine to publish my application for deployment.

After the application is published, I create two artifacts, one for the app and one for the infrastructure.

deployDevInfrastructure The job is far more interesting. This task uses Azure CLI and PowerShell to deploy Bicep files and store the output for use in other tasks.

The outputs from the bicep deployment are collected on line 75. That value is stored with the published profile as output at 83 and 84. The output of this step was defined on lines 46–48.

These values ​​are used in deployDevApp Work on lines 104 and 106.

To use the Azure CLI, we need to create a GitHub secret that stores Azure credentials. You can read up on the Secrets to Set Up in GitHub Action Workflows on GitHub. I used the below command to generate the credentials. make sure and replace {appName}And {subscriptionId} with your values.

az ad sp create-for-rbac --name {appName} --role Contributor --sdk-auth --scopes /subscriptions/{subscriptionId} --output json >> azurecreds.json

This command will create the required credentials and store them in a JSON file that we will use with the GitHub CLI to create the required secrets.

Run this command using the GitHub CLI to create a secret to hold Azure credentials.

Get-Content .\azurecreds.json | gh secret set AZURE_CREDENTIALS

remove now azurecreds.json the file is No Checked in GitHub.

del .\azurecreds.json

After running these commands there is a secret in GitHub so that this pipeline can be executed.

Azure Saves Credentials as GitHub Actions Secret

Lastly, add ci.yml file and push it to GitHub.

git add .\.github\workflows\ci.yml
git commit -m "added pipeline"
git push

Pushing this code to GitHub will start the workflow and successfully deploy your application to Azure.

GitHub Actions Workflow

You can review the workflow’s log or visit the Azure portal to get a link to your new web app.

Thanks for reading. Till next time!

Leave a Comment