GitHub Actions Workshop

Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.


Project maintained by prasadhonrao Hosted on GitHub Pages — Theme by mattgraham

Lab: Deploy ASP.NET Web App to Azure Using Reusable Workflow

Objective

In this lab, you will integrate the reusable workflow for deploying an ASP.NET Web App to Azure into a GitHub Actions pipeline. This will automate the deployment of your web application to Azure after building and publishing it.

Prerequisites:

  1. Reusable Workflow for Azure Deployment: You should have the reusable workflow created in the previous lab that handles deployment to Azure. If not, please refer to the Create Reusable Workflow to create it.
  2. Azure Subscription and Service Principal: You need an Azure subscription and a service principal to authenticate with Azure. The service principal should be stored as a GitHub secret.

Step 1: Set Up the Main Workflow File

  1. Navigate to the .github/workflows directory in your GitHub repository.
  2. Create a new workflow YAML file for your main deployment pipeline. Name the file aspnet-webapp-deploy-to-azure-using-reusable-workflow.yml (or any preferred name).

  3. The starter code for the main workflow is provided in Starter Template. Copy the code and paste it into the newly created workflow file.

  4. Now we will modify the workflow to use the reusable workflow for deploying the ASP.NET Web App to Azure. Update the workflow file with the following code, put that under ‘deploy’ job:
needs: build
uses: ./.github/workflows/azure-webapp-deploy-reusable-workflow.yml
with:
  AZURE_WEBAPP_PACKAGE_PATH: webapp
  AZURE_WEBAPP_NAME: $
secrets:
  AZURE_SERVICE_PRINCIPAL: $

Explanation


Step 2: Configure Secrets in GitHub Repository

For the deployment process to authenticate with Azure, you need to add the Azure Service Principal as a secret in your GitHub repository.

  1. Create an Azure Service Principal:
    • Follow the official guide to create a Service Principal in Azure. You will need the following details:
      • App ID (Client ID)
      • Tenant ID
      • Client Secret
  2. Add the Service Principal to GitHub Secrets:
    • Go to your repository in GitHub.
    • Navigate to Settings > Secrets and variables > Actions.
    • Click New repository secret and name it AZURE_SERVICE_PRINCIPAL.
    • Paste the Service Principal information in the following format:
      {
        "clientId": "<App-ID>",
        "clientSecret": "<Secret>",
        "tenantId": "<Tenant-ID>"
      }
      

Step 3: Triggering the Workflow

Once the workflow is set up, you can trigger the deployment in two ways:

  1. Push to GitHub:

    • The workflow will automatically run whenever changes are pushed to the src/dotnet/WebApp/** path or the workflow files (.github/workflows/aspnet-webapp-deploy.yml).
  2. Manually Trigger the Workflow:

    • You can also trigger the workflow manually by going to the Actions tab in GitHub and selecting the ASP.NET Web App Deploy to Azure workflow. Click Run Workflow and select the branch to run the workflow.

Step 4: Monitor the Workflow Execution

  1. Go to the Actions tab in your GitHub repository.
  2. Monitor the workflow execution:
    • You should see the workflow running, with logs from the build and deploy jobs.
    • If everything is set up correctly, your ASP.NET Web App will be built, published, and deployed to Azure.

Step 5: Conclusion

In this lab, you successfully created a GitHub Actions workflow that builds an ASP.NET Web App, then deploys it to Azure using a reusable workflow. By breaking the deployment logic into a reusable workflow, you can easily reuse it across multiple projects, streamlining your CI/CD pipeline.

Next Steps


This concludes the lab on deploying an ASP.NET Web App to Azure using a reusable workflow.