Check Broken Links #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Broken Links | |
on: | |
schedule: | |
- cron: "0 2 * * 0" # Runs every Sunday at 2 AM | |
workflow_dispatch: | |
jobs: | |
linkcheck: | |
runs-on: ubuntu-latest | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE_LABEL: "Content" | |
ISSUE_TITLE: "[CONTENT] Broken Links Found" | |
WORKFLOW_FILE_PATH: ".github/workflows/check-broken-links.yml" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Check links with linkcheck | |
uses: filiph/[email protected] | |
with: | |
arguments: "https://www.yourwebsite.com --external --skip-file linkcheck-skip-file.txt > linkcheck-results.txt" | |
id: linkcheck | |
- name: Upload linkcheck-results.txt as artifact | |
if: ${{ failure() }} | |
uses: actions/upload-artifact@v3 | |
with: | |
name: linkcheck-results | |
path: linkcheck-results.txt | |
- name: Fetch existing issues | |
if: ${{ failure() }} | |
run: gh issue list --label "${ISSUE_LABEL}" --search "${ISSUE_TITLE}" --json number -q ".[].number" > issue_numbers.txt | |
- name: Close existing issues | |
if: ${{ failure() }} | |
run: | | |
while IFS= read -r ISSUE; do | |
echo "Attempting to close issue #$ISSUE" | |
gh issue close $ISSUE --repo ${{ github.repository }} | |
done < issue_numbers.txt | |
- name: Create new issue | |
if: ${{ failure() }} | |
id: create_issue | |
uses: dacbd/create-issue-action@main | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
title: ${{ env.ISSUE_TITLE }} | |
body: | | |
**Describe the issue** | |
During our scheduled (or manually-triggered) link check, the workflow [Check Broken Links](https://github.com/${{ github.repository }}/${{ env.WORKFLOW_FILE_PATH }}) identified one or more broken links in the repository. | |
Details of the broken links, including their locations and the reasons for failure, can be found in the `linkcheck-results.txt` artifact, at the end of the [failed run page here](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}). | |
🔗 **Workflow name** - `${{ github.workflow }}` | |
📇 **Job** - `${{ github.job }}` | |
🚦 **Status** - `${{ job.status }}` | |
labels: ${{ env.ISSUE_LABEL }} | |
assignees: your-github-username | |
- name: Update closed issues with new issue link | |
if: ${{ failure() }} | |
run: | | |
NEW_ISSUE_LINK="${{ steps.create_issue.outputs.html_url }}" | |
while IFS= read -r ISSUE; do | |
gh issue comment $ISSUE --body "Closed in favor of $NEW_ISSUE_LINK" | |
done < issue_numbers.txt |