[英]Gitlab CI how to run tests before building docker image
I have a Python based repository and I am trying to setup Gitlab CI to Build a Docker image using Dockerfile and pushing the image to Gitlab's Registry.我有一个基于 Python 的存储库,我正在尝试设置 Gitlab CI 以使用 Dockerfile 构建 Docker 映像并将映像推送到 Gitlab 的注册表。
Before building and deploying the Docker image to registry, I want to run my unit tests using Python.在构建 Docker 映像并将其部署到注册表之前,我想使用 Python 运行我的单元测试。 Here is my current gitlab-ci.yml file that only does testing:这是我目前只做测试的 gitlab-ci.yml 文件:
image: python:3.7-slim
before_script:
- pip3 install -r requirements.txt
test:
variables:
DJANGO_SECRET_KEY: some-key-here
script:
- python manage.py test
build:
DO NOT KNOW HOW TO DO IT
I am checking some templates from Gitlab's website and found one for Docker:我正在检查 Gitlab 网站上的一些模板,并为 Docker 找到了一个模板:
# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
build-master:
stage: build
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
build:
stage: build
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
except:
- master
However, both of these do not work for me because I need to have python for testing and docker for building the image.但是,这两个都不适合我,因为我需要用 python 进行测试,用 docker 来构建图像。 Is there way to do it with Gitlab CI without creating a custom Docker image that has both python and Docker installed?有没有办法使用 Gitlab CI 做到这一点,而无需创建同时安装了 python 和 Docker 的自定义 Docker 映像?
I found out that I can create multiple jobs, each with their own images:我发现我可以创建多个作业,每个作业都有自己的图像:
stages:
- test
- build
test:
stage: test
image: python:3.7-slim
variables:
DJANGO_SECRET_KEY: key
before_script:
- pip3 install -r requirements.txt
script:
- python manage.py test
only:
- master
build:
stage: build
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.