build.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. name: Build, Lint & Coverage
  2. on:
  3. push:
  4. branches: [ "master" ]
  5. pull_request:
  6. branches: [ "master" ]
  7. env:
  8. BUILD_TYPE: Release
  9. jobs:
  10. # Linting job with MegaLinter
  11. megalinter:
  12. name: MegaLinter
  13. runs-on: ubuntu-latest
  14. permissions:
  15. contents: read
  16. issues: write
  17. pull-requests: write
  18. steps:
  19. - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
  20. with:
  21. fetch-depth: 0
  22. - name: MegaLinter
  23. uses: oxsecurity/megalinter/flavors/c_cpp@v9.1.0
  24. env:
  25. VALIDATE_ALL_CODEBASE: true
  26. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  27. # Use C/C++ flavor for faster execution
  28. MEGALINTER_FLAVOR: c_cpp
  29. # Enable specific linters you want
  30. ENABLE_LINTERS: C_CPPLINT,C_CPPCHECK,C_CLANG_FORMAT
  31. # Optional: disable linters you don't need
  32. DISABLE_LINTERS: SPELL_CSPELL
  33. # Fail on errors (set to false if you want warnings only)
  34. DISABLE_ERRORS: false
  35. - name: Archive MegaLinter reports
  36. if: always()
  37. uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
  38. with:
  39. name: megalinter-reports
  40. path: |
  41. megalinter-reports
  42. mega-linter.log
  43. # Build, test and coverage job
  44. build-and-test:
  45. name: Build, Test & Coverage
  46. runs-on: ubuntu-latest
  47. needs: megalinter
  48. if: success() || failure()
  49. steps:
  50. - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
  51. with:
  52. fetch-depth: 0
  53. - name: Install dependencies
  54. run: |
  55. sudo apt-get update
  56. sudo apt-get install -y \
  57. libcap-dev \
  58. lcov \
  59. cpputest
  60. - name: Set up Python for gcovr
  61. uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548
  62. with:
  63. python-version: '3.11'
  64. - name: Install gcovr
  65. run: pip install gcovr
  66. - name: Configure CMake
  67. run: |
  68. cmake -S ${{github.workspace}}/source \
  69. -B ${{github.workspace}}/build \
  70. -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
  71. -DOpENer_PLATFORM:STRING="POSIX" \
  72. -DBUILD_SHARED_LIBS:BOOL=OFF \
  73. -DOpENer_TRACES:BOOL=OFF \
  74. -DOpENer_TESTS:BOOL=ON \
  75. -DCPPUTEST_HOME:PATH=/usr
  76. - name: Build
  77. run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
  78. - name: Test
  79. working-directory: ${{github.workspace}}/build
  80. run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
  81. - name: Generate coverage reports
  82. run: |
  83. # Generate HTML report for human viewing
  84. gcovr --html-details --output coverage-report.html
  85. # Generate Cobertura XML for tools/badges
  86. gcovr --cobertura --output coverage.xml
  87. # Generate text summary for console
  88. gcovr --print-summary > coverage-summary.txt
  89. # Print summary to console
  90. echo "Coverage Summary:"
  91. cat coverage-summary.txt
  92. - name: Upload coverage reports
  93. uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
  94. with:
  95. name: coverage-report
  96. path: |
  97. coverage-report.html
  98. coverage-report.*.html
  99. coverage.xml
  100. coverage-summary.txt
  101. - name: Create coverage summary comment
  102. if: github.event_name == 'pull_request'
  103. uses: actions/github-script@v7
  104. with:
  105. script: |
  106. const fs = require('fs');
  107. const summary = fs.readFileSync('coverage-summary.txt', 'utf8');
  108. const comment = `## 📊 Coverage Report\n\n\`\`\`\n${summary}\n\`\`\`\n\nDownload the [detailed HTML report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) from the artifacts.`;
  109. github.rest.issues.createComment({
  110. issue_number: context.issue.number,
  111. owner: context.repo.owner,
  112. repo: context.repo.repo,
  113. body: comment
  114. });
  115. - name: Generate coverage badge
  116. if: github.ref == 'refs/heads/master'
  117. run: |
  118. # Extract coverage percentage
  119. COVERAGE=$(gcovr --print-summary | grep 'lines:' | awk '{print $2}')
  120. echo "COVERAGE_PERCENTAGE=$COVERAGE" >> $GITHUB_ENV
  121. echo "Coverage: $COVERAGE"
  122. - name: Upload build artifacts
  123. uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
  124. with:
  125. name: build-artifacts
  126. path: ${{github.workspace}}/build