skip to Main Content

I’m building my docker image in a generic way so that might be used by multiple services, however not all the --build-arg will be present because it depends on the service that is being called on the GitHub Actions. My question is; is it possible to use only one or more --build-arg without using every --build-arg?

Command:

docker build 
  -f ${{ inputs.docker-location }} 
  -t $IMAGE_NAME:$TAG 
  --build-arg value1=${{secrets.value1}} 
  --build-arg value2=${{ secrets.value2}} 
  --build-arg value3=${{secrets.value3}} 
  --build-arg value4=${{ secrets.value4 }} . 

So in this case I would only pass the value1 and value2, however, it fails because the rest of the values will be empty.

I know it works if all args are present, as I’m already using it, but wanted to expand it in a more generic way.

Thanks in advance! 🙂

3

Answers


  1. Chosen as BEST ANSWER

    I've tried a couple of things and this one work.

    docker build -f ${{ inputs.docker-location }} 
              -t $IMAGE_NAME:$TAG 
              --build-arg value1=${{secrets.value1 || 'default_value'}} 
              --build-arg value2=${{ secrets.value2 || 'default_value'}} 
              --build-arg value3=${{secrets.value3 || 'default_value'}} 
              --build-arg value4=${{ secrets.value4 || 'default_value'}} . 
    
    

    Also figure it out that if you don't want to pass any value as if it empty, then you can just leave it without the default value, like below and the pipeline will ignore the empty args. --build-arg value2=${{ secrets.value2}}


  2. is it possible to use only 1 or more –build -arg without using every –build-arg?

    Yes, you can do this by defining a default ARG in the Dockerfile, e.g:

    ARG value4=FOO
    

    For more information: https://docs.docker.com/engine/reference/builder/#default-values

    Login or Signup to reply.
  3. In your pipeline check if those secrets are empty or not. If empty then give it a default value that you would handle in the Dockerfile appropriatly.

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