简体   繁体   English

Gitlab CI 如何在构建 docker 镜像之前运行测试

[英]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.

相关问题 构建映像后,如何从.gitlab-ci.yml 运行我的 docker 映像? - How do I run my docker image from .gitlab-ci.yml after building the image? Gitlab CI:如何使用docker和shell运行器在管道中运行测试 - Gitlab CI: How to run tests in pipeline using docker and shell runner 如何在构建 Docker 映像之前对 GitLab 的容器注册表进行身份验证? - How to authenticate to GitLab's container registry before building a Docker image? 如何在 Gitlab ci 中的 docker 中运行 docker? - How to run docker in docker in Gitlab ci? 尝试使用 docker 映像在 Gitlab CI 管道运行器中运行 Webdriver IO(端到端)测试时出现问题 - Issue when trying to run Webdriver IO (end to end) tests in a Gitlab CI pipeline runner using a docker image 使用 MinGW 编译的 Qt5 构建 Docker 映像在从“docker:latest”映像运行的容器中工作,但在 GitLab CI 中失败 - Building of a Docker image with Qt5 compiled with MinGW works in a container run from "docker:latest" image, but fails in GitLab CI 从 Gitlab CI 构建 docker 图像时环境变量丢失 - Env vars lost when building docker image from Gitlab CI 在 Z9792E23419583666B178605ZDECAEC5 中构建和推送 Spring 启动 maven 插件 docker 映像 - Building and pushing Spring Boot maven plugin docker image in Gitlab CI 如何在 Gitlab CI/CD 管道中使用 Docker-Compose 运行 API 端点测试 - How to run API endpoints tests with Docker-Compose in Gitlab CI/CD pipeline 在GitLab CI中为Node.js应用程序构建Docker映像 - Building a Docker image for a Node.js app in GitLab CI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM