ashish bustler
ashish bustler

Reputation: 500

How to use a variable defined at Gitlab CI stage in Dockerfile

I have a GitlabCI stage for building Docker image of a Python app. It uses a dynamic S3 variable name which is defined at stage level like this:

Build Container:
  stage: package
  variables:
    S3_BUCKET: <value>

I tried using it inside Dockerfile like this as I want to use it during build time:

FROM python:3.8
EXPOSE 8080
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
RUN SOMETHING
ARG S3_BUCKET_NAME=${S3_BUCKET}
RUN echo "$S3_BUCKET_NAME"
RUN python3 some_Script.py
CMD streamlit run app.py

But this variable is not able to fetch value. Is there some other way of using it inside Dockerfile.

Upvotes: 0

Views: 294

Answers (1)

TheQueenIsDead
TheQueenIsDead

Reputation: 955

How do you build the image? You will need to ensure that the flag --build-arg passes S3_BUCKET_NAME in.

If you wish to persist the value within the image, such that the value is available during runtime, you will need to export it via a RUN command, or an ENV command. For example

# expect a build-time variable
ARG A_VARIABLE
# use the value to set the ENV var default
ENV an_env_var=$A_VARIABLE

Upvotes: 1

Related Questions