First Commit #8
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 Mini Project dengan Folder | |
on: | |
push: | |
paths: | |
- 'mini-project/**' # Memicu workflow saat ada perubahan di folder mini-project | |
jobs: | |
update-html: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repositori | |
uses: actions/checkout@v2 | |
- name: Ambil semua folder di mini-project | |
id: get_folders | |
run: | | |
# Mendapatkan semua folder di dalam mini-project | |
FOLDERS=$(ls -d mini-project/*/ | sed 's/mini-project\///;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="' mini-project/index.html; then | |
CURRENT_FOLDERS=$(grep -ioP '(?<=<a href=\").*?(?=\/\")' mini-project/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="mini-project/template.html" | |
OUTPUT_FILE="mini-project/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 "siegrin" | |
git config --global user.email "piubitt@gmail.com" | |
git add $OUTPUT_FILE | |
git commit -m "Reset dan sinkronkan folder di index.html dengan repositori" | |
git push |