I’m looking to pass ENV args from a cloudbuild.yaml to insert values during Docker run in Cloud Build.
I’m defining _MYFLAG
and _SECOND_FLAG
in the Cloud Build UI trigger options.
cloudbuild.yaml
steps:
- name: gcr.io/cloud-builders/docker
args:
[
build,
-t,
us-central1-docker.pkg.dev/XXX/test-flags/test-flags,
".",
]
- name: gcr.io/cloud-builders/docker
entrypoint: docker
args:
[
run,
-e MYFLAG=$_MYFLAG,
-e SECOND_FLAG=$_SECOND_FLAG,
us-central1-docker.pkg.dev/XXX/test-flags/test-flags,
]
images:
[us-central1-docker.pkg.dev/XXX/test-flags/test-flags]
options:
logging: CLOUD_LOGGING_ONLY
substitutions:
_MYFLAG: "default build yaml arg"
_SECOND_FLAG: ""
Dockerfile
FROM python:latest
COPY . .
RUN pip install -r requirements.txt
CMD python main.py --myflag ${MYFLAG}
--second_flag ${SECOND_FLAG}
Python file
from absl import app
from absl import flags
from absl import logging
FLAGS = flags.FLAGS
flags.DEFINE_string("myflag", "Default value", "Flag description.")
flags.DEFINE_string("second_flag", "Default value second", "Flag description.")
def main(_):
logging.info("STARTING FLAG SCRIPT")
logging.info("myflag: %s", FLAGS.myflag)
logging.info("second_flag: %s", FLAGS.second_flag)
if __name__ == "__main__":
app.run(main)
From the outputs it seems like the first flag is interpreting the second flags as the arg and the second flag is using my default arg in the cloudbuild.yaml file which means the definitions in the Cloud Build UI are not being interpreted properly.
I’ve tried
From Docker:
-
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables–e—env—env-file
-
https://docs.docker.com/engine/reference/run/#env-environment-variables
-
In cloudbuild.yaml:
-e MYFLAG=$_MYFLAG,
--env MYFLAG=$_MYFLAG,
"--env 'MYFLAG=$_MYFLAG',"
These produce the same results.
3
Answers
After days of trying, I figured it out:
The flags also need to be on their own element. I suppose the rule is anything separated by space should have its own comma separated value.
Can you try your step with double quote?
Just a guess, but might also be the lack of the curly brackets {}
since it is like this in the docs