Get branch name in Dockerfile

docker hub
docker build
dockerfile example
docker build image name
dockerfile reference
dockerfile tag
docker build and run
docker image build example

In my Dockerfile I copy and build my frontend:

FROM node:8.11 as frontend
COPY frontend/package.json /frontend/package.json
COPY ./VERSION /frontend/VERSION
WORKDIR /frontend
RUN npm install
COPY frontend/ /frontend
RUN sed -i "s|VERSION|frontend@$(cat "VERSION")|g" src/environments/environment.prod.ts
RUN npm run build-prod

I want to change the following line:

RUN sed -i "s|VERSION|frontend@$(cat "VERSION")|g" src/environments/environment.prod.ts

to have a branch name at the beginning so it would look like:

RUN sed -i "s|VERSION|`**BRANCH_NAME**`-frontend@$(cat "VERSION")|g" src/environments/environment.prod.ts

so I want to get Branch name in my dockerfile somehow. but I'm not sure is it possible to use branch/commit names in dockerfile?

this is my gitlab-ci.yaml file:

build:
  stage: build
  before_script:
    - docker login -u gitlab-ci-token -p "$CI_JOB_TOKEN" registry.xx.xx
  script:
    - export TARGET=frontend
    - export IMAGE=$CI_REGISTRY_IMAGE/$TARGET
    - docker pull $IMAGE:$CI_COMMIT_REF_NAME || echo "no branch image"
    - docker pull $IMAGE:latest || echo "no latest image"
    - docker build --target $TARGET -t $IMAGE:$CI_COMMIT_REF_NAME .
    - docker push $IMAGE:$CI_COMMIT_REF_NAME

    - export IMAGE=$CI_REGISTRY_IMAGE
    - docker pull $IMAGE:$CI_COMMIT_REF_NAME || echo "no branch image"
    - docker pull $IMAGE:latest || echo "no latest image"
    - docker build -t $IMAGE:$CI_COMMIT_REF_NAME .
    - docker push $IMAGE:$CI_COMMIT_REF_NAME
  tags:
    - local-docker

What you can do is add an ARG in your Dockerfile called BRANCH_NAME and pass it in with --build-arg.

Below will create a build arg that defaults to master if no build arg is passed to the docker build:

FROM node:8.11 as frontend

ARG BRANCH_NAME=master
...
...

Your pipeline will include a docker build step like the following:

docker build -t $IMAGE:$CI_COMMIT_REF_NAME --build=arg BRANCH_NAME=$CI_COMMIT_REF_NAME .

From there, you can use the argument as a variable in your Dockerfile.

Automatically get git branch name in Dockerfile � Issue #6, Git branch name is hardcoded to 'development' in Dockerfile. Find some way to get this from an env var when building on hub.docker.com. To build three images using the same Dockerfile, give each image a new name. In this example, webdev1, webdev2, webdev3: docker build -t “webdev1:Dockerfile” . docker build -t “webdev2:Dockerfile” . docker build -t “webdev3:Dockerfile” . Each image created will be tagged Dockerfile. To change the tag during build, change Dockerfile:

There are several environment variables that could help you achieve this like CI_EXTERNAL_PULL_REQUEST_SOURCE_BRANCH_NAME, CI_COMMIT_REF_NAME, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME

Initially, I will say CI_COMMIT_REF_NAME is the one working for you. However, which one you should use will depend on what conditions are triggering the jobs or even the setup of your YAML file. Look at the following link to check which is your best fit for your jobs and the considerations that it will need.: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

docker build, For example, run this command to use a directory called docker in the branch We could find that all layer's name is <missing> , and there is a new layer with� Enter the name of the Source branch or tag you want to build. The first time you configure automated builds, a default build rule is set up for you. This default set builds from the Branch in your source code called master, and creates a Docker image tagged with latest. You can also use a regex to select which source branches or tags to build.

FROM node:8.11 as frontend
COPY frontend/package.json /frontend/package.json
COPY ./VERSION /frontend/VERSION
WORKDIR /frontend
RUN npm install
COPY frontend/ /frontend
RUN sed -i "s|VERSION|frontend@$(cat "VERSION")|g" src/environments/environment.prod.ts
RUN npm run build-prod

I dont know about gitlab's CI. But i see that you've just copied frontend to /frontend. I assume you have .git folder inside frontend, no?

If that is the case, why dont you use it? Just after you copy frontend :

RUN cd /frontend/ &&  BRANCH_NAME=$(git branch | grep \* | cut -d ' ' -f2) && export BRANCH_NAME

So now you have BRANCH_NAME as environment variable.

RUN sed -i "s|VERSION|`$BRANCH_NAME`-frontend@$(cat "VERSION")|g" src/environments/environment.prod.ts

Advanced options for Autobuild and Autotest, SOURCE_BRANCH : the name of the branch or the tag that is currently being a different branch (such as a post_push hook), you can't checkout that branch,� Because the apt-get update is not run, your build can potentially get an outdated version of the curl and nginx packages. Using RUN apt-get update && apt-get install -y ensures your Dockerfile installs the latest package versions with no further coding or manual intervention. This technique is known as “cache busting”.

Set up automated builds, You can enter a name, or use a regex to match which source branch or tag names to build. To learn more, see regexes. Enter the tag to apply to Docker images� ONBUILD <Dockerfile INSTRUCTION> Information: Adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream build, as if it had been inserted immediately after the FROM instruction in the downstream Dockerfile.

Provide env variable for branch name targeted by pull, Provide env variable for branch name targeted by pull request https://discuss. circleci.com/t/conditioning-docker-image-via-base-branch-name-from-pr/28426 Based off of these "sets of variables" that get declared when it's a PR build, you� The name of the branch the build was queued for. Git repo branch or pull request: The last path segment in the ref. For example, in refs/heads/master this value is master. In refs/heads/feature/tools this value is tools. TFVC repo branch: The last path segment in the root server path for the workspace. For example in $/teamproject/main this value is main.

Variables � Codefresh, to get the branch name normalized. It will be without any chars that are illegal in case the branch name were to be used as the Docker image tag name. This makes it easier to quickly jump to the Dockerfile for each version of the Docker image. So, for example, when I release version 0.3 of the Docker image (which I recently did), I also have a matching v0.3 release in GitHub that points to the specific Git commit from which version 0.3 of the Docker image is built.

Comments
  • sorry for noob question, but with this will the gitlab-cli.yaml file have BRANCH_NAME=master (because ARG BRANCH_NAME=master was declared) or Dockerfile will have $CI_COMMIT_REF_NAME (that's what I'm trying to get)?
  • thank you so much! I got it! ` - docker build --target $TARGET -t $IMAGE:$CI_COMMIT_REF_NAME --build-arg BRANCH_NAME=$CI_COMMIT_REF_NAME . ` and ARG BRANCH_NAME
  • You're welcome and glad you figured it out. I set the ARG BRANCH_NAME=master because I like to default variables even if you will override it 100% of the time. Just a habit but of course not necessary. Good luck with the rest of your pipeline improvements!
  • I just noticed when I have - docker build --target $TARGET -t $IMAGE:$CI_COMMIT_REF_NAME --build-arg BRANCH_NAME=$CI_COMMIT_REF_NAME . frontend is built twice, do you know why this could be?