Update update-index.yml #310
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
name: Perbarui HTML dengan Folder | |
on: | |
push: | |
paths: | |
- '**/*' # Memicu workflow saat ada perubahan di folder manapun | |
jobs: | |
update-html: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repositori | |
uses: actions/checkout@v2 | |
- name: Ambil semua folder | |
id: get_folders | |
run: | | |
# Mendapatkan semua folder kecuali .github dan node_modules | |
FOLDERS=$(ls -d */ | grep -v '\.github\|node_modules' | sed 's/\/$//') | |
echo "folders=$(echo "$FOLDERS" | tr '\n' ' ')" >> $GITHUB_ENV | |
- name: Cek folder saat ini di index.html | |
id: read_html | |
run: | | |
# Ambil semua folder yang sudah ada di index.html secara case-insensitive | |
if grep -q '<a href="' index.html; then | |
CURRENT_FOLDERS=$(grep -ioP '(?<=<a href=\").*?(?=\/\")' index.html | tr '[:upper:]' '[:lower:]' | sort -u) | |
else | |
CURRENT_FOLDERS="" | |
fi | |
echo "current_folders=$(echo "$CURRENT_FOLDERS" | tr '\n' ' ')" >> $GITHUB_ENV | |
- name: Reset dan sinkronkan folder di index.html | |
run: | | |
# Jika tidak ada folder dalam repositori, keluar | |
if [ -z "${{ env.folders }}" ]; then | |
echo "Tidak ada folder yang ditemukan di repositori." | |
exit 0 | |
fi | |
# Baca template dari template.html | |
TEMPLATE_FILE="template.html" | |
OUTPUT_FILE="index.html" | |
# Hapus isi file index.html | |
> $OUTPUT_FILE | |
# Salin konten template ke file index.html | |
cp $TEMPLATE_FILE $OUTPUT_FILE | |
# Generate daftar folder untuk HTML | |
UPDATED_FOLDER_LIST="" | |
# Tambahkan folder yang ada ke list | |
for folder in ${{ env.folders }}; do | |
folder_clean=$(echo "$folder" | tr '[:upper:]' '[:lower:]' | sed 's/^[ \t]*//;s/[ \t]*$//') # Normalisasi nama folder | |
UPDATED_FOLDER_LIST="$UPDATED_FOLDER_LIST<li><a href=\"$folder_clean/\">$folder</a></li>\n" | |
done | |
# Gantikan placeholder dengan daftar folder yang diperbarui di index.html | |
sed -i.bak "s|<!-- FOLDER_LIST_PLACEHOLDER -->|$UPDATED_FOLDER_LIST|" $OUTPUT_FILE | |
# Cek jika tidak ada perubahan pada index.html | |
if git diff --quiet; then | |
echo "Tidak ada perubahan pada index.html. Melewatkan commit." | |
exit 0 | |
fi | |
# Commit dan push perubahan | |
git config --global user.name "github-actions" | |
git config --global user.email "github-actions@github.com" | |
git add $OUTPUT_FILE | |
git commit -m "Reset dan sinkronkan folder di index.html dengan repositori" | |
git push |