2017-08-05 40 views
5

Gitlab提供了一个.gitlab-ci.yml模板,用于构建和发布图像到自己的注册表(点击项目中的“新文件”,选择.gitlab-ci.ymldocker)。该文件看起来像这样,它开箱即用:)如何从gitlab-ci发布码头图像到码头集线器

# 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 

但默认情况下,这将发布到gitlab的注册表。我们如何才能发布到docker hub而不是?

回答

7

根本不需要改变那个.gitlab-ci.yml,我们只需要在项目的管道设置中添加/替换环境变量。

首先,我们需要知道注册表url。使用hub.docker.com将无法​​正常工作,您会收到以下错误:

Error response from daemon: login attempt to https://hub.docker.com/v2/ failed with status: 404 Not Found

默认泊坞窗枢纽注册网址可以发现这样的:

docker info | grep Registry 
Registry: https://index.docker.io/v1/ 

index.docker.io是我正在寻找。我想出版gableroux/unity3d image,这是我在使用管道的秘密:

CI_REGISTRY_USER=gableroux 
CI_REGISTRY_PASSWORD=******** 
CI_REGISTRY=index.docker.io 
CI_REGISTRY_IMAGE=index.docker.io/gableroux/unity3d 

CI_REGISTRY_IMAGE是设置重要。需要
它默认为registry.gitlab.com/<username>/<project>
regsitry URL进行更新,以便使用index.docker.io/<username>/<project>
由于码头工人枢纽是默认的注册表,你也可以使用<username>/<project>代替,但我更喜欢当它的冗长。所以这个答案也应该包括其他注册表,只是相应地更新环境变量。

相关问题