diff --git a/action.yml b/action.yml index 8ac668b..bcd3daf 100644 --- a/action.yml +++ b/action.yml @@ -144,6 +144,10 @@ runs: run: echo "${{ inputs.config }}" >> hyde.yml shell: bash + - name: Check site URL configuration + run: bash check-site-url.sh + shell: bash + # TODO: Add pre-build hook to run some bash commands? - name: Build the site diff --git a/docs/index.md b/docs/index.md index a15cd84..96b8be7 100644 --- a/docs/index.md +++ b/docs/index.md @@ -254,3 +254,11 @@ Sets the `SITE_URL` environment variable > Deprecated: Use the `env` input instead with `TORCHLIGHT_TOKEN=value` Sets the `TORCHLIGHT_TOKEN` environment variable + +### Site URLs + +It's highly recommended to set the site URL for your project so that sitemaps and meta tags are generated correctly. + +You can do this by setting the `SITE_URL` environment variable, or by adding the `url` key to your `hyde.yml` file or `config/hyde.php` file. + +If the site URL is not set, the action will output a warning and suggest a solution. An easy way to set the site URL is to use the `env` input as seen above. diff --git a/src/check-site-url.sh b/src/check-site-url.sh new file mode 100755 index 0000000..a9c3a3d --- /dev/null +++ b/src/check-site-url.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Function to check if SITE_URL is effectively set +check_site_url() { + # Check if SITE_URL is set in .env file + if grep -q "^SITE_URL=" .env; then + return 0 + fi + + # Check for hyde.yml or hyde.yaml with any url setting + if [ -f "hyde.yml" ] && grep -q "^[[:space:]]*url:" hyde.yml; then + return 0 + fi + + if [ -f "hyde.yaml" ] && grep -q "^[[:space:]]*url:" hyde.yaml; then + return 0 + fi + + # If config/hyde.php exists, check if it has a non-localhost URL + if [ -f "config/hyde.php" ]; then + # Look for url configuration line and exclude localhost + if grep -q "'url' => env('SITE_URL'," config/hyde.php; then + line_number=$(grep -n "'url' => env('SITE_URL'," config/hyde.php | cut -d: -f1) + if ! grep -q "'url' => env('SITE_URL', 'http://localhost')," config/hyde.php; then + return 0 + fi + echo "::warning file=config/hyde.php,line=$line_number,title=Missing Site URL::The site URL is set to localhost in your configuration file. Consider setting a production URL here or in using the 'env' input with 'SITE_URL=https://example.com'" + return 0 + fi + fi + + echo "::warning title=Missing Site URL::No SITE_URL environment variable found. It's recommended to set a production URL for your site. You can set it using the 'env' input with 'SITE_URL=https://example.com'" + return 0 +} + +# Show warning if SITE_URL is not properly set +check_site_url diff --git a/tests/unit/test-check-site-url.sh b/tests/unit/test-check-site-url.sh new file mode 100755 index 0000000..85ecc4e --- /dev/null +++ b/tests/unit/test-check-site-url.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +test_site_url_warnings() { + base_dir="$(pwd)" + temp_dir=$(mktemp -d) + cd "$temp_dir" + + # Test case 1: No SITE_URL set anywhere + touch .env + output=$(bash "$base_dir/src/check-site-url.sh") + expected="::warning title=Missing Site URL::No SITE_URL environment variable found. It's recommended to set a production URL for your site. You can set it using the 'env' input with 'SITE_URL=https://example.com'" + + if [[ "$output" != "$expected" ]]; then + echo "Test 1 failed: Warning not shown when SITE_URL is missing" + echo "Expected: $expected" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + # Test case 2: SITE_URL is set in .env + echo "SITE_URL=https://example.com" > .env + output=$(bash "$base_dir/src/check-site-url.sh") + + if [[ -n "$output" ]]; then + echo "Test 2 failed: Warning shown when SITE_URL is set" + echo "Expected empty output" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + # Test case 3: SITE_URL set in config with non-localhost value + rm .env + mkdir -p config + echo "'url' => env('SITE_URL', 'https://example.com')," > config/hyde.php + output=$(bash "$base_dir/src/check-site-url.sh") + + if [[ -n "$output" ]]; then + echo "Test 3 failed: Warning shown when SITE_URL is set in config" + echo "Expected empty output" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + # Test case 4: SITE_URL in config but with localhost + echo "'url' => env('SITE_URL', 'http://localhost')," > config/hyde.php + output=$(bash "$base_dir/src/check-site-url.sh") + expected="::warning file=config/hyde.php,line=1,title=Missing Site URL::The site URL is set to localhost in your configuration file. Consider setting a production URL here or in using the 'env' input with 'SITE_URL=https://example.com'" + + if [[ "$output" != "$expected" ]]; then + echo "Test 4 failed: Warning not shown when SITE_URL is localhost" + echo "Expected: $expected" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + # Test case 5: URL set in hyde.yml + rm config/hyde.php + echo "url: http://localhost" > hyde.yml + output=$(bash "$base_dir/src/check-site-url.sh") + + if [[ -n "$output" ]]; then + echo "Test 5 failed: Warning shown when URL is set in hyde.yml" + echo "Expected empty output" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + # Test case 6: URL set in hyde.yaml with indentation + rm hyde.yml + echo " url: example.com" > hyde.yaml + output=$(bash "$base_dir/src/check-site-url.sh") + + if [[ -n "$output" ]]; then + echo "Test 6 failed: Warning shown when URL is set in hyde.yaml" + echo "Expected empty output" + echo "Got: $output" + cd "$base_dir" + rm -rf "$temp_dir" + exit 1 + fi + + cd "$base_dir" + rm -rf "$temp_dir" +} + +# Run the tests +test_site_url_warnings