Reputation: 2684
I have noticed that when docker run
is executed in .gitlab-ci.yml
, the docker image is built using files from the master branch of the repository, independently on whether a commit is pushed from master or from another branch.
In my current script, I am building an image and running some test similarly as follows:
image: docker:stable
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
DOCKERHUB_IMAGE: "${CI_PROJECT_NAMESPACE}/myproject"
services:
- docker:dind
before_script:
- docker info
- docker login -u $CI_PROJECT_NAMESPACE -p $PASSWORD $CI_REGISTRY
stages:
- build
- test
build_myproject:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE .
- docker push $CI_REGISTRY_IMAGE
test_myproject:
stage: test
script:
- docker pull $CI_REGISTRY_IMAGE
- docker tag $CI_REGISTRY_IMAGE myproject
- docker run --rm -v $PWD:/tmp myproject options input ...
...
How can I specify there the current branch for building the image?
So far I have tried to install git
with and use git checkout
before building with:
before_script:
- apt-get update && apt-get -y install git
....
script:
- git checkout $CI_COMMIT_REF_NAME
- docker build -t $CI_REGISTRY_IMAGE .
But this does not work with the docker:stable
image I guess:
/bin/sh: eval: line 93: apt-get: not found
Following docker build
documentation (here), I have also tried to build the image from the URL. But I am not sure how to use this. When I run docker build
with the following code:
- docker build -t $CI_REGISTRY_IMAGE https://gitlab.com/user/myproject.git#$CI_COMMIT_REF_NAME
I get again an error that has to do with git.
unable to prepare context: unable to find 'git': exec: "git": executable file not found in $PATH
This makes me wonder if I should ask about how to install git
within a docker:stable
image, but I do not want to convert this into a XY problem. Perhaps there is a simpler way to perform this task.
EDIT
The error was a mistake on my side, but I will leave the question here in case it helps somebody.
The problem was that in the Dockerfile I was using git clone
for getting a copy of the repository, instead of directly using the Docker COPY
command. Of course that way the branch used for docker build was the master one.
Upvotes: 0
Views: 2019
Reputation: 4366
It seems highly unprobable that pipeline triggered from push to custom branch contains files from master branch.
Check logs from executed pipeline for checkout line, e.g. Checking out <commit sha> as <branch name>...
To ensure you build new image I would try building with –no-cache
option.
I can't see CI_REGISTRY_IMAGE
declaration in your pipeline definition. Does docker name change or is it constant?
If it doesn't change and you don't mind additional stored images in your repository then you could tag each build with branch specific name. Or even commit specific. Then you would be sure you test proper image.
Upvotes: 1