Github Workflow推送失败但本地运行正常的技术问题求助
问题排查:GitHub Actions中Docker化API测试失败
我有一个包含API及其他服务的项目,已编写Unittest文件,用于推送到Github仓库时测试API健康状态,测试文件运行在独立Docker容器中,API也部署在容器内。本地执行docker-compose up api-test-runner时测试完全正常,但推送到Github仓库后Workflow执行失败,返回500服务器错误。
当前Workflow存在的问题
- 健康检查步骤陷入循环,API服务始终未就绪
- 测试结果检查步骤无法捕获错误,即使测试返回非0退出码也会通过
Workflow文件内容
# This is a basic workflow to help you get started with Actions name: API Test # Define in which situiations and on what branches the workflow should be ran. on: push: branches: - "*" pull_request: branches: - "*" jobs: api-test: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 # Build the docker image - name: Build Docker images run: docker-compose up --build -d api db # Health check to make sure the application is ready to accept requests - name: Wait for application to be ready run: | until curl --silent --fail http://localhost:4000/health; do echo 'Waiting for application to be ready...' sleep 5 done # Run the python test script in an isolated docker container - name: Run API tests run: docker-compose up api_test_runner # Checks that results of tests passed, if not, throw error. - name: Check test results run: if [ $? -ne 0 ]; then exit 1; fi
Compose.yaml文件内容
# Instructions for running the application # ======== Basic app startup ======== # 1. start application services: ´docker-compose up api db -d´ # ======== Running API test locally ======== # (Note: application services have to be running before running the tests) # # 1. start application services: ´docker-compose up api db -d´ # 2. run tests: ´docker-compose up api_test_runner´ version: '3.9' services: # <======== Application services ========> # ======== API container ======== api: container_name: api build: context: ./app dockerfile: Dockerfile image: justingav/d0020e:latest environment: - DB_URL=postgresql://postgres:postgres@db:5432/postgres depends_on: - db volumes: - ./app:/app ports: # ports being listened on - "4000:4000" # ================================ # ====== Database container ====== db: container_name: db image: postgres:latest #volumes: # - /home/upload/data:/var/lib/postgresql/data ports: - "5432:5432" environment: - POSTGRES_DB=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres volumes: - postgres_data:/var/lib/postgresql/data # ================================ # <========= Test runner service =========> api_test_runner: container_name: api_test_runner build: context: ./api_test_runner dockerfile: Dockerfile volumes: - ./api_test_runner:/api_test_runner depends_on: - db - api # run tests on startup command: [ "python", "-m", "unittest", "api_test" ] # ================================ volumes: postgres_data: {}
已尝试的排查动作
- 增加健康检查步骤等待API就绪,但步骤陷入循环,API始终未就绪
- 查看Docker日志,未找到有效错误线索
- 在API启动代码中添加异常捕获逻辑:
try: # Your existing code to start the Flask application logging.info('Starting application...') api.run(host='0.0.0.0', port=4000) except Exception as e: logging.exception('Failed to start application')
需求
目前推测API文件和测试文件无问题,但仍需确认,寻求可行的问题排查方案。
内容的提问来源于stack exchange,提问作者Jay
相关产品推荐
相关产品推荐

