skip to Main Content

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.

screenshot

I’ve tried

From Docker:

These produce the same results.

3

Answers


  1. Chosen as BEST ANSWER

    After days of trying, I figured it out:

    [
      run,
      -e, 
      MYFLAG=$_MYFLAG,
      -e,
      SECOND_FLAG=$_SECOND_FLAG,
      us-central1-docker.pkg.dev/XXX/test-flags/test-flags,
    ]
    

    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.


  2. Can you try your step with double quote?

      - 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",
          ]
    
    Login or Signup to reply.
  3. Just a guess, but might also be the lack of the curly brackets {}

    ${_MYFLAG}

    since it is like this in the docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search