diff --git a/.github/workflows/check_external_links.yml b/.github/workflows/check_external_links.yml new file mode 100644 index 000000000000..924e37edbf54 --- /dev/null +++ b/.github/workflows/check_external_links.yml @@ -0,0 +1,66 @@ +name: Check external links + +# Checks every external link in the docs on PRs that change the docs. A broken +# link shows up as a warning and never fails the PR, because an external site +# can break a link that the PR didn't touch. + +on: + pull_request: + paths: + - "docs/**" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + check-external-links: + name: External links (warnings only, not caused by this PR) + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 + with: + python-version: '3.10' + + - name: Install doc-builder + run: | + pip install git+https://github.com/huggingface/doc-builder.git@main + + - name: Check external links + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + status=0 + doc-builder check-links docs/source/en --check-external --format list > link-report.txt || status=$? + cat link-report.txt + + # Turn each broken external link into a warning on its file and line. + broken=0 + while IFS= read -r line; do + file=${line%%:*} + rest=${line#*:} + message=${rest#* - } + message=${message//%/%25} + echo "::warning file=$file,line=${rest%% - *},title=Broken external link::$message" + broken=$((broken + 1)) + done < <(sed '/Could not verify/q' link-report.txt | grep -E '^[^ ]+:[0-9]+ - https?://' || true) + + { + echo '## External link check' + echo '```' + cat link-report.txt + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + + if [ "$status" -ne 0 ] && [ "$broken" -eq 0 ]; then + echo "::error::check-links failed without listing a broken external link. See the log above." + exit 1 + fi