build-on-comment.yml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. name: Build on Comment
  2. on:
  3. issue_comment:
  4. types: [created, edited]
  5. permissions:
  6. pull-requests: write
  7. concurrency:
  8. group: build-on-comment-${{ github.event.issue.number || github.event.pull_request.number }}
  9. cancel-in-progress: true
  10. jobs:
  11. check-and-build:
  12. if: github.event.issue.pull_request != null
  13. runs-on: ubuntu-latest
  14. steps:
  15. - name: Check if user is in reviewers team
  16. id: check-reviewer
  17. run: |
  18. USERNAME="${{ github.event.comment.user.login }}"
  19. STATUS_CODE=$(curl -s -H "Authorization: token ${{ secrets.LOOKUP_MEMBERS }}" \
  20. -o response.json -w "%{http_code}" \
  21. https://api.github.com/orgs/openwrt/teams/reviewers/memberships/$USERNAME)
  22. if grep -q '"state": "active"' response.json && [ "$STATUS_CODE" -eq 200 ]; then
  23. echo "authorized=true" >> $GITHUB_OUTPUT
  24. else
  25. echo "authorized=false" >> $GITHUB_OUTPUT
  26. fi
  27. - name: Parse build command
  28. if: steps.check-reviewer.outputs.authorized == 'true'
  29. id: parse-command
  30. run: |
  31. COMMENT="${{ github.event.comment.body }}"
  32. if echo "$COMMENT" | grep -q "build [a-zA-Z0-9_-]\+/[a-zA-Z0-9_-]\+/[a-zA-Z0-9_-]\+"; then
  33. BUILD_PATH=$(echo "$COMMENT" | grep -o "build [a-zA-Z0-9_-]\+/[a-zA-Z0-9_-]\+/[a-zA-Z0-9_-]\+" | sed 's/build //')
  34. TARGET=$(echo "$BUILD_PATH" | cut -d'/' -f1)
  35. SUBTARGET=$(echo "$BUILD_PATH" | cut -d'/' -f2)
  36. PROFILE=$(echo "$BUILD_PATH" | cut -d'/' -f3)
  37. echo "build_requested=true" >> $GITHUB_OUTPUT
  38. echo "target=$TARGET" >> $GITHUB_OUTPUT
  39. echo "subtarget=$SUBTARGET" >> $GITHUB_OUTPUT
  40. echo "profile=$PROFILE" >> $GITHUB_OUTPUT
  41. echo "build_path=$BUILD_PATH" >> $GITHUB_OUTPUT
  42. else
  43. echo "build_requested=false" >> $GITHUB_OUTPUT
  44. fi
  45. - name: Find existing build comment
  46. if: steps.parse-command.outputs.build_requested == 'true'
  47. id: find-comment
  48. uses: peter-evans/find-comment@v2
  49. with:
  50. issue-number: ${{ github.event.pull_request.number || github.event.issue.number }}
  51. comment-author: "github-actions[bot]"
  52. - name: Create early build comment
  53. if: steps.parse-command.outputs.build_requested == 'true'
  54. id: start-comment
  55. uses: peter-evans/create-or-update-comment@v3
  56. with:
  57. issue-number: ${{ github.event.pull_request.number || github.event.issue.number }}
  58. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  59. body: |
  60. 🚧 **Build in progress for** `${{ steps.parse-command.outputs.build_path }}`...
  61. You can follow progress [here](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
  62. *Triggered by: @${{ github.event.comment.user.login }}*
  63. edit-mode: replace
  64. - name: Checkout repository
  65. if: steps.parse-command.outputs.build_requested == 'true'
  66. uses: actions/checkout@v4
  67. with:
  68. token: ${{ secrets.GITHUB_TOKEN }}
  69. fetch-depth: 0
  70. ref: refs/pull/${{ github.event.issue.number }}/merge
  71. - name: Setup build environment
  72. if: steps.parse-command.outputs.build_requested == 'true'
  73. continue-on-error: true
  74. run: |
  75. sudo apt-get update
  76. sudo apt-get install -y build-essential libncurses5-dev gawk git subversion libssl-dev gettext zlib1g-dev swig unzip time rsync
  77. - name: Build target
  78. if: steps.parse-command.outputs.build_requested == 'true'
  79. id: build
  80. run: |
  81. make defconfig
  82. echo "CONFIG_DEVEL=y" > .config
  83. echo "CONFIG_BPF_TOOLCHAIN_HOST=y" >> .config
  84. echo "CONFIG_TARGET_${{ steps.parse-command.outputs.target }}=y" >> .config
  85. echo "CONFIG_TARGET_${{ steps.parse-command.outputs.target }}_${{ steps.parse-command.outputs.subtarget }}=y" >> .config
  86. echo "CONFIG_TARGET_${{ steps.parse-command.outputs.target }}_${{ steps.parse-command.outputs.subtarget }}_DEVICE_${{ steps.parse-command.outputs.profile }}=y" >> .config
  87. make defconfig
  88. make -j$(nproc) BUILD_LOG=1
  89. echo "build_success=true" >> $GITHUB_OUTPUT
  90. - name: Upload log
  91. uses: actions/upload-artifact@v4
  92. if: steps.check-reviewer.outputs.authorized == 'true' && (success() || failure())
  93. with:
  94. name: build-log-${{ steps.parse-command.outputs.target }}-${{ steps.parse-command.outputs.subtarget }}-${{ steps.parse-command.outputs.profile }}
  95. path: logs/
  96. - name: Create artifact archive
  97. if: steps.build.outputs.build_success == 'true'
  98. run: |
  99. cd bin/
  100. tar -czf ../build-artifacts.tar.gz *
  101. cd ..
  102. - name: Upload build artifacts
  103. if: steps.build.outputs.build_success == 'true'
  104. uses: actions/upload-artifact@v4
  105. with:
  106. name: build-${{ steps.parse-command.outputs.target }}-${{ steps.parse-command.outputs.subtarget }}-${{ steps.parse-command.outputs.profile }}
  107. path: build-artifacts.tar.gz
  108. - name: Update comment with build results
  109. if: steps.build.outputs.build_success == 'true'
  110. uses: peter-evans/create-or-update-comment@v3
  111. with:
  112. comment-id: ${{ steps.start-comment.outputs.comment-id }}
  113. issue-number: ${{ github.event.pull_request.number || github.event.issue.number }}
  114. body: |
  115. ## Build Results for `${{ steps.parse-command.outputs.build_path }}`
  116. ✅ **Build completed successfully!**
  117. **Target:** `${{ steps.parse-command.outputs.target }}`
  118. **Subtarget:** `${{ steps.parse-command.outputs.subtarget }}`
  119. **Profile:** `${{ steps.parse-command.outputs.profile }}`
  120. 📦 **Artifacts:** [Download build-${{ steps.parse-command.outputs.target }}-${{ steps.parse-command.outputs.subtarget }}-${{ steps.parse-command.outputs.profile }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
  121. *Build triggered by: @${{ github.event.comment.user.login }}*
  122. *Last updated: ${{ github.event.comment.created_at }}*
  123. edit-mode: replace
  124. - name: Update comment on build failure
  125. if: steps.parse-command.outputs.build_requested == 'true' && steps.build.outputs.build_success == 'false'
  126. uses: peter-evans/create-or-update-comment@v3
  127. with:
  128. comment-id: ${{ steps.start-comment.outputs.comment-id }}
  129. issue-number: ${{ github.event.pull_request.number || github.event.issue.number }}
  130. body: |
  131. ## Build Results for `${{ steps.parse-command.outputs.build_path }}`
  132. ❌ **Build failed!**
  133. Please check the [action logs](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details.
  134. *Build triggered by: @${{ github.event.comment.user.login }}*
  135. edit-mode: replace