|
| 1 | +name: Tests and Linters Scalingo go-utils Packages |
| 2 | + |
| 3 | +on: [push] |
| 4 | + |
| 5 | +# # The list of permissions is explained on the GitHub doc: |
| 6 | +# # https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs |
| 7 | +# permissions: |
| 8 | +# # allow read access to pull request. Use with golangci-lint `only-new-issues` option. |
| 9 | +# pull-requests: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + detect-modules: |
| 13 | + name: Detect the Go modules declared in go-utils |
| 14 | + runs-on: ubuntu-22.04 |
| 15 | + outputs: |
| 16 | + modules: ${{ steps.set-modules.outputs.modules }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + # We need to define the fetch-depth to 0 so that we can get the commit ID of the master branch |
| 21 | + fetch-depth: 0 |
| 22 | + - uses: actions/setup-go@v5 |
| 23 | + with: |
| 24 | + go-version: stable |
| 25 | + check-latest: true |
| 26 | + - id: set-modules |
| 27 | + run: | |
| 28 | + echo -n "modules=" > $GITHUB_OUTPUT |
| 29 | + for m in ./*/*go.mod; do |
| 30 | + pushd $(dirname "$m") > /dev/null |
| 31 | + module_path="$(go list -m -json | jq --slurp '.' | jq --compact-output --raw-output '.[].Dir')" |
| 32 | + echo $module_path |
| 33 | + popd > /dev/null |
| 34 | + done | jq --raw-input --slurp 'split("\n")' | jq --compact-output 'map(select(length > 0))' >> $GITHUB_OUTPUT |
| 35 | +
|
| 36 | + linter-pull-request: |
| 37 | + needs: detect-modules |
| 38 | + name: golangci-lint on a PR or from a tag |
| 39 | + runs-on: ubuntu-22.04 |
| 40 | + strategy: |
| 41 | + matrix: |
| 42 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 43 | + if: github.ref != 'refs/heads/master' |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + # We need to define the fetch-depth to 0 so that we can get the commit ID of the master branch |
| 48 | + fetch-depth: 0 |
| 49 | + - uses: actions/setup-go@v5 |
| 50 | + with: |
| 51 | + go-version: stable |
| 52 | + check-latest: true |
| 53 | + - name: Get golangci-lint configuration file |
| 54 | + run: wget --output-document=$(pwd)/.golangci.yml https://sc-devtools.s3.eu-west-1.amazonaws.com/golang-ci/golangci.yml |
| 55 | + - name: Get master branch commit ID |
| 56 | + id: new-from-rev |
| 57 | + run: echo "NEW_FROM_REV=$( git rev-parse origin/master )" >> "$GITHUB_OUTPUT" |
| 58 | + - name: "Execute golangci-lint on a pull request" |
| 59 | + uses: golangci/golangci-lint-action@v6 |
| 60 | + with: |
| 61 | + working-directory: ${{ matrix.modules }} |
| 62 | + # The `only-new-issues` flag is not working (https://github.com/golangci/golangci-lint-action/issues/531). |
| 63 | + # We rather decided to use the suggestion from the FAQ (https://golangci-lint.run/welcome/faq/#how-to-integrate-golangci-lint-into-large-project-with-thousands-of-issues) and use `--new-from-rev` |
| 64 | + # only-new-issues: false |
| 65 | + args: "--config=$(pwd)/../.golangci.yml --new-from-rev=${{ steps.new-from-rev.outputs.NEW_FROM_REV }} --modules-download-mode=mod" |
| 66 | + |
| 67 | + |
| 68 | + tests: |
| 69 | + needs: detect-modules |
| 70 | + name: Unit Tests |
| 71 | + runs-on: ubuntu-22.04 |
| 72 | + strategy: |
| 73 | + matrix: |
| 74 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 75 | + steps: |
| 76 | + - uses: actions/checkout@v4 |
| 77 | + - uses: actions/setup-go@v5 |
| 78 | + with: |
| 79 | + go-version: stable |
| 80 | + check-latest: true |
| 81 | + - name: Execute the tests |
| 82 | + working-directory: ${{ matrix.modules }} |
| 83 | + run: go test -race ./... |
0 commit comments