notify-discord.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. name: Discord Notification
  2. on:
  3. release:
  4. types: [published]
  5. workflow_dispatch:
  6. inputs:
  7. tag_name:
  8. description: "Tag name (leave empty for latest release)"
  9. required: false
  10. type: string
  11. custom_title:
  12. description: "Custom title for the release notification"
  13. required: false
  14. type: string
  15. jobs:
  16. Discord:
  17. runs-on: ubuntu-latest
  18. name: Discord Notifier
  19. steps:
  20. - uses: actions/checkout@v4
  21. with:
  22. fetch-depth: 0
  23. - name: Get release info
  24. id: release-info
  25. run: |
  26. if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
  27. # For manual trigger, get the latest release or use the input
  28. if [ -n "${{ github.event.inputs.tag_name }}" ]; then
  29. TAG_NAME="${{ github.event.inputs.tag_name }}"
  30. else
  31. TAG_NAME=$(gh release list --limit 1 --json tagName --jq '.[0].tagName')
  32. fi
  33. # Get release info via GitHub CLI
  34. RELEASE_INFO=$(gh release view "$TAG_NAME" --json name,body,url,author,publishedAt,tagName)
  35. echo "tag_name=$(echo "$RELEASE_INFO" | jq -r '.tagName')" >> $GITHUB_OUTPUT
  36. echo "name=$(echo "$RELEASE_INFO" | jq -r '.name')" >> $GITHUB_OUTPUT
  37. # Use EOF for the body which may contain special characters
  38. echo "body<<EOF" >> $GITHUB_OUTPUT
  39. echo "$RELEASE_INFO" | jq -r '.body' >> $GITHUB_OUTPUT
  40. echo "EOF" >> $GITHUB_OUTPUT
  41. echo "html_url=$(echo "$RELEASE_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
  42. echo "author_login=$(echo "$RELEASE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  43. echo "author_html_url=https://github.com/$(echo "$RELEASE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  44. echo "published_at=$(echo "$RELEASE_INFO" | jq -r '.publishedAt')" >> $GITHUB_OUTPUT
  45. else
  46. # For automatic trigger, use event data
  47. echo "tag_name=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
  48. echo "name=${{ github.event.release.name }}" >> $GITHUB_OUTPUT
  49. # Use EOF for the automatic release body as well
  50. echo "body<<EOF" >> $GITHUB_OUTPUT
  51. echo "${{ github.event.release.body }}" >> $GITHUB_OUTPUT
  52. echo "EOF" >> $GITHUB_OUTPUT
  53. echo "html_url=${{ github.event.release.html_url }}" >> $GITHUB_OUTPUT
  54. echo "author_login=${{ github.event.release.author.login }}" >> $GITHUB_OUTPUT
  55. echo "author_html_url=${{ github.event.release.author.html_url }}" >> $GITHUB_OUTPUT
  56. echo "published_at=${{ github.event.release.published_at }}" >> $GITHUB_OUTPUT
  57. fi
  58. env:
  59. GH_TOKEN: ${{ github.token }}
  60. - name: Get previous release
  61. id: previous-release
  62. run: |
  63. PREV_TAG=$(git tag --sort=-version:refname | grep -v '${{ steps.release-info.outputs.tag_name }}' | head -n1)
  64. echo "previous_tag=${PREV_TAG}" >> $GITHUB_OUTPUT
  65. - name: Get changed files since last release
  66. id: changed-files
  67. uses: tj-actions/changed-files@v44
  68. with:
  69. base_sha: ${{ steps.previous-release.outputs.previous_tag }}
  70. separator: "\n• "
  71. - name: Create Discord webhook payload
  72. run: |
  73. # Create a temporary JSON file
  74. cat > discord_payload.json << 'EOF'
  75. {
  76. "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
  77. "embeds": [
  78. {
  79. "title": "🚀 New Release: ${{ steps.release-info.outputs.tag_name }}${{ github.event.inputs.custom_title && ' - ' || '' }}${{ github.event.inputs.custom_title || '' }}",
  80. "description": "${{ steps.release-info.outputs.name }}",
  81. "url": "${{ steps.release-info.outputs.html_url }}",
  82. "color": 5763719,
  83. "thumbnail": {
  84. "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  85. },
  86. "fields": [
  87. {
  88. "name": "📦 Version",
  89. "value": "`${{ steps.release-info.outputs.tag_name }}`",
  90. "inline": true
  91. },
  92. {
  93. "name": "👤 Released by",
  94. "value": "[${{ steps.release-info.outputs.author_login }}](${{ steps.release-info.outputs.author_html_url }})",
  95. "inline": true
  96. },
  97. {
  98. "name": "📁 Repository",
  99. "value": "[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})",
  100. "inline": true
  101. },
  102. {
  103. "name": "🔗 Download",
  104. "value": "[Release Page](${{ steps.release-info.outputs.html_url }})",
  105. "inline": true
  106. }
  107. ],
  108. "timestamp": "${{ steps.release-info.outputs.published_at }}",
  109. "footer": {
  110. "text": "Workout Cool • Release",
  111. "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  112. }
  113. }
  114. ]
  115. }
  116. EOF
  117. - name: Send Discord notification
  118. run: |
  119. curl -H "Content-Type: application/json" \
  120. -d @discord_payload.json \
  121. "${{ secrets.DISCORD_RELEASE_WEBHOOK }}"
  122. # https://stackoverflow.com/a/68068674/19395252
  123. # https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html
  124. # https://github.com/marketplace/actions/changed-files