DEV Community

Fabrício Marcondes Santos
Fabrício Marcondes Santos

Posted on

Automating Deployments with GitHub Actions and Azure App Service

Introduction

Keeping your applications up-to-date and running efficiently is crucial to your project’s success. But how do you ensure every new code is automatically tested, integrated, and deployed? The answer lies in automating deployments using CI/CD (Continuous Integration/Continuous Deployment). GitHub Actions and Azure App Service form a powerful combination that allows you to automate the deployment process for .NET applications, ensuring your updates are always consistent and secure.

In this post, we’ll show you how to set up CI/CD pipelines using GitHub Actions to automate the deployment of .NET applications to Azure App Service.

What is GitHub Actions?

GitHub Actions is a workflow automation platform that allows you to create and run pipelines directly in your GitHub repositories. You can automate tasks such as testing, builds, and deployments whenever code is updated.

What is CI/CD?

CI/CD (Continuous Integration/Continuous Deployment) is a methodology that allows you to integrate and deliver code continuously. In practice, this means that every code change is automatically tested and deployed, ensuring your application is always up to date in production.

Prerequisites

Before we begin, make sure you have:

  • Azure Account: If you don’t have one yet, you can create one here.
  • GitHub Account: Go to GitHub to create a repository for your code.
  • Azure App Service Created: The application you want to deploy to Azure.
  • ASP.NET Application in GitHub: The .NET application is already in GitHub, and you want to automate its deployment.

Step-by-Step: Automating Deployments with GitHub Actions

Step 1: Creating a Service Principal in Azure

First, we need to configure a Service Principal in Azure, which is a security identity that GitHub Actions will use to authenticate and deploy to Azure App Service.

  1. Open the Azure CLI and run the following command to create a Service Principal:
az ad sp create-for-rbac --name "github-deploy" --role contributor \
  --scopes /subscriptions/{your-subscription-id}/resourceGroups/{your-resource-group} \
  --sdk-auth
Enter fullscreen mode Exit fullscreen mode
  1. Replace {your-subscription-id} with your Azure subscription ID and {your-resource-group} with the name of your resource group.

  2. The command will generate an output with the credentials for the Service Principal in the following format:

{
  "clientId": "YOUR_CLIENT_ID",
  "clientSecret": "YOUR_CLIENT_SECRET",
  "subscriptionId": "YOUR_SUBSCRIPTION_ID",
  "tenantId": "YOUR_TENANT_ID",
  ...
}
Enter fullscreen mode Exit fullscreen mode
  1. Copy this output. We will use it to configure secrets in GitHub.

Step 2: Adding Secrets to GitHub

Now, we will add the Azure credentials as secrets in GitHub.

  1. In GitHub, go to your application’s repository.

  2. Click Settings and then Secrets and Variables.

  3. Add the following secrets:

    • AZURE_CREDENTIALS: Paste the full JSON generated by the Azure CLI.
    • AZURE_WEBAPP_NAME: The name of your application in Azure App Service.

Step 3: Creating the GitHub Actions Workflow File

Next, we will configure the GitHub Actions workflow file to deploy the application whenever new changes are pushed to the repository.

  1. In your GitHub repository, create a new folder called .github/workflows.

  2. Inside this folder, create a YAML file called azure-deploy.yml with the following content:

name: Deploy ASP.NET Core App to Azure

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Setup .NET Core
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: '6.0.x'

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

    - name: Run tests
      run: dotnet test --no-build --verbosity normal

    - name: Publish project
      run: dotnet publish -c Release -o ./publish

    - name: 'Deploy to Azure Web App'
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ secrets.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_CREDENTIALS }}
        package: ./publish
Enter fullscreen mode Exit fullscreen mode
  • Workflow Explanation:
  • on: push: The pipeline will trigger whenever there’s a push to the main branch.
  • Setup .NET Core: Configures the environment to use the .NET 6.0 version.
  • Build project: Builds the application.
  • Run tests: Runs unit tests.
  • Publish project: Publishes the app to a ./publish folder.
  • Deploy to Azure Web App: Deploys the app to Azure using the secrets set up earlier.

Step 4: Testing the Pipeline

Now that the pipeline is set up, make a small change to your code and push it to the main branch. GitHub Actions will be triggered, and you can monitor the deployment process in the Actions tab of your repository.

Benefits of Using GitHub Actions with Azure

Automating the deployment process with GitHub Actions brings several benefits:

  • Full Automation: Ensure that every time new code is pushed to the repository, it is automatically tested and deployed, eliminating manual errors.
  • Deep Integration with Azure: GitHub Actions offers native integrations with Azure services, making setup and deployment easier.
  • Scalability: As your application grows, you can easily adjust the pipeline to include additional tests, build in multiple environments, and more.

Conclusion

Automating your deployments with GitHub Actions and Azure App Service is an efficient way to ensure your application is always up-to-date and in production with minimal manual effort. CI/CD pipelines allow you to focus on development while automation takes care of the rest.

Top comments (0)