From 23d7ba335b8969606118a2d99d24eca19d9aac04 Mon Sep 17 00:00:00 2001 From: Michael Thamm Date: Thu, 5 Oct 2023 12:02:18 -0400 Subject: [PATCH] Feat: Prepare testing workflow --- .github/workflows/heroku-deploy.yml | 29 +++++++++++++++++++++++------ app/tests/budget.json | 11 +++++++++++ app/tests/test-urls.sh | 23 +++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/tests/budget.json create mode 100644 app/tests/test-urls.sh diff --git a/.github/workflows/heroku-deploy.yml b/.github/workflows/heroku-deploy.yml index a187ebc..36cc59e 100644 --- a/.github/workflows/heroku-deploy.yml +++ b/.github/workflows/heroku-deploy.yml @@ -2,19 +2,14 @@ name: Deploy Website on: push: -# branches: [ "main" ] + branches: [ "main" ] tags: - 'v*' -# -# pull_request: -# branches: [ "main" ] jobs: build: - runs-on: ubuntu-latest - steps: - uses: actions/checkout@v3 - name: Docker login @@ -31,6 +26,15 @@ jobs: docker login -u $DOCKER_USER -p $DOCKER_PSWD docker build . -t michaelthamm/website:$GITHUB_REF_NAME docker push michaelthamm/website:$GITHUB_REF_NAME + + test-local: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Test index.html links + run: | + . ./app/tests/test-urls.sh + deploy: runs-on: ubuntu-latest steps: @@ -40,3 +44,16 @@ jobs: heroku_api_key: ${{secrets.HEROKU_API_KEY}} heroku_email: ${{secrets.HEROKU_EMAIL}} heroku_app_name: "michaelthamm-website" + + test-deployed: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Audit URLs using Lighthouse + uses: treosh/lighthouse-ci-action@v10 + with: + urls: | + https://www.michaelthamm.com/ + budgetPath: ./app/tests/budget.json # test performance budgets + uploadArtifacts: true # save results as an action artifacts + temporaryPublicStorage: true # upload lighthouse report to the temporary storage \ No newline at end of file diff --git a/app/tests/budget.json b/app/tests/budget.json new file mode 100644 index 0000000..5047fdf --- /dev/null +++ b/app/tests/budget.json @@ -0,0 +1,11 @@ +[ + { + "path": "/*", + "resourceSizes": [ + { + "resourceType": "total", + "budget": 200 + } + ] + } +] diff --git a/app/tests/test-urls.sh b/app/tests/test-urls.sh new file mode 100644 index 0000000..bc7af59 --- /dev/null +++ b/app/tests/test-urls.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Parse index.html and extract all href attributes starting with https:// +links=$(grep -o 'href="https://[^"]*' ./app/templates/index.html | sed 's/href="//') + +# Function to check if a given status code is acceptable +is_acceptable_status() { + case "$1" in + 200|403|999) return 0 ;; + *) return 1 ;; + esac +} + +# Loop through each https:// link and check existence with curl +for link in $links; do + response=$(curl -sL -w "%{http_code}" "$link" -o /dev/null) + if is_acceptable_status "$response"; then + echo "Link $link exists (HTTP $response)" + else + echo "Link $link does not exist or has an unacceptable status code (HTTP $response)" + exit 1 + fi +done