1

Bash script find a a tags in ECR repo:

aws ecr describe-images --repository-name laplacelab-backend-repo 
      \ --query 'sort_by(imageDetails,& imagePushedAt)[*]'
      \--output json | jq -r '.[].imageTags'

Output:

[
  "v1",
  "sometag",
  ...
]

How I can extract the version number? v<number> can contain the only version tag. I need to get a number and increment version for the set to var. If output of sort_by(imageDetails,& imagePushedAt)[*] is empty JSON arr instead

[
    {
        "registryId": "057296704062",
        "repositoryName": "laplacelab-backend-repo",
        "imageDigest": "sha256:c14685cf0be7bf7ab1b42f529ca13fe2e9ce00030427d8122928bf2d46063bb7",
        "imageTags": [
            "v1"
        ],
        "imageSizeInBytes": 351676915,
        "imagePushedAt": 1593514683.0
    }
]

Set 2

No one repo sort_by(imageDetails,& imagePushedAt)[*] return [] set 1.

As a result, I try to get var VERSION with next version for an update or 1 if the repo is empty.

Inian
  • 80,270
  • 14
  • 142
  • 161
Pavel
  • 2,005
  • 5
  • 36
  • 68

1 Answers1

1

You could use the select() function on the imageTags array and get only the tag starting with v and increment it.

jq '( .[].imageTags[] | select(startswith("v")) | ltrimstr("v") | tonumber | .+1 ) // 1'

For other cases like the tags array being empty or containing null strings (error case), the value defaults to 1

For storing into the variable e.g. say version (avoid using uppercase variable names from a user scripts), use command substitution. See How do I set a variable to the output of a command in Bash?

version=$( <your-pipeline> )

Note: This does not work well with version strings following Semantic versioning RFC, e.g. as v1.2.1 as jq does not have a library to parse them.

Inian
  • 80,270
  • 14
  • 142
  • 161