我已经配置了以下 github actions 文件
name: Python application
on:
push:
branches: [ "app" ]
pull_request:
branches: [ "app" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
make install
- name: Lint with pylint
run: |
make lint
- name: Start Fastapi service
run: |
nohup make serve
- name: Test with pytest
run: |
make test
这里的Makefile如下
install:
pip install -r requirements.txt
lint:
pylint --disable=R,C *.py
serve:
python app.py
test:
python -m pytest -vv test_app.py
问题是,当我运行 makeserve 命令时,服务器启动,然后无限地继续,永远不会到达测试部分。如何配置我的操作工作流程,以便服务器在后端运行并开始测试。我尝试过使用nohup,但没有成功。我还能做什么?
我尝试使用 nohup 命令。但下一个命令不会自行提示。它会一直等到服务器必须关闭。-
您遇到的问题是,当您运行命令 nohup makeserve 时,它会在后台启动 FastAPI 服务器,但不会立即将控制权释放回 GitHub Actions 工作流程。由于服务器无限期地运行,因此后续的 make test 命令不会按预期执行。
要解决此问题,您可以以允许 GitHub Actions 工作流程继续执行后续步骤(例如运行测试)的方式运行 FastAPI 服务。
name: Python application
on:
push:
branches: [ "app" ]
pull_request:
branches: [ "app" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
make install
- name: Start FastAPI service
run: |
make serve &
sleep 5 # Give the server some time to start
- name: Wait for FastAPI service to start
run: |
for i in {1..10}; do
if curl -s http://localhost:8000/; then
echo "Service is up!"
break
fi
echo "Waiting for service to start..."
sleep 5
done
- name: Test with pytest
run: |
make test
进行这些更改后,您的服务器应该在后台运行,以便随后执行测试。