Skip to content

Instantly share code, notes, and snippets.

@sasasin
Created May 29, 2023 11:55
Show Gist options
  • Save sasasin/c524d64f4d59d05de3860b9b1d19da36 to your computer and use it in GitHub Desktop.
Save sasasin/c524d64f4d59d05de3860b9b1d19da36 to your computer and use it in GitHub Desktop.
コンテナイメージ名っぽい文字列を抽出するやつ
#!/bin/bash
# コンテナイメージ名っぽい文字列を抽出するやつ
# 組織のgitリポジトリをドサッと手元に持ってきて、リポジトリ横断でリストアップしたい
# ファイル形式それぞれで漁り方が異なる
# フィルタリングが不完全で、コンテナ名ではないものが残ることがあるし、コンテナ名が除外されてることもある(かもしれない)
# rg は ripgrep
# rg --type xxxx が指す拡張子の調べ方は rg --list-type | grep xxxx で出る
# 詳しくは https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md を参照
# Dockerfile を対象に FROM を抽出
rg -uuu "FROM *:*" --type docker \
| sed -e 's| |\n|g' \
| grep -v 'Dockerfile' \
| grep ':' \
| sort -u \
> dockerfile.images.txt
# YAML を対象に image: を抽出
rg -uuu "image:" --type yaml \
| grep -vE '[_-]image' \
| sed -e 's| |\n|g' \
| grep -vE 'yml|yaml' \
| grep ':' \
| grep -v 'dkr.ecr.ap-northeast-1.amazonaws.com' \
| sed -e 's|"||g' -e 's|,||g' \
| sort -u \
> yaml.images.txt
# json を対象に image: を抽出
rg -uuu '"image":' --type json \
| sed -e 's| |\n|g' \
| grep -vE '\.json' \
| grep ":" \
| grep -v '":' \
| sed -e 's|"||g' -e 's|,||g' \
| grep -v "dkr.ecr.ap-northeast-1.amazonaws.com" \
| sort -u \
> json.images.txt
ls {dockerfile, yaml, json}.images.txt
cat {dockerfile, yaml, json}.images.txt \
| sort -u \
> all.images.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment