我想使用 Gitlab CI/CD 管道和 docker-compose 為我的簡單 API 自動化測驗程序。我有要在構建應用程式容器時運行的測驗,問題是在 http://app:80 地址上運行測驗之前我無法等待應用程式服務。
專案結構:
project:
-- app
-- tests
-- docker-compose.yml
-- .gitlab-ci.yml
我擁有的:
docker-compose:
version: "3.0"
services:
app:
build:
context: ./app
dockerfile: Dockerfile
ports:
- "81:80"
volumes:
- ./app:/app/app
tests:
build:
context: ./tests
dockerfile: Dockerfile
postgres:
image: postgres:12-alpine
ports:
- "5432"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASS}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- ./app/data:/var/lib/postgresql/data
tests/ dir with files:
def test_select():
url = f"{HOST}/select/"
response = requests.get(url)
status_code = response.status_code
result_len = len(response.json().get("result"))
assert status_code == 200
assert result_len != 0
.gitlab-ci.yml:
stages:
- build
build:
stage: build
script:
- sudo mkdir -p /home/app/
- sudo cp -r $PWD/* /home/app/
- cd /home/app/
- docker-compose up -d
最終目標是在 docker-compose 構建完成之前運行測驗,如果某些測驗失敗,那么 docker-compose 將失敗,管道也會失敗。
這可能嗎,如果有另一種方法可以解決這個問題,我將不勝感激。
uj5u.com熱心網友回復:
有一些解決方案具有不同的復雜程度:
- 為容器的啟動添加足夠長的等待時間
- 將重試邏輯(最好帶有退避)添加到在容器內運行的代碼中
- 依賴一個中間容器,該容器的邏輯負責確保其他依賴項完全可用且正常運行。
不過,我認為您的問題是您只是depends_on在 docker-compose.yml 中缺少宣告。還要確保您的app影像具有正確的EXPOSE宣告或將它們添加到撰寫檔案中。
此外,由于您在 docker 網路中運行測驗,因此不需要埠映射。您可以直接在其暴露的埠上聯系該服務。
app:
ports:
- "80"
tests:
depends_on: # IMPORTANT! Waits for app to be available
- app # makes sure you can talk to app on the network
# ...
那么你的測驗應該能夠達到 http://app
作為使用公共專案的完整示例:
version: "3"
services:
app:
image: strm/helloworld-http
ports:
- "80" # not strictly needed since this image has EXPOSE 80
tests:
depends_on:
- app
image: curlimages/curl
command: "curl http://app"
如果您運行,docker-compose up您會看到以下輸出:
Creating testproj_app_1 ... done
Creating testproj_tests_1 ... done
Attaching to testproj_app_1, testproj_tests_1
app_1 | 172.18.0.3 - - [12/Nov/2021 03:03:46] "GET / HTTP/1.1" 200 -
tests_1 | % Total % Received % Xferd Average Speed Time Time Time Current
tests_1 | Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<html><head><title>HTTP Hello World</title></head><body><h1>Hello from d8
f6894ccd1e</h1></body></html
100 102 100 102 0 0 4706 0 --:--:-- --:--:-- --:--:-- 4857
testproj_tests_1 exited with code 0
您也可以選擇使用 gitlab,services:但是,如果您已經使用 docker -compose 在本地進行了作業流測驗,那么這不太理想,因為您在本地進行測驗的方式與在 GitLab 中的測驗方式不同,然后該測驗方法不可移植到其他 CI 系統或其他開發人員的本地環境。
uj5u.com熱心網友回復:
通常您不會docker-compose.yml從管道內開始。您docker-compose.yml對本地開發很有用,但在管道中您必須使用不同的方法,使用 GitLab 服務:https ://docs.gitlab.com/ee/ci/services/
但是,如果您想從 GitLab 管道進行 E2E 或負載測驗您的 API,您可以使用它的服務來公開例如 postgress 資料庫:
test:e2e:
image: ubuntu:20.04
stage: test
services:
- name: postgres:12-alpine
alias: postgress
script:
- curl http://postgress:5432 # should work!
接下來的步驟是在分離模式下啟動你的 api。例如:
script:
- python my-app.py &
- sleep 30
- # your app should be app now and should be exposed on let's say localhost:81 according your specs. You can safely run your API tests here
Notepython將無法開箱即用。為此,您必須將其安裝在管道中或創建在管道中使用的 docker 映像。我個人總是在 GitLab 管道中使用自定義 docker 鏡像來防止 Docker 速率限制。我有一個個人專案的例子來創建自定義影像并將它們存盤在 GitLab 中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357378.html
標籤:Python 码头工人 测试 docker-compose gitlab-ci
