In the world of continuous integration and deployment (CI/CD), it's crucial to have efficient monitoring and troubleshooting procedures. One aspect that often needs attention is validating the health of a deployed web application. If you are utilizing Microsoft Azure as your cloud service provider, specifically Azure Web App service, this blog post will guide you through a simple yet effective way to verify your application's health. The focus is on creating a YAML task to check the HTTP status code of your Azure Web App.

Setting Up the Azure Web App

Azure Web Apps are part of the Azure App Services suite and provide an excellent platform for hosting web applications. They offer the ability to scale on demand, automate deployments with CI/CD through Azure Pipelines, and much more. Once you've set up and deployed your web app on Azure, the next step is to verify that the application is running smoothly.

Creating the YAML Task

Azure Pipelines is a fantastic tool provided by Azure DevOps to implement your CI/CD workflows. You can define your pipelines in YAML (Yet Another Markup Language) format, which offers versioning, history tracking, and reusable code.

In our scenario, we will use a simple YAML task to make a GET request to your Azure Web App and check the returned HTTP status code. If the HTTP status code is 200, it's a sign that the web app is running fine.

Here is the YAML task in Azure Pipelines to verify the status of your web application:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

jobs:
- job: CheckWebAppStatus
  displayName: 'Check Web App Status'
  variables:
    webAppUrl: 'https://.azurewebsites.net'  # replace with your web app URL
  steps:
  - script: |
      STATUS=$(curl -s -o /dev/null -w "%{http_code}" $(webAppUrl))
      if [ $STATUS -eq 200 ]; then
        echo "Web app is running fine, returned status 200"
      else
        echo "Web app is not running fine, returned status $STATUS"
        exit 1
      fi
    displayName: 'Check Web App Status'

This YAML task uses the 'curl' command to send an HTTP GET request to your web app and retrieves the HTTP status code. If the status code is 200, it echoes a message indicating the web app's healthy status. If not, it flags an error message and exits with a failure.

Conclusion

Regular health checks on your Azure Web App are a key component of maintaining a robust CI/CD pipeline. With the YAML task demonstrated in this blog post, you can automate this check and keep your web app running smoothly. Remember to replace your-web-app in the YAML script with the actual URL of your Azure Web App. Happy coding with Azure Pipelines and YAML!