How to run a command in background in Windows GitHub Action

I would like to serve local files through a web-server and execute some tests inside GitHub Action that runs Windows, but I cannot make it run server in the background

Here’s my setup:

name: CI Windows

on:
  push:
    branches:
    - '*'

jobs:
  build:
    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v1

    - uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    - name: Test local api
      run: |
        cmd /c "START /b run-local-api-server.bat"
        curl -X GET "http://localhost:9195/README.md"

And run-local-api-server.bat script:

@echo off

python3 -m http.server 9195

The error I get is curl: (7) Failed to connect to localhost port 9195: Connection refused.

Links:

This seems to be a timing issue:

  • The script that starts the server is run
  • Curl tries to fetch a file
  • The Python webserver is ready to accept requests

As you can see, the server isn’t ready in time for Curl, either because the Batch script is still being run or because the Python server is bootstrapping.

If you put a sleep 5 between cmd ... and curl ... then it should work fine. You can try to reduce the wait time of 5 seconds, but 1 second was too short in my test.