如何在gitlab-ci.yml中将文件从存储库复制到用于作业的Docker容器中 [英] How to copy a file from the repository, into the Docker container used for a job, in gitlab-ci.yml
问题描述
如何使用 gitlab-ci
作业将项目中的文件添加到Docker中。假设我的 .gitlab-ci.yml
中的工作低于标准。
build:master:
图像:ubuntu:最新
脚本:
-cp sample.txt /sample.txt
阶段:仅构建
:
-master
如何在Ubuntu映像中复制sample.txt?我在想,因为它已经是一个正在运行的容器,所以我们不能直接执行复制命令,而必须运行
docker cp示例.txt mycontainerID:/sample.txt
但是我又如何获得mycontainerID?因为它将在Gitlab运行程序中运行,并且每次运行都会分配任何随机ID。我的假设是错误的吗?
文件已经在容器内。如果您仔细阅读CI / CD构建日志,则在拉动映像并启动它之后,您将在最顶部看到该存储库,并将其克隆到正在运行的容器中。
您可以在 / builds / organization / repository
下找到它(请注意,这些是变量和您必须调整为实际的组织和名称)
或使用变量 $ CI_PROJECT_DIR
实际上,这是开始工作时所在的目录您。
例如,来自存储库
如您所见,我可以在容器内打印出README.md的内容。
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
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
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?
The file is already inside of 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.
You can find it under /builds/organization/repository
(note that these are variables and you have to adjust to your actual organization and name)
Or with the variable $CI_PROJECT_DIR
In fact that is the directory "you" are in when starting the job.
For example this .gitlab-ci.yml from the repository https://gitlab.com/bluebrown/gitlab-ci-playground
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"
returns this pipeline output: As you can see, I could print out the content of the README.md, inside the container.
这篇关于如何在gitlab-ci.yml中将文件从存储库复制到用于作业的Docker容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!