sonar.yml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Run secret-dependent sonar tests only after /ok-to-sonar approval
  2. on:
  3. pull_request:
  4. repository_dispatch:
  5. types: [ok-to-sonar-command]
  6. env:
  7. # Common versions
  8. GO_VERSION: '1.16'
  9. GOLANGCI_VERSION: 'v1.33'
  10. DOCKER_BUILDX_VERSION: 'v0.4.2'
  11. GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
  12. name: sonar
  13. jobs:
  14. sonar-fork:
  15. runs-on: ubuntu-latest
  16. if:
  17. github.event_name == 'repository_dispatch'
  18. steps:
  19. - name: Fork based /ok-to-sonar checkout
  20. uses: actions/checkout@v2
  21. with:
  22. ref: 'refs/pull/${{ github.event.client_payload.pull_request.number }}/merge'
  23. - name: Fetch History
  24. run: git fetch --prune --unshallow
  25. - name: Setup Go
  26. uses: actions/setup-go@v2
  27. with:
  28. go-version: ${{ env.GO_VERSION }}
  29. - name: Find the Go Cache
  30. id: go
  31. run: |
  32. echo "::set-output name=build-cache::$(go env GOCACHE)"
  33. echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
  34. - name: Cache the Go Build Cache
  35. uses: actions/cache@v2.1.6
  36. with:
  37. path: ${{ steps.go.outputs.build-cache }}
  38. key: ${{ runner.os }}-build-unit-tests-${{ hashFiles('**/go.sum') }}
  39. restore-keys: ${{ runner.os }}-build-unit-tests-
  40. - name: Cache Go Dependencies
  41. uses: actions/cache@v2.1.6
  42. with:
  43. path: ${{ steps.go.outputs.mod-cache }}
  44. key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
  45. restore-keys: ${{ runner.os }}-pkg-
  46. - name: Add envtest binaries
  47. run: |
  48. curl -sSLo envtest-bins.tar.gz "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-${{env.KUBEBUILDER_TOOLS_VERSION}}-linux-amd64.tar.gz"
  49. sudo mkdir -p /usr/local/kubebuilder
  50. sudo tar -C /usr/local/kubebuilder --strip-components=1 -zvxf envtest-bins.tar.gz
  51. - name: Cache envtest binaries
  52. uses: actions/cache@v2.1.6
  53. with:
  54. path: /usr/local/kubebuilder
  55. key: ${{ runner.os }}-kubebuilder-${{env.KUBEBUILDER_TOOLS_VERSION}}
  56. restore-keys: ${{ runner.os }}-kubebuilder-
  57. - name: Run Unit Tests
  58. run: |
  59. export KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT=true
  60. make test
  61. - name: SonarCloud Scans
  62. if: env.SONAR_TOKEN != ''
  63. uses: SonarSource/sonarcloud-github-action@master
  64. env:
  65. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
  66. SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  67. # Update check run called "integration-fork"
  68. - uses: actions/github-script@v5
  69. id: update-check-run
  70. if: ${{ always() }}
  71. env:
  72. number: ${{ github.event.client_payload.pull_request.number }}
  73. job: ${{ github.job }}
  74. # Conveniently, job.status maps to https://developer.github.com/v3/checks/runs/#update-a-check-run
  75. conclusion: ${{ job.status }}
  76. with:
  77. github-token: ${{ secrets.GITHUB_TOKEN }}
  78. script: |
  79. const { data: pull } = await github.pulls.get({
  80. ...context.repo,
  81. pull_number: process.env.number
  82. });
  83. const ref = pull.head.sha;
  84. const { data: checks } = await github.checks.listForRef({
  85. ...context.repo,
  86. ref
  87. });
  88. const check = checks.check_runs.filter(c => c.name === process.env.job);
  89. const { data: result } = await github.checks.update({
  90. ...context.repo,
  91. check_run_id: check[0].id,
  92. status: 'completed',
  93. conclusion: process.env.conclusion
  94. });
  95. return result;