LoginSignup
4
4

More than 3 years have passed since last update.

ECRリポジトリから最新のタグを取得する

Posted at

目的

  • ECRリポジトリに保存されたDockerイメージの最新のタグを取得する

使うもの

  • AWS CLI

手法

--query を利用してAWS CLIの結果をフィルタ、抽出できます。

aws ecr describe-images --repository-name 【リポジトリ名】 \
  --query "reverse(sort_by(imageDetails[*], &imagePushedAt))[0].imageTags[0]" \
  | sed -e 's/"//g'

--queryJMESPath の文法が使えます。

上記の例では、リポジトリのイメージ一覧を取得して、imagePushedAt(日付)で降順ソートを行い、1つ目の imageTagsを取得します。

配列になっているので、そのままでは以下のように扱いづらいため、imagePushedAt[0] で値だけ取り出します。

# imageTags で取得
[
    "005d6f6"
]

# imageTags[0] で取得
"005d6f6"

最後に sed でダブルクォートを取り除き、再利用可能にします。

参考

AWS CLI からのコマンド出力の制御 - AWS Command Line Interface

--query オプションを使用して出力をフィルタリングする方法
AWS CLI は、--query オプションによって、組み込みの JSON ベースの出力フィルタリング機能を提供します。--query パラメータは、JMESPath の仕様に準拠している文字列を受け入れます。

JMESPath — JMESPath

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4