我有一个基于Python的存储库,我试图设置Gitlab CI以使用Dockerfile构建Docker映像,并将该映像推送到Gitlab的注册表。
在构建Docker映像并将其部署到注册表之前,我想使用Python运行单元测试。这是我当前的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
我正在从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
但是,这两种方法都不适合我,因为我需要使用python进行测试,并需要使用docker来构建映像。是否可以使用Gitlab CI做到这一点,而无需创建同时安装了python和Docker的自定义Docker映像?
答案 0 :(得分:0)
我发现我可以创建多个作业,每个作业都有自己的图像:
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