How to copy a file from the repository, into the Docker container used for a job, in gitlab-ci.yml

二次信任 提交于 2019-12-12 13:08:50

问题


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?


回答1:


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.




回答2:


Your runner is already executing script in your docker container. What your job does here is:

  • start a container using Ubuntu image and mount your Git project in there.
  • cp sample.txt from Git project's root to container's
  • stop the container saying "job done"

That's basically what image means: use this docker image to start a container that will execute the commands listed in the script part.

I don't exactly understand what you're trying to achieve. If it's a build job, then why don't you actually COPY your file from your Dockerfile and configure your job to build it with docker build ? A Runner shell executor doing docker build -t your/image:latest -f build/Dockerfile . will do just fine. Then you push this image in some Docker registry (Gitlab's for example, or Docker Hub).

If really your goal is more complex and you want to just add a file to a running container, you can use the same Runner (with a shell executor, not a docker one, so no image) and run something like

- docker run --name YOUR_CONTAINER_NAME -v $PWD:/mnt ubuntu:latest cp /mnt/sample.txt /sample.txt
- docker commit -m "Commit Message" -a "You" YOUR_CONTAINER_NAME your/image:latest
- docker push your/image:latest
- docker rm YOUR_CONTAINER_NAME

Note: I'm not 100% sure the first one would work, but that would be the general idea of creating an image from a container without relying on the actual Dockerfile if really you can't achieve your goal with a Dockerfile.



来源:https://stackoverflow.com/questions/52679245/how-to-copy-a-file-from-the-repository-into-the-docker-container-used-for-a-job

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