anchor.yml 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. name: Anchor
  2. on:
  3. schedule:
  4. - cron: "0 0 * * *"
  5. push:
  6. branches:
  7. - main
  8. pull_request:
  9. types: [opened, synchronize, reopened]
  10. branches:
  11. - main
  12. env:
  13. MAX_JOBS: 64
  14. MIN_PROJECTS_PER_JOB: 4
  15. MIN_PROJECTS_FOR_MATRIX: 4
  16. jobs:
  17. changes:
  18. runs-on: ubuntu-latest
  19. permissions:
  20. pull-requests: read
  21. outputs:
  22. changed_projects: ${{ steps.analyze.outputs.changed_projects }}
  23. total_projects: ${{ steps.analyze.outputs.total_projects }}
  24. matrix: ${{ steps.matrix.outputs.matrix }}
  25. steps:
  26. - uses: actions/checkout@v4
  27. - uses: dorny/paths-filter@v3
  28. id: changes
  29. if: github.event_name == 'pull_request'
  30. with:
  31. list-files: shell
  32. filters: |
  33. anchor:
  34. - added|modified: '**/anchor/**'
  35. workflow:
  36. - added|modified: '.github/workflows/anchor.yml'
  37. - name: Analyze Changes
  38. id: analyze
  39. run: |
  40. # Generate ignore pattern, excluding comments
  41. ignore_pattern=$(grep -v '^#' .github/.ghaignore | grep -v '^$' | tr '\n' '|' | sed 's/|$//')
  42. echo "Ignore pattern: $ignore_pattern"
  43. function get_projects() {
  44. find . -type d -name "anchor" | grep -vE "$ignore_pattern" | sort
  45. }
  46. # Determine which projects to build and test
  47. if [[ "${{ github.event_name }}" == "push" || "${{ github.event_name }}" == "schedule" || "${{ steps.changes.outputs.workflow }}" == "true" ]]; then
  48. projects=$(get_projects)
  49. elif [[ "${{ steps.changes.outputs.anchor }}" == "true" ]]; then
  50. changed_files=(${{ steps.changes.outputs.anchor_files }})
  51. projects=$(for file in "${changed_files[@]}"; do dirname "${file}" | grep anchor | sed 's#/anchor/.*#/anchor#g'; done | grep -vE "$ignore_pattern" | sort -u)
  52. else
  53. projects=""
  54. fi
  55. # Output project information
  56. if [[ -n "$projects" ]]; then
  57. echo "Projects to build and test"
  58. echo "$projects"
  59. total_projects=$(echo "$projects" | wc -l)
  60. echo "Total projects: $total_projects"
  61. echo "total_projects=$total_projects" >> $GITHUB_OUTPUT
  62. echo "changed_projects=$(echo "$projects" | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
  63. else
  64. echo "No projects to build and test."
  65. echo "total_projects=0" >> $GITHUB_OUTPUT
  66. echo "changed_projects=[]" >> $GITHUB_OUTPUT
  67. fi
  68. - name: Generate matrix
  69. id: matrix
  70. run: |
  71. total_projects=${{ steps.analyze.outputs.total_projects }}
  72. max_jobs=${{ env.MAX_JOBS }}
  73. min_projects_per_job=${{ env.MIN_PROJECTS_PER_JOB }}
  74. min_projects_for_matrix=${{ env.MIN_PROJECTS_FOR_MATRIX }}
  75. # Generate matrix based on number of projects
  76. if [ "$total_projects" -lt "$min_projects_for_matrix" ]; then
  77. echo "matrix=[0]" >> $GITHUB_OUTPUT
  78. else
  79. projects_per_job=$(( (total_projects + max_jobs - 1) / max_jobs ))
  80. projects_per_job=$(( projects_per_job > min_projects_per_job ? projects_per_job : min_projects_per_job ))
  81. num_jobs=$(( (total_projects + projects_per_job - 1) / projects_per_job ))
  82. indices=$(seq 0 $(( num_jobs - 1 )))
  83. echo "matrix=[$(echo $indices | tr ' ' ',')]" >> $GITHUB_OUTPUT
  84. fi
  85. build-and-test:
  86. needs: changes
  87. if: needs.changes.outputs.total_projects != '0'
  88. runs-on: ubuntu-latest
  89. strategy:
  90. fail-fast: false
  91. matrix:
  92. index: ${{ fromJson(needs.changes.outputs.matrix) }}
  93. name: build-and-test-group-${{ matrix.index }}
  94. outputs:
  95. failed_projects: ${{ steps.set-failed.outputs.failed_projects }}
  96. steps:
  97. - uses: actions/checkout@v4
  98. - uses: heyAyushh/setup-anchor@v4.2
  99. with:
  100. anchor-version: 0.30.1
  101. solana-cli-version: 1.18.17
  102. node-version: 20.x
  103. use-avm: false
  104. - name: Display Versions and Install pnpm
  105. run: |
  106. solana -V
  107. solana-keygen new --no-bip39-passphrase
  108. rustc -V
  109. anchor -V
  110. npm i -g pnpm
  111. - name: Build and Test
  112. env:
  113. TOTAL_PROJECTS: ${{ needs.changes.outputs.total_projects }}
  114. PROJECTS_PER_JOB: ${{ env.MIN_PROJECTS_PER_JOB }}
  115. run: |
  116. function build_and_test() {
  117. local project=$1
  118. echo "Building and Testing $project"
  119. cd "$project" || return 1
  120. # Run anchor build
  121. if ! anchor build; then
  122. echo "::error::anchor build failed for $project"
  123. echo "$project: anchor build failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  124. rm -rf target
  125. cd - > /dev/null
  126. return 1
  127. fi
  128. # Install dependencies
  129. if ! pnpm install --frozen-lockfile; then
  130. echo "::error::pnpm install failed for $project"
  131. echo "$project: pnpm install failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  132. cd - > /dev/null
  133. return 1
  134. fi
  135. # Run anchor test
  136. if ! anchor test; then
  137. echo "::error::anchor test failed for $project"
  138. echo "$project: anchor test failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  139. rm -rf target node_modules
  140. cd - > /dev/null
  141. return 1
  142. fi
  143. echo "Build and tests succeeded for $project."
  144. rm -rf target node_modules
  145. cd - > /dev/null
  146. return 0
  147. }
  148. # Determine which projects to build in this job
  149. readarray -t all_projects < <(echo '${{ needs.changes.outputs.changed_projects }}' | jq -r '.[]?')
  150. start_index=$(( ${{ matrix.index }} * PROJECTS_PER_JOB ))
  151. end_index=$(( start_index + PROJECTS_PER_JOB ))
  152. end_index=$(( end_index > TOTAL_PROJECTS ? TOTAL_PROJECTS : end_index ))
  153. echo "Projects to build and test in this job"
  154. for i in $(seq $start_index $(( end_index - 1 ))); do
  155. echo "${all_projects[$i]}"
  156. done
  157. # Build and test projects
  158. failed=false
  159. failed_projects=()
  160. for i in $(seq $start_index $(( end_index - 1 ))); do
  161. echo "::group::Building and testing ${all_projects[$i]}"
  162. if ! build_and_test "${all_projects[$i]}"; then
  163. failed=true
  164. failed_projects+=("${all_projects[$i]}")
  165. fi
  166. echo "::endgroup::"
  167. done
  168. if [[ "$failed" == "true" ]]; then
  169. echo "::group::Failed projects"
  170. cat $GITHUB_WORKSPACE/failed_projects.txt
  171. echo "::endgroup::"
  172. echo "failed_projects=${failed_projects[@]}" >> $GITHUB_OUTPUT
  173. exit 1
  174. else
  175. echo "failed_projects=" >> $GITHUB_OUTPUT
  176. fi
  177. - name: Set failed projects output
  178. id: set-failed
  179. if: failure()
  180. run: |
  181. # Prepare failed projects list for output
  182. failed_projects=$(cat $GITHUB_WORKSPACE/failed_projects.txt | jq -R -s -c 'split("\n")[:-1]')
  183. echo "failed_projects=$failed_projects" >> $GITHUB_OUTPUT
  184. summary:
  185. needs: [changes, build-and-test]
  186. if: always()
  187. runs-on: ubuntu-latest
  188. steps:
  189. - uses: actions/checkout@v4
  190. - name: Create job summary
  191. run: |
  192. echo "## Anchor Workflow Summary" >> $GITHUB_STEP_SUMMARY
  193. echo "- Total projects: ${{ needs.changes.outputs.total_projects }}" >> $GITHUB_STEP_SUMMARY
  194. # List all processed projects
  195. echo "<details>" >> $GITHUB_STEP_SUMMARY
  196. echo "<summary>Projects processed (click to expand)</summary>" >> $GITHUB_STEP_SUMMARY
  197. echo "" >> $GITHUB_STEP_SUMMARY
  198. echo '${{ needs.changes.outputs.changed_projects }}' | jq -r '.[]' | while read project; do
  199. echo "- $project" >> $GITHUB_STEP_SUMMARY
  200. done
  201. echo "" >> $GITHUB_STEP_SUMMARY
  202. echo "</details>" >> $GITHUB_STEP_SUMMARY
  203. # Report build and test results
  204. if [[ "${{ needs.build-and-test.result }}" == "failure" ]]; then
  205. echo "## :x: Build or tests failed" >> $GITHUB_STEP_SUMMARY
  206. echo "<details>" >> $GITHUB_STEP_SUMMARY
  207. echo "<summary>Failed projects (click to expand)</summary>" >> $GITHUB_STEP_SUMMARY
  208. echo "" >> $GITHUB_STEP_SUMMARY
  209. failed_projects='${{ needs.build-and-test.outputs.failed_projects }}'
  210. if [[ -n "$failed_projects" ]]; then
  211. echo "$failed_projects" | jq -r '.[]' | while IFS=: read -r project failure_reason; do
  212. echo "- **$project**" >> $GITHUB_STEP_SUMMARY
  213. echo " - Failure reason: $failure_reason" >> $GITHUB_STEP_SUMMARY
  214. done
  215. else
  216. echo "No failed projects reported. This might indicate an unexpected error in the workflow." >> $GITHUB_STEP_SUMMARY
  217. fi
  218. echo "" >> $GITHUB_STEP_SUMMARY
  219. echo "</details>" >> $GITHUB_STEP_SUMMARY
  220. elif [[ "${{ needs.build-and-test.result }}" == "success" ]]; then
  221. echo "## :white_check_mark: All builds and tests passed" >> $GITHUB_STEP_SUMMARY
  222. else
  223. echo "## :warning: Build and test job was skipped or canceled" >> $GITHUB_STEP_SUMMARY
  224. fi