简体   繁体   English

GitLab-CI:如何从其他存储库构建 docker 映像?

[英]GitLab-CI: How to build docker image from other repository?

I would like to have all my dockerfiles in one repository, instead of having them in the seperate projects.我想将我所有的 dockerfiles 放在一个存储库中,而不是将它们放在单独的项目中。 Therefore I have created the project "conductor".因此我创建了项目“指挥”。 All my.gitlab-ci stages are also specified in "conductor".所有 my.gitlab-ci 阶段也在“conductor”中指定。 In the actual project I only use the include command.在实际项目中我只使用了include命令。

My current code:我当前的代码:

docker build -f dockerfile .

Problem is that "dockerfile" isn't in the project where the pipeline gets executed but is in the "conductor" project.问题是“dockerfile”不在执行管道的项目中,而是在“指挥”项目中。

How can i modify the -f parameter to take the dockerfile from another gitlab project?如何修改 -f 参数以从另一个 gitlab 项目中获取 dockerfile?

The actual project could add conductor as a Git submodule ( git submodule add .实际项目可以添加导体作为Git 子模块git submodule add
In conductor, you would store your Dockerfile, one per actual project.在导体中,您将存储 Dockerfile,每个实际项目一个。

That way, your actual project can use这样,您的实际项目可以使用

docker build -f conductor/dockerfile.actual

You can use您可以使用

git clone https://gitlab.example.com/tanuki/awesome_project.git

to clone the remote repo.克隆远程仓库。 If the repo is not publicly accessible, you can create a deploy token to do this and use git clone with the deploy token.如果 repo 不可公开访问,您可以创建一个部署令牌来执行此操作,并将git clone与部署令牌一起使用。

git clone https://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git

see: https://docs.gitlab.com/ce/user/project/deploy_tokens/#git-clone-a-repository请参阅: https://docs.gitlab.com/ce/user/project/deploy_tokens/#git-clone-a-repository

I would like to have all my dockerfiles in one repository, instead of having them in the seperate projects.我想将我所有的 dockerfiles 放在一个存储库中,而不是将它们放在单独的项目中。 Therefore I have created the project "conductor".因此我创建了项目“指挥”。 All my.gitlab-ci stages are also specified in "conductor".所有 my.gitlab-ci 阶段也在“conductor”中指定。 In the actual project I only use the include command.在实际项目中我只使用了include命令。

It looks like a good usecase to define the generic stage/template in the common repository and to to include them in the users repositories via the include keyword.在公共存储库中定义通用阶段/模板并通过include关键字将它们包含在用户存储库中似乎是一个很好的用例。
Look at the include documentation :查看包含文档

include Use the include keyword to include external YAML files in your CI/CD configuration. include 使用 include 关键字在 CI/CD 配置中包含外部 YAML 文件。 You can break down one long gitlab-ci.yml into multiple files to increase readability, or reduce duplication of the same configuration in multiple places.您可以将一个长的 gitlab-ci.yml 分解为多个文件以增加可读性,或减少在多个地方重复相同的配置。

From user git repositories you could include the common template in the.从用户 git 存储库中,您可以在 . gitlab-ci.yml such as: gitlab-ci.yml例如:

include:
  - project: 'my-group/my-common-project'
    file: '/templates/.gitlab-ci-docker-build-template.yml'

And of course, reference the common stage(s) you need to use in it.当然,请参考您需要在其中使用的常见阶段。
Note that the common templates are not ridid.请注意,通用模板并没有被删除。 User/consummer repositories may override some things.用户/消费者存储库可能会覆盖某些内容。 For example variables are overridable.例如变量是可覆盖的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM