|
| 1 | +name: Tests and Linters Scalingo go-utils Packages |
| 2 | + |
| 3 | +on: [push] |
| 4 | + |
| 5 | +jobs: |
| 6 | + detect-modules: |
| 7 | + name: Detect the Go modules declared in go-utils |
| 8 | + runs-on: ubuntu-22.04 |
| 9 | + outputs: |
| 10 | + modules: ${{ steps.set-modules.outputs.modules }} |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + # We need to define the fetch-depth to 0 so that we can get the commit ID of the master branch |
| 15 | + fetch-depth: 0 |
| 16 | + - uses: actions/setup-go@v5 |
| 17 | + with: |
| 18 | + go-version: stable |
| 19 | + check-latest: true |
| 20 | + - id: set-modules |
| 21 | + run: | |
| 22 | + echo -n "modules=" > $GITHUB_OUTPUT |
| 23 | + for m in ./*/*go.mod; do |
| 24 | + pushd $(dirname "$m") > /dev/null |
| 25 | + module_path="$(go list -m -json | jq --slurp '.' | jq --compact-output --raw-output '.[].Dir')" |
| 26 | + echo $module_path |
| 27 | + popd > /dev/null |
| 28 | + done | jq --raw-input --slurp 'split("\n")' | jq --compact-output 'map(select(length > 0))' >> $GITHUB_OUTPUT |
| 29 | +
|
| 30 | + linter-pull-request: |
| 31 | + needs: detect-modules |
| 32 | + name: golangci-lint on a PR or from a tag |
| 33 | + runs-on: ubuntu-22.04 |
| 34 | + strategy: |
| 35 | + matrix: |
| 36 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 37 | + if: github.ref != 'refs/heads/master' |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + # We need to define the fetch-depth to 0 so that we can get the commit ID of the master branch |
| 42 | + fetch-depth: 0 |
| 43 | + - uses: actions/setup-go@v5 |
| 44 | + with: |
| 45 | + go-version: stable |
| 46 | + check-latest: true |
| 47 | + - name: Get golangci-lint configuration file |
| 48 | + run: wget --output-document=$(pwd)/.golangci.yml https://sc-devtools.s3.eu-west-1.amazonaws.com/golang-ci/golangci.yml |
| 49 | + - name: Get master branch commit ID |
| 50 | + id: new-from-rev |
| 51 | + run: echo "NEW_FROM_REV=$( git show-branch --merge-base FETCH_HEAD )" >> "$GITHUB_OUTPUT" |
| 52 | + - name: "Execute golangci-lint on a pull request" |
| 53 | + uses: golangci/golangci-lint-action@v8 |
| 54 | + with: |
| 55 | + working-directory: ${{ matrix.modules }} |
| 56 | + # The `only-new-issues` flag is not working (https://github.com/golangci/golangci-lint-action/issues/531). |
| 57 | + # 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` |
| 58 | + # only-new-issues: false |
| 59 | + args: "--config=$(pwd)/../.golangci.yml --new-from-rev=${{ steps.new-from-rev.outputs.NEW_FROM_REV }} --modules-download-mode=mod" |
| 60 | + |
| 61 | + linter-master: |
| 62 | + needs: detect-modules |
| 63 | + name: golangci-lint on the master branch |
| 64 | + runs-on: ubuntu-22.04 |
| 65 | + strategy: |
| 66 | + matrix: |
| 67 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 68 | + if: github.ref == 'refs/heads/master' |
| 69 | + steps: |
| 70 | + - uses: actions/checkout@v4 |
| 71 | + with: |
| 72 | + # We need to define the fetch-depth to 0 so that we can get the commit ID of the master branch |
| 73 | + fetch-depth: 0 |
| 74 | + - uses: actions/setup-go@v5 |
| 75 | + with: |
| 76 | + go-version: stable |
| 77 | + check-latest: true |
| 78 | + - name: Get golangci-lint configuration file |
| 79 | + run: wget --output-document=$(pwd)/.golangci.yml https://sc-devtools.s3.eu-west-1.amazonaws.com/golang-ci/golangci.yml |
| 80 | + - name: "Execute golangci-lint on a pull request" |
| 81 | + uses: golangci/golangci-lint-action@v8 |
| 82 | + with: |
| 83 | + working-directory: ${{ matrix.modules }} |
| 84 | + args: "--config=$(pwd)/../.golangci.yml --new-from-rev=HEAD~1 --modules-download-mode=mod" |
| 85 | + |
| 86 | + tests: |
| 87 | + needs: detect-modules |
| 88 | + name: Unit Tests |
| 89 | + runs-on: ubuntu-22.04 |
| 90 | + strategy: |
| 91 | + matrix: |
| 92 | + modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }} |
| 93 | + services: |
| 94 | + mongodb: |
| 95 | + image: mongo:4.0.3 |
| 96 | + env: |
| 97 | + MONGO_INITDB_ROOT_USERNAME: admin |
| 98 | + MONGO_INITDB_ROOT_PASSWORD: admin |
| 99 | + MONGO_INITDB_DATABASE: mydb |
| 100 | + ports: |
| 101 | + - 27017:27017 |
| 102 | + options: >- |
| 103 | + --health-cmd "echo \"db.runCommand({ ping: 1 }).ok\" | mongo" |
| 104 | + --health-interval 10s |
| 105 | + --health-timeout 5s |
| 106 | + --health-retries 5 |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v4 |
| 109 | + - uses: actions/setup-go@v5 |
| 110 | + with: |
| 111 | + go-version: stable |
| 112 | + check-latest: true |
| 113 | + # For some reasons I failed to understand, the admin user we created failed to connect to the MongoDB. Hence we install MongoDB Shell and create a user that is later used by the tests execution. |
| 114 | + - name: Install MongoDB Shell |
| 115 | + # `matrix.modules` contains something like `/home/runner/work/go-utils/go-utils/mongo`. |
| 116 | + if: endsWith(matrix.modules, '/mongo') |
| 117 | + # TODO we need to install a MongoDB Shell version which uses the Node.js driver v6.15.0, latest version which supports MongoDB 4.0. This is version 2.5.0. |
| 118 | + run: | |
| 119 | + sudo apt-get install gnupg |
| 120 | + wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-7.0.asc |
| 121 | + echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list |
| 122 | + sudo apt-get update |
| 123 | + # apt list --all-versions mongodb-mongosh |
| 124 | + sudo apt-get install --yes mongodb-mongosh=2.5.0 |
| 125 | + mongosh --version |
| 126 | + - name: Create a User for Tests Execution |
| 127 | + if: endsWith(matrix.modules, '/mongo') |
| 128 | + run: | |
| 129 | + mongosh --host localhost:27017 -u admin -p admin --authenticationDatabase admin <<EOF |
| 130 | + db.runCommand({ ping: 1 }); |
| 131 | + db = db.getSiblingDB('mydb'); |
| 132 | + db.createUser({ |
| 133 | + user: 'mgouser', |
| 134 | + pwd: 'mgopassword', |
| 135 | + roles: [{ role: 'readWrite', db: 'mydb' }] |
| 136 | + }); |
| 137 | + EOF |
| 138 | + - name: Execute the Tests |
| 139 | + working-directory: ${{ matrix.modules }} |
| 140 | + run: go test -v -race ./... |
| 141 | + env: |
| 142 | + MONGO_URL: "mongodb://mgouser:mgopassword@localhost:27017/mydb?connect=direct" |
0 commit comments