skip to Main Content

Consider the following json data, sourced from from Docker image nginx:1.25-bookworm:

# docker pull nginx:1.25-bookworm
# docker image inspect 81be38025439 | jq '.[].Config.Env'
[
  "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  "NGINX_VERSION=1.25.3",
  "NJS_VERSION=0.8.2",
  "PKG_RELEASE=1~bookworm"
]
#

I would like to be able to access ‘1.25.3’ from this list to tag the image in a build step of a CI-CD pipeline.

I am close to my goal with the following:

# docker image inspect 0743972990c7 | jq -r '.[].Config.Env[1]'
NGINX_VERSION=1.23.4

But would like to know how to access the item ‘NGINX_VERSION’ by name instead of relying on the assumption that it will be the second environment variable.

2

Answers


  1. So filter NGINX_VERSION from the array and remove it.

    .[].Config.Env |
    select(startswith("NGINX_VERSION=")) |
    sub("^NGINX_VERSION="; "")
    
    Login or Signup to reply.
  2. For the task given, I’d also go with startswith as described in KamilCuk’s answer. But if you need to access multiple values, you might want to consider creating a lookup object using INDEX:

    INDEX(.[:index("=")]) as $ix   # stores {"PATH": … , "NGINX_VERSION": … , …}
    

    and then access the values by field name (truncate by the index position of the first = character):

    | $ix["NGINX_VERSION"]         # yields "NGINX_VERSION=1.25.3"
    | .[index("=")+1:]             # yields "1.25.3
    

    Demo

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