Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file from the repository, into the Docker container used for a job, in 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

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?

like image 735
gamechanger17 Avatar asked Oct 06 '18 12:10

gamechanger17


1 Answers

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.

You can find it under /builds/<organization>/<repository>

(note that these are examples, and you have to adjust to your actual organization and repository 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:

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: enter image description here As you can see, I could print out the content of the README.md, inside the container.

like image 100
The Fool Avatar answered Oct 13 '22 18:10

The Fool