Deploy .Net Core app to Azure App Service via Github Actions

Setting up a GitHub Action to deploy a .Net Core web app to Azure App Service from your GitHub repository involves a couple steps. This blog post shows a detailed guide to help you set up a CI/CD pipeline using GitHub Actions.

Setting up a GitHub Action to deploy a .Net Core web app to Azure App Service from your GitHub repository involves a couple steps. This blog post shows a detailed guide to help you set up a CI/CD pipeline using GitHub Actions.

Step 1: Create a GitHub Workflow File

  1. Navigate to your repository on GitHub.
  2. Create a new directory called .github/workflows in the root of your repository if it doesn't already exist.
  3. Inside this directory, create a new file called azure-deploy.yml or any other descriptive name.

Step 2: Define the Workflow

Add the following content to your azure-deploy.yml file. This configuration sets up a workflow that triggers on pushes to the main branch and deploys to Azure App Service.

yamlname: Deploy to Azure Web App

on:
push:
branches:
- main # Or whatever the branch name from which you want to deploy is

jobs:
build-and-deploy:
runs-on: ubuntu-latest # Specifies the runner environment

steps:
- name: Checkout code
uses: actions/checkout@v2 # Checks out your repository under $GITHUB_WORKSPACE

- name: Set up .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x' # Specify your .NET version here

- name: Build with dotnet
run: dotnet build --configuration Release

- name: Publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp

- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v2
with:
app-name: 'YourAppName' # Replace with your App Name
slot-name: 'production' # Optional: if using deployment slots
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{env.DOTNET_ROOT}}/myapp

Step 3: Configure Azure Credentials in GitHub Secrets

  1. Navigate to the Azure portal and go to your App Service.
  2. Download the Publish Profile:
    • Select your web app.
    • In the left sidebar, click on "Get publish profile" and download the file.
  3. Add the Publish Profile to GitHub Secrets:
    • Go back to your repository on GitHub.
    • Click on Settings > Secrets > Actions.
    • Click New repository secret.
    • Name the secret AZURE_WEBAPP_PUBLISH_PROFILE and paste the content of the publish profile you downloaded.

Step 4: Commit and Push the Workflow

Commit the azure-deploy.yml file and push it to your repository. GitHub Actions will detect this new workflow file and start running the workflow on your next push to the main branch.

Done. Hopefully 😄