Regularly scheduled check links #16
This file contains hidden or 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
| # This is called after the deployed environment is ready to be checked | |
| name: Regularly scheduled check links | |
| on: | |
| schedule: | |
| - cron: "0 2 * * 1,4" | |
| workflow_dispatch: | |
| inputs: | |
| url: | |
| description: 'Site to check for broken links' | |
| type: string | |
| default: '' | |
| jobs: | |
| determine-urls: | |
| name: determine which urls to use | |
| runs-on: ubuntu-latest | |
| outputs: | |
| urls: ${{ steps.determine-url.outputs.urls }} | |
| steps: | |
| - id: determine-url | |
| run: | | |
| # default URLs we want to check | |
| # todo: replace this with a vars.VAL ? | |
| urls="[\"https://fixed.docs.upsun.com\",\"https://docs.upsun.com\"]" | |
| if [ "workflow_dispatch" = "${{ github.event_name }}" ]; then | |
| # make sure there's no extra spaces in what we were given | |
| inputURL=$(echo "${{ inputs.url }}" | xargs) | |
| if [ -n "${inputURL}" ]; then | |
| urls="[\"${inputURL}\"]" | |
| fi | |
| fi | |
| echo "urls=${urls}" >> $GITHUB_OUTPUT | |
| check-links: | |
| name: Check links | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| url: ${{ fromJSON(needs.determine-urls.outputs.urls) }} | |
| needs: | |
| - determine-urls | |
| steps: | |
| - uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| - name: install linkchecker | |
| id: install-linkchecker | |
| run: | | |
| pip3 install linkchecker | |
| - name: checkout xml to markdown | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: platformsh/linkchecker-xml2md | |
| - name: checkout linkchecker config | |
| uses: actions/checkout@v4 | |
| with: | |
| sparse-checkout: | | |
| linkcheckerrc | |
| sparse-checkout-cone-mode: false | |
| path: linkchecker | |
| - name: install-parser | |
| run: | | |
| pip3 install -r requirements.txt | |
| - name: Run linkchecker | |
| id: run-link-checker | |
| continue-on-error: true | |
| shell: bash | |
| run: | | |
| echo "::notice::Scanning ${{ matrix.url }} for broken links." | |
| # if linkchecker exits with a non-zero then it means broken links were found. | |
| scan=$(linkchecker ${{ matrix.url }} -f ./linkchecker/linkcheckerrc -F xml/utf_8/brokenlinks.xml ) | |
| result=$? | |
| if [[ $result -ne 0 ]]; then | |
| echo "::notice::Broken links detected." | |
| fi | |
| - name: Convert xml to markdown | |
| if: ${{ steps.run-link-checker.outcome == 'failure' }} | |
| run: | | |
| ./xml2md.py brokenlinks.xml > broken_links.md | |
| - name: Report no broken links | |
| if: ${{ steps.run-link-checker.outcome == 'success' }} | |
| run: | | |
| echo "::notice::No broken links were detected" | |
| - name: get domain name | |
| if: ${{ steps.run-link-checker.outcome == 'failure' }} | |
| id: get-domain-name | |
| run: | | |
| domainName=$(basename ${{ matrix.url }}) | |
| echo "domain=${domainName}" >> $GITHUB_OUTPUT | |
| - name: check message size | |
| id: check-msg-size | |
| if: ${{ steps.run-link-checker.outcome == 'failure' }} | |
| run: | | |
| artifact="false" | |
| sizeOfMessage=$(cat broken_links.md | wc -c) | |
| echo "::notice::Size of report: ${sizeOfMessage}" | |
| # the size of a comment in an issue is 65536 | |
| if [ "${sizeOfMessage}" -gt 65000 ]; then | |
| # we'll need to store the broken_links.md file as an aritifact | |
| artifact="true" | |
| fi | |
| echo "artifact=${artifact}" >> $GITHUB_ENV | |
| - name: Upload artifact if applicable | |
| id: upload-artifact | |
| if: ${{ steps.run-link-checker.outcome == 'failure' && env.artifact == 'true' }} | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: broken-links-${{ steps.get-domain-name.outputs.domain }} | |
| path: | | |
| ./broken_links.md | |
| - name: Create issue | |
| id: create-issue | |
| if: ${{ steps.run-link-checker.outcome == 'failure' }} | |
| env: | |
| ARTIFACT: ${{ env.artifact }} | |
| DOMAIN: ${{ steps.get-domain-name.outputs.domain }} | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { ARTIFACT, DOMAIN } = process.env | |
| var fs = require('fs'); | |
| // | |
| failMsg = ':bug: Broken links detected on ' + DOMAIN | |
| if ('true' == ARTIFACT) { | |
| address = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}#artifacts` | |
| var bodyMsg = `Broken links found. Please view the report artifact at ${address}` | |
| } else { | |
| var bodyMsg = fs.readFileSync('broken_links.md','utf8'); | |
| } | |
| github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: failMsg, | |
| body: bodyMsg, | |
| labels: ['broken-links'], | |
| }) |