在 Windows 上分多个步骤运行后台进程

问题描述 投票:0回答:1

在我下面的工作中,我尝试在一个步骤中本地启动一台服务器,并在下一步中连接到该服务器。这在 Linux 上工作得很好,但在 Windows 上,看起来所有后台进程都在执行下一步之前被终止。在名为 再次连接到服务器 (Windows) 的步骤上失败。

有什么解决办法吗?


name: Test

on:
  push:
    branches:
      - '*'
  workflow_dispatch:

jobs:
  micro-server:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]

    steps:
      - name: Create index.html
        run: echo "<h1>Hello world!</h1>" > index.html

      - name: Install dotnet-serve
        run: dotnet tool install --global dotnet-serve

      - name: Start server and connect to server (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: |
            $env:RUNNER_TRACKING_ID=""
            $web = Start-Job { dotnet serve --port 5100 } -WorkingDirectory $pwd
            
            $timeout = 30
            $startTime = Get-Date

            do {
                $elapsedTime = (Get-Date) - $startTime
                if ($elapsedTime.TotalSeconds -ge $timeout) {
                    Write-Host "Timeout reached. Server not available after $($elapsedTime.TotalSeconds) seconds."
                    exit 1
                }

                try {
                    $response = Invoke-WebRequest -Uri "http://localhost:5100" -UseBasicParsing -Method Head -TimeoutSec 2
                    $serverAvailable = $response.StatusCode -eq 200
                } catch {
                    $serverAvailable = $false
                }

                if (-not $serverAvailable) {
                    Write-Host "Waiting for server to become available... Elapsed time: $($elapsedTime.ToString('hh\:mm\:ss'))"
                    Start-Sleep -Seconds 1  # Wait for 1 second before checking again
                }
            } while (-not $serverAvailable)

            $totalWaitTime = (Get-Date) - $startTime
            Write-Host "Server is now available after $($totalWaitTime.ToString('hh\:mm\:ss'))."

            Get-Process | Where-Object { $_.Modules.FileName -like "*dotnet*" }

      - name: List dotnet processes (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: |
            Get-Process | Where-Object { $_.Modules.FileName -like "*dotnet*" }

      - name: Connect to server again (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: |
            Invoke-WebRequest -Uri "http://localhost:5100" -UseBasicParsing -Method Head -TimeoutSec 2

 
      - name: Start server and connect to server (Linux)
        if: ${{ matrix.os != 'windows-latest' }}
        run: |
            dotnet serve --port 5100 &
            sleep 5
            curl http://localhost:5100

      - name: Connect to server again (Linux)
        if: ${{ matrix.os != 'windows-latest' }}
        run: |
            curl http://localhost:5100

github-actions
1个回答
0
投票

解决方案似乎是在 Windows 上也使用 bash。服务器将在以下步骤中可用。

name: Test

on:
  push:
    branches:
      - '*'
  workflow_dispatch:

jobs:
  micro-server:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]

    steps:
      - name: Create index.html
        run: echo "<h1>Hello world!</h1>" > index.html

      - name: Install dotnet-serve
        run: dotnet tool install --global dotnet-serve
 
      - name: Start server and connect to server 
        shell: bash
        run: |
            dotnet serve --port 5100 &
            BASE_URL=http://localhost:5100/
            
            timeout=30  # Timeout in seconds
            start_time=$(date +%s)  # Get current timestamp in seconds

            while true; do
                current_time=$(date +%s)
                elapsed_time=$((current_time - start_time))

                if (( elapsed_time >= timeout )); then
                    echo "Timeout reached. Server not available after $elapsed_time seconds."
                    exit 1  # Exit with non-zero status indicating failure
                fi

                # Check if server is reachable with curl
                if curl -f $BASE_URL &> /dev/null; then
                    echo "Server is now available at $BASE_URL after $elapsed_time seconds."
                    break
                else
                    echo "Waiting for server to become available at $BASE_URL... Elapsed time: $elapsed_time seconds"
                    sleep 1  # Wait for 1 second before checking again
                fi
            done

      - name: Connect to server again
        run: |
            curl http://localhost:5100
© www.soinside.com 2019 - 2024. All rights reserved.