pre-commit.yml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. name: pre-commit
  3. on:
  4. pull_request:
  5. push:
  6. jobs:
  7. pre-commit:
  8. runs-on: ubuntu-latest
  9. env:
  10. RAW_LOG: pre-commit.log
  11. CS_XML: pre-commit.xml
  12. steps:
  13. - name: Install required tools
  14. run: sudo apt-get update && sudo apt-get install cppcheck
  15. if: false
  16. # The next uses the git API because there is no clone yet.
  17. # This is faster for a big repo.
  18. - name: Get all changed php files (if PR)
  19. id: changed-php
  20. uses: tj-actions/changed-files@v42
  21. if: github.event_name == 'pull_request'
  22. with:
  23. files: |
  24. **.php
  25. # Checkout git sources to analyze
  26. - uses: actions/checkout@v4
  27. # Action setup-python needs a requirements.txt or pyproject.toml
  28. # This ensures one of them exists.
  29. - name: Create requirements.txt if no requirements.txt or pyproject.toml
  30. run: |-
  31. [ -r requirements.txt ] || [ -r pyproject.toml ] || touch requirements.txt
  32. # Install python and pre-commit tool
  33. - uses: actions/setup-python@v5
  34. with:
  35. cache: pip
  36. python-version: "3.11"
  37. - run: python -m pip install pre-commit
  38. # Restore previous cache of precommit
  39. - uses: actions/cache/restore@v4
  40. with:
  41. path: ~/.cache/pre-commit/
  42. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  43. # Run all the precommit tools (defined into pre-commit-config.yaml).
  44. # We can force exclusion of some of them here.
  45. - name: Run pre-commit hooks
  46. env:
  47. # SKIP is used by pre-commit to not execute certain hooks
  48. SKIP: no-commit-to-branch,php-cs,php-cbf,trailing-whitespace,end-of-file-fixer,check-json,check-executables-have-shebangs,check-shebang-scripts-are-executable,beautysh,yamllint,shellcheck
  49. run: |
  50. set -o pipefail
  51. pre-commit gc
  52. pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG}
  53. # The next uses git, which is slow for a bit repo.
  54. # - name: Get all changed php files (if PR)
  55. # id: changed-php
  56. # uses: tj-actions/changed-files@v42
  57. # if: github.event_name == 'pull_request'
  58. # with:
  59. # files: |
  60. # **.php
  61. - name: Setup PHPCS
  62. uses: shivammathur/setup-php@v2
  63. # Install when we're going to run phpcs
  64. if: |
  65. steps.changed-php.outputs.any_changed == 'true'
  66. ||
  67. (
  68. github.event_name == 'push'
  69. && (
  70. github.event.ref == 'refs/heads/develop'
  71. || endsWith(github.event.ref, '.0')
  72. )
  73. )
  74. with:
  75. php-version: 8.1
  76. coverage: none # disable xdebug, pcov
  77. tools: phpcs
  78. - name: Run some pre-commit hooks on selected changed files only
  79. if: steps.changed-php.outputs.any_changed == 'true'
  80. env:
  81. ALL_CHANGED_FILES: ${{ steps.changed-php.outputs.all_changed_files }}
  82. run: |
  83. set -o pipefail
  84. pre-commit run php-cs --files ${ALL_CHANGED_FILES} | tee -a ${RAW_LOG}
  85. - name: Run some pre-commit hooks on all files on push to "main" branches
  86. if: |
  87. github.event_name == 'push'
  88. && (
  89. github.event.ref == 'refs/heads/develop'
  90. || endsWith(github.event.ref, '.0')
  91. )
  92. run: |
  93. set -o pipefail
  94. ln -sf ~/.cache .cache # Absolute path in .pre-commit-config.yaml
  95. pre-commit run --hook-stage manual -a php-cs-with-cache | tee -a ${RAW_LOG}
  96. ls -l ~/.cache/pre-commit/
  97. - name: Convert Raw Log to Annotations
  98. uses: mdeweerd/logToCheckStyle@v2024.2.9
  99. if: ${{ failure() }}
  100. with:
  101. in: ${{ env.RAW_LOG }}
  102. # Save the precommit cache
  103. - uses: actions/cache/save@v4
  104. if: ${{ ! cancelled() }}
  105. with:
  106. path: ~/.cache/pre-commit/
  107. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  108. # Upload result log files of precommit into the Artifact shared store
  109. - name: Provide log as artifact
  110. uses: actions/upload-artifact@v4
  111. if: ${{ ! cancelled() }}
  112. with:
  113. name: precommit-logs
  114. path: |
  115. ${{ env.RAW_LOG }}
  116. ${{ env.CS_XML }}
  117. retention-days: 2