anchor.yml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: dtolnay/rust-toolchain@1.79.0
  99. with:
  100. components: rustfmt, clippy
  101. - uses: heyAyushh/setup-anchor@v4.9
  102. with:
  103. anchor-version: 0.30.1
  104. solana-cli-version: stable
  105. node-version: 20.x
  106. use-avm: false
  107. - name: Display Versions and Install pnpm
  108. run: |
  109. solana -V
  110. solana-keygen new --no-bip39-passphrase
  111. rustc -V
  112. anchor -V
  113. npm i -g pnpm
  114. - name: Build and Test
  115. env:
  116. TOTAL_PROJECTS: ${{ needs.changes.outputs.total_projects }}
  117. PROJECTS_PER_JOB: ${{ env.MIN_PROJECTS_PER_JOB }}
  118. run: |
  119. function build_and_test() {
  120. local project=$1
  121. echo "Building and Testing $project"
  122. cd "$project" || return 1
  123. # Run anchor build
  124. if ! anchor build; then
  125. echo "::error::anchor build failed for $project"
  126. echo "$project: anchor build failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  127. rm -rf target
  128. cd - > /dev/null
  129. return 1
  130. fi
  131. # Install dependencies
  132. if ! pnpm install --frozen-lockfile; then
  133. echo "::error::pnpm install failed for $project"
  134. echo "$project: pnpm install failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  135. cd - > /dev/null
  136. return 1
  137. fi
  138. # Run anchor test
  139. if ! anchor test; then
  140. echo "::error::anchor test failed for $project"
  141. echo "$project: anchor test failed" >> $GITHUB_WORKSPACE/failed_projects.txt
  142. rm -rf target node_modules
  143. cd - > /dev/null
  144. return 1
  145. fi
  146. echo "Build and tests succeeded for $project."
  147. rm -rf target node_modules
  148. cd - > /dev/null
  149. return 0
  150. }
  151. # Determine which projects to build in this job
  152. readarray -t all_projects < <(echo '${{ needs.changes.outputs.changed_projects }}' | jq -r '.[]?')
  153. start_index=$(( ${{ matrix.index }} * PROJECTS_PER_JOB ))
  154. end_index=$(( start_index + PROJECTS_PER_JOB ))
  155. end_index=$(( end_index > TOTAL_PROJECTS ? TOTAL_PROJECTS : end_index ))
  156. echo "Projects to build and test in this job"
  157. for i in $(seq $start_index $(( end_index - 1 ))); do
  158. echo "${all_projects[$i]}"
  159. done
  160. # Build and test projects
  161. failed=false
  162. failed_projects=()
  163. for i in $(seq $start_index $(( end_index - 1 ))); do
  164. echo "::group::Building and testing ${all_projects[$i]}"
  165. if ! build_and_test "${all_projects[$i]}"; then
  166. failed=true
  167. failed_projects+=("${all_projects[$i]}")
  168. fi
  169. echo "::endgroup::"
  170. done
  171. if [[ "$failed" == "true" ]]; then
  172. echo "::group::Failed projects"
  173. cat $GITHUB_WORKSPACE/failed_projects.txt
  174. echo "::endgroup::"
  175. echo "failed_projects=${failed_projects[@]}" >> $GITHUB_OUTPUT
  176. exit 1
  177. else
  178. echo "failed_projects=" >> $GITHUB_OUTPUT
  179. fi
  180. - name: Set failed projects output
  181. id: set-failed
  182. if: failure()
  183. run: |
  184. # Prepare failed projects list for output
  185. failed_projects=$(cat $GITHUB_WORKSPACE/failed_projects.txt | jq -R -s -c 'split("\n")[:-1]')
  186. echo "failed_projects=$failed_projects" >> $GITHUB_OUTPUT
  187. summary:
  188. needs: [changes, build-and-test]
  189. if: always()
  190. runs-on: ubuntu-latest
  191. steps:
  192. - uses: actions/checkout@v4
  193. - name: Create job summary
  194. run: |
  195. echo "## Anchor Workflow Summary" >> $GITHUB_STEP_SUMMARY
  196. echo "- Total projects: ${{ needs.changes.outputs.total_projects }}" >> $GITHUB_STEP_SUMMARY
  197. # List all processed projects
  198. echo "<details>" >> $GITHUB_STEP_SUMMARY
  199. echo "<summary>Projects processed (click to expand)</summary>" >> $GITHUB_STEP_SUMMARY
  200. echo "" >> $GITHUB_STEP_SUMMARY
  201. echo '${{ needs.changes.outputs.changed_projects }}' | jq -r '.[]' | while read project; do
  202. echo "- $project" >> $GITHUB_STEP_SUMMARY
  203. done
  204. echo "" >> $GITHUB_STEP_SUMMARY
  205. echo "</details>" >> $GITHUB_STEP_SUMMARY
  206. # Report build and test results
  207. if [[ "${{ needs.build-and-test.result }}" == "failure" ]]; then
  208. echo "## :x: Build or tests failed" >> $GITHUB_STEP_SUMMARY
  209. echo "<details>" >> $GITHUB_STEP_SUMMARY
  210. echo "<summary>Failed projects (click to expand)</summary>" >> $GITHUB_STEP_SUMMARY
  211. echo "" >> $GITHUB_STEP_SUMMARY
  212. failed_projects='${{ needs.build-and-test.outputs.failed_projects }}'
  213. if [[ -n "$failed_projects" ]]; then
  214. echo "$failed_projects" | jq -r '.[]' | while IFS=: read -r project failure_reason; do
  215. echo "- **$project**" >> $GITHUB_STEP_SUMMARY
  216. echo " - Failure reason: $failure_reason" >> $GITHUB_STEP_SUMMARY
  217. done
  218. else
  219. echo "No failed projects reported. This might indicate an unexpected error in the workflow." >> $GITHUB_STEP_SUMMARY
  220. fi
  221. echo "" >> $GITHUB_STEP_SUMMARY
  222. echo "</details>" >> $GITHUB_STEP_SUMMARY
  223. elif [[ "${{ needs.build-and-test.result }}" == "success" ]]; then
  224. echo "## :white_check_mark: All builds and tests passed" >> $GITHUB_STEP_SUMMARY
  225. else
  226. echo "## :warning: Build and test job was skipped or canceled" >> $GITHUB_STEP_SUMMARY
  227. fi