Skip to content

Commit

Permalink
ci: list modules and pass it to test matrix
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <[email protected]>
  • Loading branch information
mkungla committed Jan 29, 2024
1 parent dc59f3c commit c2ff700
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .github/actions/go-test-monorepo-action/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
25 changes: 20 additions & 5 deletions .github/actions/go-test-monorepo-action/go-test-monorepo-action.sh
Original file line number Diff line number Diff line change
@@ -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
51 changes: 17 additions & 34 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2ff700

Please sign in to comment.