-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add clean up images job DOC-1232
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
name: Clean Up Unused Images | ||
|
||
on: | ||
schedule: | ||
# On the first of every month at 2 am | ||
- cron: '0 2 1 * *' | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: clean-up-images-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
jobs: | ||
find_unused_images: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- id: checkout | ||
name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Nodejs | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20" | ||
|
||
- name: Find unused images | ||
run: make find-unused-images | ||
|
||
- name: Install GitHub CLI | ||
run: sudo apt-get install gh | ||
|
||
- name: Create PR with unused images | ||
run: | | ||
# Ensure that we are on master. | ||
git checkout master | ||
# Create a new branch. | ||
branch_name="clean-up-unused-images-$(date +%Y%m%d%H%M%S)" | ||
git checkout -b "$branch_name" | ||
# Remove all the images identified as unused. | ||
for img in $(cat unused_images.json); do | ||
rm static/assets/docs/images/$img | ||
done | ||
# Clean up results file. | ||
rm unused_images.json | ||
# Commit and push branch | ||
git add . | ||
git commit -m "docs: clean up unused images" | ||
git push origin $branch_name | ||
# Create the pull request | ||
gh pr create --base master --title "docs: clean up librarium unused images " --body "$(cat <<EOF | ||
## Describe the Change | ||
This PR removes images identified as unused across all our branches. | ||
The images are identified using `scripts/find-unused-images.sh` script. | ||
Please review this PR carefully before merging it. | ||
EOF | ||
)" | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
# Enable error handling | ||
set -e | ||
|
||
echo "Checking for unused image files in static/assets/docs/images folder..." | ||
|
||
# Create a list of all the images we have and save it to a json. | ||
# Trim the path static/assets/docs/images. | ||
find static/assets/docs/images -type f ! -name ".DS_STORE" ! -name ".DS_Store" | sed 's|static/assets/docs/images||g' > all_images.json | ||
|
||
# List all the version branches | ||
branches="master" | ||
version_branches=$(git branch --list "version-[0-9]-[0-9]") | ||
branches+=" $version_branches" | ||
|
||
for current_branch in $branches; do | ||
git checkout $current_branch | ||
echo "Current branch: $current_branch" | ||
git pull | ||
|
||
find docs -type f -name "*.md" -exec grep -Hn "\.webp" {} + > docs_used_images.json | ||
find _partials -type f -name "*.mdx" -exec grep -Hn "\.webp" {} + > partials_used_images.json | ||
cat docs_used_images.json partials_used_images.json > used_images.json | ||
|
||
line_number=1 | ||
for img in $(cat all_images.json); do | ||
if grep -q $img used_images.json; then | ||
sed -i .bak "${line_number}s|.*|${img},FOUND_USED|" all_images.json | ||
fi | ||
((line_number++)) | ||
done | ||
|
||
done | ||
|
||
# Remove all marked used files to make up the list | ||
sed '/FOUND_USED/d' all_images.json > unused_images.json | ||
|
||
# Clean up files | ||
rm all_images.json | ||
rm all_images.json.bak | ||
rm docs_used_images.json | ||
rm partials_used_images.json | ||
rm used_images.json |