From c2ff70079564d64e00527861755104bd1f1f078f Mon Sep 17 00:00:00 2001 From: Marko Kungla Date: Mon, 29 Jan 2024 20:54:48 +0200 Subject: [PATCH] ci: list modules and pass it to test matrix Signed-off-by: Marko Kungla --- .../go-test-monorepo-action/action.yml | 5 +- .../go-test-monorepo-action.sh | 25 +++++++-- .github/workflows/main.yml | 51 +++++++------------ 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/.github/actions/go-test-monorepo-action/action.yml b/.github/actions/go-test-monorepo-action/action.yml index 4f289cf..bea0b3a 100644 --- a/.github/actions/go-test-monorepo-action/action.yml +++ b/.github/actions/go-test-monorepo-action/action.yml @@ -4,6 +4,9 @@ inputs: go-version: description: 'Go version' default: '1.21.5' + only-module-list: + description: 'Only outputs module list without running tests' + default: 'false' outputs: modules: description: "List of Go modules" @@ -17,5 +20,5 @@ runs: go-version: ${{ inputs.go-version }} - name: Run tests and generate coverage for all modules id: coveredmodules - run: ./.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh + run: ./.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh ${{ inputs.only-module-list }} shell: bash diff --git a/.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh b/.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh index 170add1..e85f73e 100755 --- a/.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh +++ b/.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh @@ -1,21 +1,36 @@ #!/bin/bash +# Check if only module list should be outputted +ONLY_MODULE_LIST="$1" + # Array to hold module paths modules=() # Find all directories containing a go.mod file while IFS= read -r module; do - modules+=("$module") - echo "Testing and generating coverage for module: $module" - (cd "$module" && go test -race -coverpkg=./... -coverprofile=coverage.out -timeout=1m ./...) + modules+=("$module") + + # Run tests only if ONLY_MODULE_LIST is not "true" + if [ "$ONLY_MODULE_LIST" != "true" ]; then + echo "Testing and generating coverage for module: $module" + (cd "$module" && go test -race -coverpkg=./... -coverprofile=coverage.out -timeout=1m ./...) + fi done < <(find . -type f -name 'go.mod' -exec dirname {} \;) # Convert modules array to JSON array format modules_json=$(printf '%s\n' "${modules[@]}" | jq -R . | jq -cs .) +# Check if GITHUB_OUTPUT is set, else use /dev/null if [ -z "$GITHUB_OUTPUT" ]; then - GITHUB_OUTPUT="/dev/null" + GITHUB_OUTPUT="/dev/null" fi + # Output the modules for the matrix echo "modules=$modules_json" >> $GITHUB_OUTPUT -echo "${modules_json}" + +# If running in GitHub Actions, set info message +if [ "$GITHUB_ACTIONS" == "true" ]; then + echo "::info::Monorepo modules: $modules_json" +else + echo "${modules_json}" +fi diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 844c0e9..cd150a6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,53 +26,36 @@ jobs: go-version: '>=1.21.0' golangci-lint-version: latest - test: + list-modules: needs: lint runs-on: ubuntu-latest outputs: - modules: ${{ steps.coveredmodules.outputs.modules }} + modules: ${{ steps.modulelist.outputs.modules }} steps: - uses: actions/checkout@v4 - - name: Test all packages - id: coveredmodules + - name: Get monorepo modules + id: modulelist uses: ./.github/actions/go-test-monorepo-action with: go-version: '>=1.21.0' - - run: echo modules "${{ steps.coveredmodules.outputs.modules }}" - shell: bash + only-module-list: true - upload-coverage: - needs: test + test-modules: + needs: list-modules if: ${{ always() }} runs-on: ubuntu-latest + strategy: + matrix: + module: ${{ fromJson(needs.list-modules.outputs.modules) }} + fail-fast: false steps: - uses: actions/checkout@v4 - - name: Upload coverage - run: echo "${{ fromJson(needs.test.outputs.modules) }}" -# test: - # needs: lint - # runs-on: ubuntu-latest - # strategy: - # matrix: - # package: - # - ./ - # - pkg/cli/ansicolor - # - pkg/devel/testutils - # - pkg/scheduling/cron - # - pkg/strings/bexp - # - pkg/vars - # - pkg/version - # fail-fast: false - # steps: - # - name: Set up Go - # uses: actions/setup-go@v5 - # with: - # go-version: '>=1.21.5' - # - uses: actions/checkout@v4 - # - name: generate coverage - # continue-on-error: true - # working-directory: ./${{ matrix.package }} - # run: go test -race -coverpkg=./... -coverprofile=coverage.out ./... + - name: Run tests and generate coverage reports + id: modulelist + uses: ./.github/actions/go-test-monorepo-action + with: + go-version: '>=1.21.0' + # - name: Check if coverage data exists # if: success() || failure() # id: check-coverage