Skip to content

Commit

Permalink
start link check from /en/index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnthompson committed Aug 23, 2024
1 parent 4922432 commit 06551a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
45 changes: 21 additions & 24 deletions .github/workflows/link-check.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Link Checker

# Trigger the workflow on pull requests to the main or develop branches
on:
pull_request:
branches:
Expand All @@ -11,55 +12,51 @@ jobs:
runs-on: ubuntu-latest

steps:
# Step 1: Check out the repository code
# Step 1: Check out the repository code using the latest version of actions/checkout
- name: Check out code
uses: actions/checkout@v4

# Step 2: Set up Node.js environment
# Step 2: Set up Node.js environment using actions/setup-node v4.0.3
- name: Set up Node.js
uses: actions/[email protected]
with:
node-version: '20'
node-version: '20' # Specify Node.js 20.x

# Step 3: Install dependencies
# Step 3: Install npm dependencies
- name: Install dependencies
run: npm install

# Step 4: Create a temporary .eleventy-port file
- name: Create a temporary .eleventy-port file
run: echo "8080" > .eleventy-port

# Step 5: Build and serve the site
# Step 5: Build and serve the site on a specific port
- name: Build and serve the site
run: npm run serve-only &
run: npm run serve-only & # Run the Eleventy server in the background

# Step 6: Wait for the server to start
- name: Wait for server to start
run: sleep 10
run: sleep 15 # Increase the wait time to ensure the server is fully up and running

# Step 7: Run the link checker
# Step 7: List site files to verify the build
- name: List site files
run: ls -R _site # Replace with your actual output directory (e.g., _site)

# Step 8: Check if the server is up
- name: Check if server is up
run: |
curl --fail http://localhost:8080/en/index.html || exit 1
continue-on-error: false # Ensure the workflow fails if the server is not up

# Step 9: Run the link checker
- name: Run link checker
id: link-check # Add ID to reference outputs
id: link-check # Add an ID to reference this step's outputs
run: npm run link-check

# Step 8: Upload broken links report regardless of link check result
# Step 10: Upload the broken links report if it exists
- name: Upload broken links report
if: always() # This runs regardless of the result of the previous step
uses: actions/[email protected]
with:
name: broken-links-report
path: broken-links.json

# Step 9: Post a comment on the PR with the broken links details
- name: Post comment with broken links
if: failure() # This runs only if broken links were found
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## :warning: Broken Links Detected
The following broken links were found during the build process:
```json
${{ toJSON(steps.link-check.outputs.brokenLinks) }}
```
31 changes: 20 additions & 11 deletions scripts/link-checker.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
const blc = require('broken-link-checker');
const fs = require('fs');
const path = require('path');

// Read the port from the .eleventy-port file
const port = fs.readFileSync('.eleventy-port', 'utf8');
const siteUrl = `http://localhost:${port}`;
const port = fs.readFileSync('.eleventy-port', 'utf8').trim();
const startUrl = `http://localhost:${port}/en/index.html`;

const brokenLinks = [];
let brokenLinks = [];

// Create the SiteChecker instance
const siteChecker = new blc.SiteChecker(
{
excludeExternalLinks: true, // Exclude external links from checking
filterLevel: 1, // Level of filtering, can adjust based on need
excludeExternalLinks: true, // Only check internal links
filterLevel: 0, // Ensure all links are checked
recurse: true, // Follow links to other pages within the same domain
maxSockets: 10, // Number of concurrent requests
maxRetries: 2, // Retry broken links twice before reporting
},
{
link: (result) => {
if (result.broken) {
// Log broken link details only to the JSON file, not to the terminal
brokenLinks.push({
page: result.base.original,
link: result.url.original,
linkText: result.html.text || 'N/A', // Captures the link text
status: result.http.response && result.http.response.statusCode ? result.http.response.statusCode : 'N/A',
statusText: result.http.response && result.http.response.statusMessage ? result.http.response.statusMessage : 'N/A'
linkText: result.html.text || "N/A",
status: result.status,
statusText: result.statusText
});
}
},
end: () => {
if (brokenLinks.length > 0) {
fs.writeFileSync('./broken-links.json', JSON.stringify(brokenLinks, null, 2));
console.log(`Broken links found! See broken-links.json for details.`);
const outputFilePath = path.join(__dirname, '..', 'broken-links.json');
fs.writeFileSync(outputFilePath, JSON.stringify(brokenLinks, null, 2));
console.log(`Broken links found! See ${outputFilePath} for details.`);
} else {
console.log('No broken links found.');
}
console.log('Site link check complete.');
},
}
);

siteChecker.enqueue(siteUrl);
// Start link checking from /en/index.html
siteChecker.enqueue(startUrl);

0 comments on commit 06551a4

Please sign in to comment.