Docker-in-Docker with Gitlab Shared runner for building and pushing docker images to registry

情到浓时终转凉″ 提交于 2019-11-30 09:46:42

The shared runners are now capable of building Docker images. Here is the job that you can use:

stages:
  - build
  - test
  - deploy

# ...
# other jobs here
# ...

docker:image:
  stage: deploy
  image: docker:1.11
  services:
    - docker:dind
  script:
    - docker version
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    # push only for tags
    - "[[ -z $CI_BUILD_TAG ]] && exit 0"
    - docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_BUILD_TAG
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE:$CI_BUILD_TAG

This job assumes that you are using the Container Registry provided by Gitlab. It pushes the images only when the build commit is tagged with a version number.

  • Documentation for Predefined variables.

  • Note that you will need to cache or generate as temporary artifacts of any dependencies for your service which are not committed in the repository. This is supposed to be done in other jobs. e.g. node_modules are not generally contained in the repository and must be cached from the build/test stage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!