From 51cf3bde1282b567c25898e0f96a124f4c97855e Mon Sep 17 00:00:00 2001 From: Marko Kungla Date: Mon, 29 Jan 2024 22:03:56 +0200 Subject: [PATCH] ci: add go-test-monorepo-action windows support Signed-off-by: Marko Kungla --- .../go-test-monorepo-action/action.yml | 15 +++++++++- .../go-test-monorepo-action.ps1 | 30 +++++++++++++++++++ .../workflows/{main.yml => monorepo-main.yml} | 26 ++++++++-------- 3 files changed, 57 insertions(+), 14 deletions(-) create mode 100755 .github/actions/go-test-monorepo-action/go-test-monorepo-action.ps1 rename .github/workflows/{main.yml => monorepo-main.yml} (86%) diff --git a/.github/actions/go-test-monorepo-action/action.yml b/.github/actions/go-test-monorepo-action/action.yml index bea0b3a..f353848 100644 --- a/.github/actions/go-test-monorepo-action/action.yml +++ b/.github/actions/go-test-monorepo-action/action.yml @@ -18,7 +18,20 @@ runs: uses: actions/setup-go@v5 with: go-version: ${{ inputs.go-version }} + + - name: Determine OS + id: determine-os + run: echo "::set-output name=os::$(echo $RUNNER_OS | tr '[:upper:]' '[:lower:]')" + - name: Run tests and generate coverage for all modules id: coveredmodules - run: ./.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh ${{ inputs.only-module-list }} + run: | + if [[ "${{ steps.determine-os.outputs.os }}" == "linux" ]]; then + ./.github/actions/go-test-monorepo-action/go-test-monorepo-action.sh ${{ inputs.only-module-list }} + elif [[ "${{ steps.determine-os.outputs.os }}" == "windows" ]]; then + pwsh -File ./.github/actions/go-test-monorepo-action/go-test-monorepo-action.ps1 ${{ inputs.only-module-list }} + else + echo "Unsupported OS" + exit 1 + fi shell: bash diff --git a/.github/actions/go-test-monorepo-action/go-test-monorepo-action.ps1 b/.github/actions/go-test-monorepo-action/go-test-monorepo-action.ps1 new file mode 100755 index 0000000..5706e94 --- /dev/null +++ b/.github/actions/go-test-monorepo-action/go-test-monorepo-action.ps1 @@ -0,0 +1,30 @@ +# Check if only module list should be outputted +$ONLY_MODULE_LIST = $args[0] + +# Array to hold module paths +$modules = @() + +# Find all directories containing a go.mod file +Get-ChildItem -File -Recurse -Filter "go.mod" | ForEach-Object { + $module = $_.DirectoryName + $modules += $module + + # Run tests only if ONLY_MODULE_LIST is not "true" + if ($ONLY_MODULE_LIST -ne "true") { + Write-Host "Testing and generating coverage for module: $module" + Set-Location $module + go test -race -coverpkg=./... -coverprofile=coverage.out -timeout=1m ./... + Set-Location .. + } +} + +# Convert modules array to JSON array format +$modules_json = $modules | ConvertTo-Json + +# Output the modules for the matrix +Write-Host "modules=$modules_json" + +# If running in GitHub Actions, set info message +if ($env:GITHUB_ACTIONS -eq "true") { + Write-Host "::info::Monorepo modules: $modules_json" +} diff --git a/.github/workflows/main.yml b/.github/workflows/monorepo-main.yml similarity index 86% rename from .github/workflows/main.yml rename to .github/workflows/monorepo-main.yml index 9d4ebcb..5b0aedc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/monorepo-main.yml @@ -21,12 +21,12 @@ jobs: steps: - uses: actions/checkout@v4 - name: Lint all packages - uses: ./.github/actions/golangci-lint-monorepo-action + uses: ./.github/actions/golangci-lint-action with: go-version: '>=1.21.0' golangci-lint-version: latest - list-monorepo-modules: + list-modules: needs: lint runs-on: ubuntu-latest outputs: @@ -36,24 +36,24 @@ jobs: - name: Get monorepo modules id: modulelist - uses: ./.github/actions/go-test-monorepo-action + uses: ./.github/actions/go-test-action with: go-version: '>=1.21.0' only-module-list: true - monorepo-coverage-linux: - needs: list-monorepo-modules + coverage-linux: + needs: list-modules runs-on: ubuntu-latest strategy: matrix: - module: ${{ fromJson(needs.list-monorepo-modules.outputs.modules) }} + module: ${{ fromJson(needs.list-modules.outputs.modules) }} fail-fast: false steps: - uses: actions/checkout@v4 - name: Run tests and generate coverage reports id: modulelist - uses: ./.github/actions/go-test-monorepo-action + uses: ./.github/actions/go-test-action with: go-version: '>=1.21.0' @@ -88,19 +88,19 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} file: ${{ matrix.module }}/coverage.out - monorepo-coverage-windows: - needs: list-monorepo-modules + coverage-windows: + needs: list-modules runs-on: windows-latest strategy: matrix: - module: ${{ fromJson(needs.list-monorepo-modules.outputs.modules) }} + module: ${{ fromJson(needs.list-modules.outputs.modules) }} fail-fast: false steps: - uses: actions/checkout@v4 - name: Run tests and generate coverage reports on Windows id: modulelist-windows - uses: ./.github/actions/go-test-monorepo-action + uses: ./.github/actions/go-test-action with: go-version: '>=1.21.0' platform: windows @@ -138,8 +138,8 @@ jobs: finish-coverage-report: needs: - - monorepo-coverage-linux - - monorepo-coverage-windows + - coverage-linux + - coverage-windows if: ${{ always() }} runs-on: ubuntu-latest steps: