Updated Day 1869 #357
This file contains hidden or 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: Notify Slack on New Post | |
| on: | |
| push: | |
| branches: | |
| - master | |
| paths: | |
| - '_posts/**' | |
| workflow_dispatch: | |
| jobs: | |
| notify_slack: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v2 | |
| with: | |
| fetch-depth: 0 | |
| - name: Debug info | |
| run: | | |
| echo "Triggered by: ${{ github.event_name }}" | |
| echo "Most recent commit:" | |
| git log -1 | |
| - name: Determine file(s) and build Slack message | |
| id: files | |
| run: | | |
| if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then | |
| echo "🧪 Manual run — using most recently modified file in _posts/" | |
| FILES=$(find _posts -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-) | |
| else | |
| echo "🔍 Push event — checking for new files" | |
| FILES=$(git diff --diff-filter=A --name-only HEAD^ HEAD _posts/) | |
| fi | |
| if [[ -z "$FILES" ]]; then | |
| echo "message=No new files to process." >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| MESSAGE="" | |
| for FILE in $FILES; do | |
| echo "🆕 Processing: $FILE" | |
| TITLE=$(awk '/^---/{f=!f; next} f && /^title:/ {sub(/^title:[ ]*/, "", $0); print $0; exit}' "$FILE") | |
| DESCRIPTION=$(awk '/^---/{f=!f; next} f && /^description:/ {sub(/^description:[ ]*/, "", $0); print $0; exit}' "$FILE") | |
| echo "📌 Title: $TITLE" | |
| echo "📝 Description: $DESCRIPTION" | |
| URL="https://github.com/mkiser/WTFJHT/blob/master/${FILE}" | |
| MESSAGE+="📦 *<${URL}|${TITLE}: ${DESCRIPTION}>* → GitHub\n" | |
| done | |
| echo -e "✅ Slack message:\n$MESSAGE" | |
| # Escape special characters for GitHub output | |
| MESSAGE="${MESSAGE//'%'/'%25'}" | |
| MESSAGE="${MESSAGE//$'\n'/'%0A'}" | |
| MESSAGE="${MESSAGE//$'\r'/'%0D'}" | |
| echo "message=$MESSAGE" >> $GITHUB_OUTPUT | |
| - name: Send message to Slack | |
| run: | | |
| echo "⏩ Sending to Slack:" | |
| echo "${{ steps.files.outputs.message }}" | |
| # Escape double quotes for JSON | |
| ESCAPED_MESSAGE=$(echo "${{ steps.files.outputs.message }}" | sed 's/"/\\"/g') | |
| PAYLOAD="{\"text\": \"${ESCAPED_MESSAGE}\"}" | |
| curl -v -X POST -H 'Content-type: application/json' \ | |
| --data "$PAYLOAD" \ | |
| ${{ secrets.SLACK_WEBHOOK_URL }} |