3
0
mirror of https://github.com/NekoMonci12/mine-compress.git synced 2025-12-19 15:09:20 +00:00
Files
mine-compress/.github/workflows/optimize.yml
2025-07-08 20:32:00 +07:00

201 lines
6.7 KiB
YAML

name: Optimize Resource Pack
on:
issues:
types: [labeled]
jobs:
process_resource_pack:
if: github.event.label.name == 'optimize-request'
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Extract zip link from issue
id: extract
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
ISSUE_NUMBER="${{ github.event.issue.number }}"
REPO="${{ github.repository }}"
RAW_URL=$(echo "${{ github.event.issue.body }}" | grep -oP 'https?://[^\s)]*\.zip[^\s)]*' | head -n 1)
if [[ -z "$RAW_URL" ]]; then
echo "No .zip URL found in issue body."
echo "status=failed" >> "$GITHUB_OUTPUT"
echo "❌ Adding invalid-url label due to missing .zip URL"
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" \
--add-label invalid-url \
--remove-label optimize-request
exit 1
fi
# Fix Dropbox links by ensuring dl=1
if [[ "$RAW_URL" =~ dropbox\.com ]]; then
ZIP_URL=$(echo "$RAW_URL" | sed -E 's/[?&]dl=[01]//g')
if [[ "$ZIP_URL" == *\?* ]]; then
ZIP_URL="${ZIP_URL}&dl=1"
else
ZIP_URL="${ZIP_URL}?dl=1"
fi
else
ZIP_URL="$RAW_URL"
fi
# Check if URL is downloadable
if [[ "$ZIP_URL" =~ dropbox\.com ]]; then
# Dropbox requires full download to verify
if ! curl -s --fail --output /dev/null "$ZIP_URL"; then
echo "❌ Dropbox URL not reachable: $ZIP_URL"
echo "status=failed" >> "$GITHUB_OUTPUT"
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body="❌ Invalid Dropbox URL.\n\nEnsure the link is public and includes \`dl=1\`."
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" \
--add-label invalid-url \
--remove-label optimize-request
gh issue close "$ISSUE_NUMBER" --repo "$REPO"
exit 1
fi
else
# Regular .zip URL check via HTTP headers
if ! curl -sI --fail "$ZIP_URL" | grep -q "200 OK"; then
echo "❌ URL not responding with 200 OK: $ZIP_URL"
echo "status=failed" >> "$GITHUB_OUTPUT"
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body="❌ Invalid or unreachable .zip URL provided.\n\nMake sure it is public and ends in \`.zip\`."
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" \
--add-label invalid-url \
--remove-label optimize-request
gh issue close "$ISSUE_NUMBER" --repo "$REPO"
exit 1
fi
fi
echo "zip_url=$ZIP_URL" >> "$GITHUB_OUTPUT"
echo "status=success" >> "$GITHUB_OUTPUT"
- name: Download and unzip resource pack
run: |
mkdir input
cd input
echo "⬇️ Downloading zip file..."
curl -sL "${{ steps.extract.outputs.zip_url }}" -o pack.zip
echo "📦 Unpacking zip..."
unzip -q pack.zip -d unpacked
echo "✅ Resource pack extracted."
- name: Install dependencies
run: |
sudo apt-get update -qq > /dev/null
sudo apt-get install -y -qq optipng ffmpeg zip > /dev/null
echo "🔍 Verifying installation..."
for cmd in optipng ffmpeg zip; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "❌ $cmd not found after installation."
exit 1
else
echo "✅ $cmd is installed: $($cmd --version | head -n 1)"
fi
done
- name: Optimize PNG files
shell: bash
run: |
set +e # disable exit-on-error
set +o pipefail # ignore broken pipes from printf
echo "🖼️ Scanning PNG files..."
mapfile -t FILES < <(find input/unpacked -type f -iname '*.png')
TOTAL=${#FILES[@]}
echo "🖼️ Optimizing $TOTAL PNG files..."
i=0
for file in "${FILES[@]}"; do
((i++))
# Output progress quietly, avoid broken pipe crashes
{ printf "\r[%4d/%4d] %s" "$i" "$TOTAL" "$(basename "$file")"; } || true
optipng -o7 -fix "$file" > /dev/null 2>&1 || true
done
echo -e "\n✅ PNG optimization complete."
- name: Compress OGG files
shell: bash
run: |
set +e
set +o pipefail
echo "🔊 Scanning OGG files..."
mapfile -t FILES < <(find input/unpacked -type f -iname '*.ogg')
TOTAL=${#FILES[@]}
echo "🔊 Compressing $TOTAL OGG files..."
i=0
for file in "${FILES[@]}"; do
((i++))
{ printf "\r[%4d/%4d] %s" "$i" "$TOTAL" "$(basename "$file")"; } || true
ffmpeg -v error -y -i "$file" -c:a libvorbis -qscale:a 3 "${file}.tmp.ogg" > /dev/null 2>&1 || true
if [[ -f "${file}.tmp.ogg" ]]; then
mv -f "${file}.tmp.ogg" "$file"
fi
done
echo -e "\n✅ OGG compression complete."
- name: Upload Optimized Folder
uses: actions/upload-artifact@v4
with:
name: optimized-pack
path: input/unpacked
- name: Comment + Label + Close Issue
if: always()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
ISSUE_NUMBER="${{ github.event.issue.number }}"
REPO="${{ github.repository }}"
RUN_ID="${{ github.run_id }}"
if ! command -v gh > /dev/null; then
sudo apt-get update -qq
sudo apt-get install -y -qq gh
fi
if [[ "${{ steps.extract.outputs.status }}" == "success" && -d input/unpacked ]]; then
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body=$'✅ Optimization complete!\n\nDownload from: https://github.com/'"$REPO"'/actions/runs/'"$RUN_ID"
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" \
--add-label optimization-success \
--remove-label optimize-request
gh issue close "$ISSUE_NUMBER" --repo "$REPO"
else
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body=$'❌ Optimization failed.\n\nEnsure your issue contains a valid public .zip file link.'
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" \
--add-label optimization-failed \
--remove-label optimize-request
gh issue close "$ISSUE_NUMBER" --repo "$REPO"
fi