๐Ÿ‘๏ธ Are my webpages up?

Let's check my webpages are up with a github action.

Nil

github

Infrastructure

462 Words Reading Time: 2 Minutes, 6 Seconds

09-25-2024 19:43 +0000


Today, we’re shifting gears a bit to look at how to automate the monitoring of website uptime using GitHub Actions. The idea is simple: we’ll periodically check a list of websites to ensure they are up and running, and notify us if they go down.

Setting Up the Workflow

We’ll be using GitHub Actions to check the status of multiple websites every six hours, during working days (Monday to Friday). If any site is down, the action will fail and notify us. Hereโ€™s how to set it up:

  1. In your repository, create a file at .github/workflows/check-webpages.yml.
  2. Add the following content to your YAML file:
name: Check Webpages Status

on:
  schedule:
    - cron: "0 8-18/6 * * 1-5" # Runs every 6 hours from 8 AM to 6 PM, Monday to Friday
  workflow_dispatch: # Allows manual triggering of the workflow

jobs:
  check_webpages:
    runs-on: ubuntu-latest

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

      - name: Install curl
        run: sudo apt-get install -y curl

      - name: Check if webpages are up
        run: |
          declare -a urls=("https://pops.cafe" "https://checker.pops.cafe")

          for url in "${urls[@]}"
          do
            echo "Checking $url"
            http_status=$(curl -o /dev/null -s -w "%{http_code}" "$url")
            
            if [[ "$http_status" -ne 200 ]]; then
              echo "Error: $url is down (status code $http_status)"
              exit 1
            else
              echo "$url is up (status code $http_status)"
            fi
          done

Breaking Down the Workflow:

  • name: The workflow is called Check Webpages Status.
  • on.schedule: The cron expression 0 8-18/6 * * 1-5 ensures the workflow runs every 6 hours between 8 AM and 6 PM from Monday to Friday.
  • jobs: The job, check_webpages, runs on the latest Ubuntu runner.
  • steps:
    • Checkout code: Fetches your repository’s code.
    • Install curl: Ensures curl is available for making HTTP requests.
    • Check if webpages are up: This step loops through the list of URLs, checking their HTTP status code. If any page is down (i.e., the status code is not 200), the action will fail.

How It Works

Once this workflow is in place, GitHub Actions will automatically run it at the specified times. If a website is down, the action will fail, and youโ€™ll be notified depending on your GitHub notification settings (e.g., via email or in the Actions tab).

Manually Triggering the Workflow

You can also trigger this action manually from the Actions tab in GitHub. This is useful if you want to check the status of your websites outside the scheduled runs.

Conclusion

With just a few lines of code, you can set up an automated uptime monitor for your websites using GitHub Actions. This lightweight approach is perfect for small to medium-sized websites or services where you don’t need complex monitoring tools.

In our next post, we’ll explore how to enhance this setup by sending Slack notifications or integrating with third-party monitoring tools to further automate alerting.

Stay tuned!