Skip to content

Commit

Permalink
Add GitHub Action for automated link checking on PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnthompson committed Aug 23, 2024
1 parent ece2001 commit 6f7beae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/link-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Link Checker

# Trigger the workflow on pull requests to the main or develop branches
on:
pull_request:
branches:
- main
- develop

jobs:
link-check:
runs-on: ubuntu-latest

steps:
# Step 1: Check out the repository code
- name: Check out code
uses: actions/checkout@v2

# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20' # Specify Node.js 20.x

# Step 3: Install 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 on the specified port
- name: Build and serve the site
run: npm run serve-only &

# Step 6: Wait for the server to start
- name: Wait for server to start
run: sleep 10 # Adjust as needed based on server start time

# Step 7: Run the link checker
- name: Run link checker
run: npm run link-check

# Step 8: Upload broken links report if broken links are found
- name: Upload broken links report
if: failure()
uses: actions/upload-artifact@v2
with:
name: broken-links-report
path: broken-links.json
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"newPage": "node scripts/create-new-page.js",
"link-check": "node scripts/link-checker.js",
"serve:port": "node scripts/serve-with-port.js",
"serve-only": "npx @11ty/eleventy --serve --port=8080 --quiet --formats=html,css,js",
"test": "echo \"Error: no test specified\" && exit 1",
"spellcheck": "cspell --no-progress -u \"src/en/**/*.html\" \"src/en/**/*.md\" \"src/fr/**/*.html\" \"src/fr/**/*.md\"",
"eleventy": "npx eleventy",
Expand Down
5 changes: 5 additions & 0 deletions scripts/serve-with-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ portfinder.basePort = 8080; // Start looking for an available port from 8080
portfinder.getPortPromise().then((port) => {
// Save the found port to a temporary file
fs.writeFileSync('.eleventy-port', port.toString());
console.log(`Port ${port} written to .eleventy-port`);

// Serve the Eleventy site on the found port
const serveProcess = exec(`npx @11ty/eleventy --serve --port=${port}`);
Expand All @@ -18,6 +19,10 @@ portfinder.getPortPromise().then((port) => {
serveProcess.stderr.on('data', (data) => {
console.error(data);
});

serveProcess.on('exit', (code) => {
console.log(`Eleventy server exited with code ${code}`);
});
}).catch((err) => {
console.error('Error finding a port:', err);
});

0 comments on commit 6f7beae

Please sign in to comment.