notify-discord.yml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. jobs:
  12. Discord:
  13. runs-on: ubuntu-latest
  14. name: Discord Notifier
  15. steps:
  16. - uses: actions/checkout@v4
  17. with:
  18. fetch-depth: 0
  19. - name: Get release info
  20. id: release-info
  21. run: |
  22. if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
  23. # Pour trigger manuel, récupère le dernier release ou utilise l'input
  24. if [ -n "${{ github.event.inputs.tag_name }}" ]; then
  25. TAG_NAME="${{ github.event.inputs.tag_name }}"
  26. else
  27. TAG_NAME=$(gh release list --limit 1 --json tagName --jq '.[0].tagName')
  28. fi
  29. # Récupère les infos du release via GitHub CLI
  30. RELEASE_INFO=$(gh release view "$TAG_NAME" --json name,body,url,author,publishedAt,tagName)
  31. echo "tag_name=$(echo "$RELEASE_INFO" | jq -r '.tagName')" >> $GITHUB_OUTPUT
  32. echo "name=$(echo "$RELEASE_INFO" | jq -r '.name')" >> $GITHUB_OUTPUT
  33. # Utilise EOF pour le body qui peut contenir des caractères spéciaux
  34. echo "body<<EOF" >> $GITHUB_OUTPUT
  35. echo "$RELEASE_INFO" | jq -r '.body' >> $GITHUB_OUTPUT
  36. echo "EOF" >> $GITHUB_OUTPUT
  37. echo "html_url=$(echo "$RELEASE_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
  38. echo "author_login=$(echo "$RELEASE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  39. echo "author_html_url=https://github.com/$(echo "$RELEASE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  40. echo "published_at=$(echo "$RELEASE_INFO" | jq -r '.publishedAt')" >> $GITHUB_OUTPUT
  41. else
  42. # Pour trigger automatique, utilise les données de l'événement
  43. echo "tag_name=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
  44. echo "name=${{ github.event.release.name }}" >> $GITHUB_OUTPUT
  45. # Utilise EOF pour le body du release automatique aussi
  46. echo "body<<EOF" >> $GITHUB_OUTPUT
  47. echo "${{ github.event.release.body }}" >> $GITHUB_OUTPUT
  48. echo "EOF" >> $GITHUB_OUTPUT
  49. echo "html_url=${{ github.event.release.html_url }}" >> $GITHUB_OUTPUT
  50. echo "author_login=${{ github.event.release.author.login }}" >> $GITHUB_OUTPUT
  51. echo "author_html_url=${{ github.event.release.author.html_url }}" >> $GITHUB_OUTPUT
  52. echo "published_at=${{ github.event.release.published_at }}" >> $GITHUB_OUTPUT
  53. fi
  54. env:
  55. GH_TOKEN: ${{ github.token }}
  56. - name: Get previous release
  57. id: previous-release
  58. run: |
  59. PREV_TAG=$(git tag --sort=-version:refname | grep -v '${{ steps.release-info.outputs.tag_name }}' | head -n1)
  60. echo "previous_tag=${PREV_TAG}" >> $GITHUB_OUTPUT
  61. - name: Get changed files since last release
  62. id: changed-files
  63. uses: tj-actions/changed-files@v44
  64. with:
  65. base_sha: ${{ steps.previous-release.outputs.previous_tag }}
  66. separator: "\n• "
  67. - name: Prepare Discord message
  68. id: discord-message
  69. run: |
  70. # Utiliser jq pour créer un JSON valide
  71. DESCRIPTION_FULL="${{ steps.release-info.outputs.name }}\n\n${{ steps.release-info.outputs.body }}"
  72. DISCORD_EMBEDS=$(jq -n \
  73. --arg title "🚀 New Release: ${{ steps.release-info.outputs.tag_name }}" \
  74. --arg description "$DESCRIPTION_FULL" \
  75. --arg url "${{ steps.release-info.outputs.html_url }}" \
  76. --arg tag "${{ steps.release-info.outputs.tag_name }}" \
  77. --arg author "${{ steps.release-info.outputs.author_login }}" \
  78. --arg author_url "${{ steps.release-info.outputs.author_html_url }}" \
  79. --arg repo "${{ github.event.repository.name }}" \
  80. --arg repo_url "${{ github.event.repository.html_url }}" \
  81. --arg files "${{ steps.changed-files.outputs.all_changed_files }}" \
  82. --arg timestamp "${{ steps.release-info.outputs.published_at }}" \
  83. '[{
  84. "title": $title,
  85. "description": $description,
  86. "url": $url,
  87. "color": 5763719,
  88. "thumbnail": {
  89. "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  90. },
  91. "fields": [
  92. {
  93. "name": "📦 Version",
  94. "value": ("`" + $tag + "`"),
  95. "inline": true
  96. },
  97. {
  98. "name": "👤 Released by",
  99. "value": ("[" + $author + "](" + $author_url + ")"),
  100. "inline": true
  101. },
  102. {
  103. "name": "📁 Repository",
  104. "value": ("[" + $repo + "](" + $repo_url + ")"),
  105. "inline": true
  106. },
  107. {
  108. "name": "🔗 Download",
  109. "value": ("[Release Page](" + $url + ")"),
  110. "inline": true
  111. },
  112. {
  113. "name": "📝 Files Changed",
  114. "value": ("• " + $files),
  115. "inline": false
  116. }
  117. ],
  118. "timestamp": $timestamp,
  119. "footer": {
  120. "text": "Workout Cool • Release",
  121. "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  122. }
  123. }]')
  124. echo "discord_embeds<<EOF" >> $GITHUB_OUTPUT
  125. echo "$DISCORD_EMBEDS" >> $GITHUB_OUTPUT
  126. echo "EOF" >> $GITHUB_OUTPUT
  127. - name: Discord notification
  128. env:
  129. DISCORD_WEBHOOK: ${{ secrets.DISCORD_RELEASE_WEBHOOK }}
  130. DISCORD_AVATAR: 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png'
  131. DISCORD_EMBEDS: ${{ steps.discord-message.outputs.discord_embeds }}
  132. uses: Ilshidur/action-discord@master
  133. # https://stackoverflow.com/a/68068674/19395252
  134. # https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html
  135. # https://github.com/marketplace/actions/changed-files