Skip to content

Merge pull request #10 from sctech-tr/redesign #88

Merge pull request #10 from sctech-tr/redesign

Merge pull request #10 from sctech-tr/redesign #88

name: Generate Sitemap
on:
push:
branches:
- main
workflow_dispatch:
jobs:
generate-sitemap:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Get Repository Info
id: repo-info
run: |
# Extract the repository owner and name from the GitHub environment
REPO_OWNER=$(echo "${{ github.repository_owner }}")
REPO_NAME=$(basename -s .git "${{ github.repository }}")
# Default to GitHub Pages URL (User/Organization or Project Pages)
if [ "$REPO_NAME" == "$REPO_OWNER" ]; then
PAGES_URL="https://${REPO_OWNER}.github.io"
else
PAGES_URL="https://${REPO_OWNER}.github.io/${REPO_NAME}"
fi
# Check if a CNAME file exists
if [ -f CNAME ]; then
# Read the CNAME file and use it as the URL
PAGES_URL=$(cat CNAME)
fi
# Clean up the PAGES_URL to exclude .github
PAGES_URL=$(echo "$PAGES_URL" | sed 's|\.github.*||')
echo "PAGES_URL=$PAGES_URL" >> $GITHUB_ENV
- name: Generate Sitemap
run: |
# Get the GitHub Pages URL (or CNAME) from the environment
PAGES_URL=${{ env.PAGES_URL }}
# Create the sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>' > sitemap.xml
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap-image.v1">' >> sitemap.xml
# Find all files excluding .git and format them as XML
find . -type f ! -path '*/.git/*' | while read -r file; do
file_path=$(realpath --relative-to=. "$file")
file_path=$(echo "$file_path" | sed 's|^|'"$PAGES_URL/"'|; s|\.github/||') # Add URL and remove .github
echo " <url>" >> sitemap.xml
echo " <loc>$file_path</loc>" >> sitemap.xml
echo " </url>" >> sitemap.xml
done
echo '</urlset>' >> sitemap.xml
- name: Commit and Push Sitemap
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add sitemap.xml
git commit -m "Update sitemap"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}