Building and Publishing a .NET Core 7 Web API to Azure App Service using GitHub Actions | Step-by-Step Guide

Introduction:

Azure App Service is a powerful platform for hosting web applications. In this blog post, we will guide you through the process of building and publishing a .NET Core 7 Web API to Azure App Service using GitHub Actions. By automating this process, you can streamline your development workflow and ensure efficient deployment. Let’s get started!

Prerequisites:

Before we begin, make sure you have the following:

  1. A GitHub account
  2. An Azure account with an active subscription
  3. A .NET Core 7 Web API project (for this example, we’ll use the repository https://github.com/learnsmartcoding/dotnet-with-docker)

Step 1:

Create an Azure App Service: To host our .NET Core 7 Web API, we need to create an Azure App Service. Follow these steps:

  1. Sign in to the Azure portal (https://portal.azure.com).
  2. Click on “Create a resource” in the upper left corner.
  3. Search for “App Service” and select “App Service” from the results.
  4. Click on “Create” to start the creation process.
  5. Fill in the required information, such as the app name, subscription, resource group, and runtime stack (.NET Core).
  6. Configure additional settings as needed and click on “Review + Create.”
  7. Review the details and click on “Create” to create the App Service.

Step 2:

Configure Azure App Service Deployment Credentials: To enable GitHub Actions to deploy to the Azure App Service, we need to configure deployment credentials. Follow these steps:

  1. Open the Azure portal and navigate to your App Service.
  2. In the left sidebar, click on “Deployment Center.”
  3. Select the “GitHub” option as the source control.
  4. Authenticate with your GitHub account and select the repository that contains your .NET Core 7 Web API project.
  5. Choose the branch you want to deploy (e.g., main).
  6. Under “Deployment credentials,” click on “Change” to set a username and password for deployment.
  7. Provide the desired username and password, and click on “Save.”

Step 3:

Define GitHub Actions Workflow: We will now define a GitHub Actions workflow that builds and publishes the .NET Core 7 Web API to Azure App Service. Create a file named .github/workflows/deploy.yml in your repository and add the following content:

name: Build and deploy ASP.Net Core app to Azure Web App - lsc-shoppingapp-api

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.0.x'
          include-prerelease: true

      - name: Build with dotnet
        run: dotnet build --configuration Release LearnSmartCoding.EssentialProducts.API/LearnSmartCoding.EssentialProducts.API.csproj

      - name: dotnet publish
        run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp LearnSmartCoding.EssentialProducts.API/LearnSmartCoding.EssentialProducts.API.csproj

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v2
        with:
          name: .net-app
          path: ${{env.DOTNET_ROOT}}/myapp

  deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'lsc-shoppingapp-api'
          slot-name: 'Production'
          publish-profile: "${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}"
          package: .

Make sure to replace your-app-service-name with the name of your Azure App Service. This workflow performs the following steps:

  1. Checks out the repository code.
  2. Sets up the .NET Core SDK.
  3. Builds and publishes the .NET Core 7 Web API project in the Release configuration.
  4. Deploys the published artifacts to the Azure App Service.

Step 4:

Configure GitHub Secrets: To securely store sensitive information, we’ll use GitHub Secrets to store the Azure App Service publish profile. Follow these steps:

  1. Open your GitHub repository in a browser.
  2. Navigate to the “Settings” tab.
  3. Click on “Secrets” in the left sidebar.
  4. Click on “New repository secret.”
  5. Enter AZURE_WEBAPP_PUBLISH_PROFILE in the “Name” field.
  6. Open the publish profile XML file (available in the Azure portal under “Deployment Center”) and copy its contents.
  7. Paste the contents into the “Value” field of the GitHub secret.
  8. Click on “Add secret” to save the secret.

Step 5:

Commit and Push the Workflow: Once you have added the workflow file and configured the GitHub secret, commit and push the changes to your GitHub repository. The workflow will automatically trigger for each push to the main branch, building and deploying the .NET Core 7 Web API to your Azure App Service.

Conclusion:

In this blog post, we walked through the process of setting up a CI/CD pipeline using GitHub Actions to build and publish a .NET Core 7 Web API to Azure App Service. By automating this process, you can ensure efficient deployment and streamline your development workflow. Happy coding and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights