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:
- A GitHub account
- An Azure account with an active subscription
- 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:
- Sign in to the Azure portal (https://portal.azure.com).
- Click on “Create a resource” in the upper left corner.
- Search for “App Service” and select “App Service” from the results.
- Click on “Create” to start the creation process.
- Fill in the required information, such as the app name, subscription, resource group, and runtime stack (.NET Core).
- Configure additional settings as needed and click on “Review + Create.”
- 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:
- Open the Azure portal and navigate to your App Service.
- In the left sidebar, click on “Deployment Center.”
- Select the “GitHub” option as the source control.
- Authenticate with your GitHub account and select the repository that contains your .NET Core 7 Web API project.
- Choose the branch you want to deploy (e.g.,
main
). - Under “Deployment credentials,” click on “Change” to set a username and password for deployment.
- 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:
- Checks out the repository code.
- Sets up the .NET Core SDK.
- Builds and publishes the .NET Core 7 Web API project in the
Release
configuration. - 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:
- Open your GitHub repository in a browser.
- Navigate to the “Settings” tab.
- Click on “Secrets” in the left sidebar.
- Click on “New repository secret.”
- Enter
AZURE_WEBAPP_PUBLISH_PROFILE
in the “Name” field. - Open the publish profile XML file (available in the Azure portal under “Deployment Center”) and copy its contents.
- Paste the contents into the “Value” field of the GitHub secret.
- 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!