Skip to content

test: add article to test workflows #71

test: add article to test workflows

test: add article to test workflows #71

name: Check links in diffs
on:
pull_request:
branches: [main]
env:
TARGET_REPO_URL: https://github.com/espressif/developer-portal.git
TARGET_BRANCH: main
jobs:
check-links:
runs-on: ubuntu-latest
steps:
- name: Clone repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Set environment variables
id: set-env
run: |
# Add target remote and fetch its branches
git remote add target ${{ env.TARGET_REPO_URL }}
git fetch target ${{ env.TARGET_BRANCH }}
# Extract the base commit ID where the feature branch diverged from main
base_commit=$(git merge-base target/${{ env.TARGET_BRANCH }} ${{ github.event.pull_request.head.ref }})
echo "BASE_COMMIT=$base_commit" | tee -a $GITHUB_ENV
# Extract the head commit ID on the feature branch
head_commit=${{ github.event.pull_request.head.sha }}
echo "HEAD_COMMIT=$head_commit" | tee -a $GITHUB_ENV
- name: Create baseline branch by reverting feature branch changes
run: |
# Create a copy of the feature branch
git checkout -b feature-baseline
# Reset the new branch to the base commit
git reset --hard ${{ env.BASE_COMMIT }}
- name: Dump all links from feature-baseline
uses: lycheeverse/lychee-action@v2.0.2
with:
args: |
--dump
--include-fragments
--exclude-path ./themes/
--exclude-path ./layouts/
.
output: links-baseline.txt
- name: Check out feature branch
run: git checkout ${{ github.head_ref }}
- name: Append links-baseline.txt to .lycheeignore
run: cat links-baseline.txt >> .lycheeignore
- name: Dump names of files altered in PR and append hash sign to find links with anchors
run: |
git diff --name-only --diff-filter=DM ${{ env.BASE_COMMIT }} ${{ env.HEAD_COMMIT }} > altered-files.txt
sed -i 's|$|#|' altered-files.txt
git diff --name-status --diff-filter=R ${{ env.BASE_COMMIT }} ${{ env.HEAD_COMMIT }} | awk '{print $2}' > renamed-files.txt
sed -i 's|$|#|' renamed-files.txt
- name: Print altered-files.txt
run: cat altered-files.txt
- name: Print renamed-files.txt
run: cat renamed-files.txt
- name: In .lycheeignore, remove links with anchors referring to altered files
run: |
while IFS= read -r line; do
sed -i "\|$line|d" .lycheeignore
done < altered-files.txt
- name: In .lycheeignore, remove links with anchors referring to renamed files
run: |
while IFS= read -r line; do
sed -i "\|$line|d" .lycheeignore
done < renamed-files.txt
- name: Print .lycheeignore with unaffected links
run: cat .lycheeignore
- name: Check links
uses: lycheeverse/lychee-action@v2.0.2
with:
args: |
--no-progress
--include-fragments
--exclude-path ./themes/
--exclude-path ./layouts/
.
fail: true # Fail action if broken links are found
- name: Suggestions
if: failure()
run: |
echo -e "\nPlease review the links reported in the Check links step above."
echo -e "If a link is valid but fails due to a CAPTCHA challenge, IP blocking, login requirements, etc.,"
echo -e "consider adding such links to the .lycheeignore file to bypass future checks.\n"
exit 1
- name: Clean up all changes
if: always()
run: |
git reset --hard HEAD
git clean -df