[英]How to setup Gitlab CI E2E tests using Multiple dockers
I am a bit lost with the automated testing using Gitlab CI.我对使用 Gitlab CI 的自动化测试有点迷失了。 I hope I can explain my problem so somebody can help me.
我希望我能解释我的问题,以便有人可以帮助我。 I'll try to explain the situation first, after which I'll try to ask a question (which is harder than it sounds)
我会先试着解释一下情况,然后我会试着问一个问题(这比听起来更难)
For the 2 API's, there is a Docker
and a docker-compose
file.对于 2 个 API,有一个
Docker
和一个docker-compose
文件。 These work fine and are set up correctly.这些工作正常并且设置正确。
We are using GitLab for the CI/CD, there we have the following stages in this order:我们将 GitLab 用于 CI/CD,我们按此顺序有以下阶段:
I want to set up the GitLab CI such that it runs the cypress tests.我想设置 GitLab CI 以便它运行柏树测试。 But for this, all build dockers are needed.
但为此,需要所有构建 docker。 Currently, I am not able to use all dockers together when performing the end-to-end tests.
目前,在执行端到端测试时,我无法同时使用所有 docker。
I don't really get how I would achieve this.我真的不明白我将如何实现这一目标。
dind
?dind
吗? I hope somebody can give me some advice on how to achieve this.我希望有人能给我一些关于如何实现这一目标的建议。 An example would be even better but I don't know if somebody would want to do that.
一个例子会更好,但我不知道是否有人愿意这样做。
Thanks for taking the time to read!感谢您花时间阅读!
build-api:
image: docker:19
stage: build
services:
- docker:19-dind
script:
cd api
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
docker pull $IMAGE_TAG_API:latest || true
docker build -f ./Dockerfile --cache-from $IMAGE_TAG_API:latest --tag $IMAGE_TAG_API:$CI_COMMIT_SHA .
docker push $IMAGE_TAG_API:$CI_COMMIT_SHA
test-api:
image: docker:19
stage: test
services:
- postgres:12.2-alpine
- docker:19-dind
variables:
DB_NAME: project_ci_test
POSTGRES_HOST_AUTH_METHOD: trust
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker pull $IMAGE_TAG_API:$CI_COMMIT_SHA
- docker run $IMAGE_TAG_API:$CI_COMMIT_SHA sh -c "python manage.py test"
after_script:
- echo "Pytest tests complete"
coverage: "/TOTAL.+ ([0-9]{1,3}%)/"
release-api-staging:
image: docker:19
stage: release
services:
- docker:19-dind
only:
refs: [ master ]
changes: [ ".gitlab-ci.yml", "api/**/*" ]
environment:
name: staging
script:
- docker pull $IMAGE_TAG_API:$CI_COMMIT_SHA
- docker tag $IMAGE_TAG_API:$CI_COMMIT_SHA $IMAGE_TAG_API:latest
- docker push $IMAGE_TAG_API:latest
This article might shed some light.这篇文章可能会有所启发。 https://jessie.codes/article/running-cypress-gitlab-ci/ Essentially, you make two docker composers, one for your Cypress test and one for your items that are to be tested.
https://jessie.codes/article/running-cypress-gitlab-ci/基本上,您制作了两个 docker 作曲家,一个用于 Cypress 测试,一个用于您要测试的项目。 This gets around the issues with images being able to access node and docker.
这解决了图像能够访问节点和 docker 的问题。
The answer is a bit late, but still i'll try to explain the approach briefly.答案有点晚了,但我还是会尝试简要解释一下这种方法。
When e2e test full-stack application you have to either:当 e2e 测试全栈应用程序时,您必须:
As you noted, you want to spin-up the environment temporary in the pipeline.如您所述,您希望在管道中临时启动环境。 The following steps should be taken:
应采取以下步骤:
docker-compose.yml
services in 1 job in the pipeline;docker-compose.yml
服务;First you have to publish your docker images in the private registry of GitLab.首先,您必须在 GitLab 的私有注册表中发布您的 docker 映像。 You do this, because you now can reuse those images in another job.
您这样做是因为您现在可以在另一项工作中重复使用这些图像。 For this approach you need
docker:dind
.对于这种方法,您需要
docker:dind
。 A simple example job to publish to a private registry on gitlab looks like:发布到 gitlab 上的私有注册表的简单示例作业如下所示:
before_script:
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
publish:image:docker:
stage: publish
image: docker
services:
- name: docker:dind
alias: docker
variables:
CI_DOCKER_NAME: ${CI_REGISTRY_IMAGE}/my-docker-image
script:
- docker pull $CI_REGISTRY_IMAGE || true
- docker build --pull --cache-from $CI_REGISTRY_IMAGE --tag $CI_DOCKER_NAME --file Dockerfile .
- docker push $CI_DOCKER_NAME
only:
- master
To see a real-world example, I have an example project that is public available.要查看真实示例,我有一个公开的示例项目。
docker-compose.yml
services in 1 job in the pipelinedocker-compose.yml
服务Once you dockerized all backends and published the images on private registry, you can start to mimic your docker-compose.yml
with a GitLab job.将所有后端 docker 化并在私有注册表上发布图像后,您可以开始使用 GitLab 作业来模仿您的
docker-compose.yml
。 A basic example:一个基本的例子:
test:e2e:
image: ubuntu:20.04
stage: test
services:
- name: postgres:12-alpine
alias: postgress
- name: mongo
alias: mongo
# my backend image
- name: registry.gitlab.com/[MY_GROUP]/my-docker-image
alias: server
script:
- curl http://server:3000 # expecting server exposes on port 3000, this should work
- curl http://mongo:270117 # should work
- curl http://postgress:5432 # should work!
Now everything is running in a single job in GitLab, you can simply start your front-end in detached mode and run cypress to test it.现在一切都在 GitLab 中的单个作业中运行,您可以简单地以分离模式启动前端并运行 cypress 来测试它。 Example:
例子:
script:
- npm run start & # start in detached mode
- wait-on http://localhost:8080 # see: https://www.npmjs.com/package/wait-on
- cypress run # make sure cypress is available as well
Your docker-compose.yml
is not meant to run in a pipeline.您的
docker-compose.yml
并不意味着在管道中运行。 Mimic it instead using GitLab services .使用GitLab services来模仿它。 Dockerize all backends and store them in GitLab's private registry .
Dockerize 所有后端并将它们存储在GitLab 的私有注册表中。 Spin up all services in your pipeline and run your tests.
启动管道中的所有服务并运行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.