Absolute path gitlab project

Issue

I have a GitLab instance self-managed and one of my project has a folder which contains 3 sub-directories, these 3 sub-directories have a Dockerfile.

All my Dockerfile’s have a grep command to get the latest version from the CHANGELOG.md which is located in the root directory.

I tried something like this to go back 2 steps but it doesn’t work (grep: ../../CHANGELOG.md: No such file or directory)

Dockerfile:

grep -m 1 '^## v.*$' "../../CHANGELOG.md"

example:

Link:

https://mygitlab/project/images/myproject

repo content:

.
├──build
   ├──image1
   ├──image2
   ├──image3
├──CHANGELOG.md

gitlab-ci.yaml

script:
    - docker build --network host -t $VAL_IM ./build/image1
    - docker push $VAL_IM

The issue is happening when I build the images.

Solution

docker build –network host -t $VAL_IM ./build/image1

Here, you have set the build context to ./build/image1 — builds cannot access directories or files outside of the build context. Also keep in mind that if you use RUN in a docker build, it can only access files that have already been copies inside the container (and as stated you can’t copy files outside the build context!) so this doesn’t quite make sense as stated.

If you’re committed to this versioning strategy, what you probably want to do is perform your grep command as part of your GitLab job before calling docker build and pass in the version as a build arg.

In your Dockerfile, add an ARG:

FROM # ...
ARG version
# now you can use the version in the build... eg:
LABEL com.example.version="$version"
RUN echo version is "$version"

Then your GitLab job might be like:

script:
    - version=$(grep -m 1 '^## v.*$' "./CHANGELOG.md")
    - docker build --build-arg version="${version}" --network host -t $VAL_IM ./build/image1
    - docker push $VAL_IM

Answered By – sytech

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published