You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI中Schemathesis任务执行失败问题求助

问题原因及解决方案

核心错误原因

1. 跨Job网络隔离导致无法访问应用

GitLab CI的Kubernetes执行器会为每个Job分配独立的Pod,Pod之间属于不同的网络命名空间,api-tests任务中访问http://127.0.0.1:8081只能指向自身Pod,无法连接到start-api任务所在Pod的应用。

2. Schemathesis容器启动配置错误

你设置了entrypoint: [''],但GitLab Runner需要容器提供可交互的shell环境来执行脚本命令。Schemathesis镜像默认entrypoint为st命令,清空entrypoint后容器无默认启动逻辑,导致Runner无法连接到容器,触发container not found ("build")错误。

修复方案

方案一:合并任务到同一个Pod(推荐)

将应用启动和API测试放在同一个Job中,确保localhost互通,同时避免容器配置问题:

api-tests:
  stage: test
  image: maven:3.6.3-jdk-11
  tags:
    - aks
  script:
    # 后台启动Spring Boot应用
    - mvn spring-boot:run &
    # 等待应用完全启动(可替换为curl健康检查,比如`until curl -s http://127.0.0.1:8081/actuator/health; do sleep 5; done`)
    - sleep 60
    # 安装Schemathesis
    - pip install schemathesis
    # 执行API测试
    - st run ./api.yml --checks=all --report --base-url=http://127.0.0.1:8081/api/v3/

方案二:保留独立Job并配置网络访问(复杂)

如果必须分开任务,需要:

  • 修改start-api的应用配置,让服务监听0.0.0.0(允许Pod外部访问)
  • 在GitLab CI中为start-api任务配置Kubernetes Service,暴露应用端口
  • 在api-tests任务中使用Service的DNS名称作为base-url,例如http://start-api-service:8081/api/v3/
  • 修正Schemathesis容器的entrypoint配置,指定shell环境:
    api-tests:
      stage: test
      image:
        name: schemathesis/schemathesis:stable
        entrypoint: ['/bin/sh', '-c']
      needs:
        - job: start-api
      tags:
        - aks
      script:
        - sleep 30
        - st run ./api.yml --checks=all --report --base-url=http://start-api-service:8081/api/v3/
    

内容的提问来源于stack exchange,提问作者kmanka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:23:15