如何将存储库中的文件复制到用于作业的 Docker 容器中,在 gitlab-ci.yml [英] How to copy a file from the repository, into the Docker container used for a job, in gitlab-ci.yml

查看:16
本文介绍了如何将存储库中的文件复制到用于作业的 Docker 容器中,在 gitlab-ci.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 gitlab-ci 作业将项目中的文件添加到 Docker 中.假设我在 .gitlab-ci.yml 中有以下工作.

How can I add a file from my project into a Docker using in a gitlab-ci job. Suppose I have below job in my .gitlab-ci.yml .

build:master:
  image: ubuntu:latest
  script:
    - cp sample.txt /sample.txt
  stage: build
  only:
    - master

如何在 Ubuntu 映像中复制 sample.txt?我在想,因为它已经是一个正在运行的容器,所以我们不能直接执行复制命令,而是必须运行

How to copy a sample.txt inside Ubuntu image? I was thinking as it is already a running container so we can't perform copy command directly but have to run

docker cp sample.txt mycontainerID:/sample.txt

但我将如何获得 mycontainerID?因为它将在 Gitlab 运行器中运行,并且将为每次运行分配任何随机 id.我的假设是错误的吗?

but again how will I get mycontainerID? because it will be running inside a Gitlab runner and any random id will be assigned for every run. Is my assumption is wrong?

推荐答案

文件已经在容器中了.如果您仔细阅读 CI/CD 构建日志,您会在拉取镜像并启动它后在最顶部看到,您的存储库被克隆到正在运行的容器中.

The file is already inside the container. If you read carefully through the CI/CD build log, you will see at the very top after pulling the image and starting it, your repository is cloned into the running container.

您可以在 /builds/organization/repository
下找到它(请注意,这些只是示例,您必须根据实际的组织和存储库名称进行调整)
或者使用变量 $CI_PROJECT_DIR

其实就是目录you"开始工作时在.

In fact that is the directory "you" are in when starting the job.

例如这个.gitlab-ci.yml

For example this .gitlab-ci.yml

image: alpine

test:
  script:
    - echo "the project directory is - $CI_PROJECT_DIR"
    - cat $CI_PROJECT_DIR/README.md ; echo
    - echo "and where am I? - $PWD"

返回此管道输出:如您所见,我可以在容器内打印出 README.md 的内容.

returns this pipeline output: As you can see, I could print out the content of the README.md, inside the container.

这篇关于如何将存储库中的文件复制到用于作业的 Docker 容器中,在 gitlab-ci.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆