Skip to content

Commit a000c66

Browse files
aoirintHiroshiba
andauthored
DockerビルドCI: イメージタグの別名を別々にビルドしないようにする (#1498)
* Docker: イメージタグの別名を別々にビルドしないようにする * create tools/generate_docker_image_names.bash * comment: quote shell args * comment: improve examples * comment: improve examples * comment: improve examples * comment: 冗長な例を削除 * -eq -> = * fix SC2086: double quote $GITHUB_OUTPUT * fix wrong step output reference * remove shell: bash * コンフリクト解消時にUbuntu 22.04のincludeが増殖していたのを削除 * print usage to stderr * add matrix var descriptions * add comment * fix comment * update comment * fix buildcache image name: add repository * remove typo quote * create tools/generate_docker_image_names.py * pysen run format * update usage * fix B950 * Bashの行コメント"#"をdocstringに変えたときに消し忘れていたので削除 * コメントの表現修正: タグ名 -> Dockerイメージ名 * コメントの表現修正: タグ名 -> Dockerイメージ名 * コメントの表現修正: タグ名 -> Dockerイメージ名 * add --repository argument help * コメントの表現修正: 空文字 -> 空文字列 * helpの出力を末尾句点なしに統一 * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py * Update .github/workflows/build-engine-container.yml * Update tools/generate_docker_image_names.py * Update tools/generate_docker_image_names.py * Update tools/generate_docker_image_names.py * pysen run format * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py Co-authored-by: Hiroshiba <[email protected]> * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * Update tools/generate_docker_image_names.py Co-authored-by: Hiroshiba <[email protected]> * Update .github/workflows/build-engine-container.yml Co-authored-by: Hiroshiba <[email protected]> * rename matrix.tags -> matrix.prefixes * rename buildcache_tag -> buildcache_prefix * lockファイルに更新があったっぽいので追従 * generate_docker_image_names: styling arguments * Update .github/workflows/build-engine-container.yml --------- Co-authored-by: Hiroshiba <[email protected]> Co-authored-by: Hiroshiba Kazuyuki <[email protected]>
1 parent 94b748f commit a000c66

File tree

2 files changed

+172
-59
lines changed

2 files changed

+172
-59
lines changed

.github/workflows/build-engine-container.yml

+55-59
Original file line numberDiff line numberDiff line change
@@ -35,59 +35,44 @@ jobs:
3535
3636
build-docker:
3737
needs: [config]
38-
runs-on: ${{ matrix.os }}
38+
runs-on: ubuntu-latest
3939

4040
strategy:
4141
matrix:
42-
os: [ubuntu-latest]
43-
tag:
44-
- ""
45-
- cpu
46-
- cpu-ubuntu20.04
47-
- nvidia
48-
- nvidia-ubuntu20.04
49-
- cpu-ubuntu22.04
50-
- nvidia-ubuntu22.04
42+
# 各変数の説明
43+
# prefixes: Docker tagのプレフィックス。カンマ区切り。空文字列の場合、バージョン文字列のみがタグ名になる
44+
# buildcache_prefix: ビルドキャッシュのプレフィックス。空文字列やカンマは使用不可
45+
# target: Dockerfileのビルドステージ名
46+
# base_image: Dockerfileのビルド用ステージのベースイメージ
47+
# base_runtime_image: Dockerfileの実行用ステージのベースイメージ
48+
# onnxruntime_version: ONNX Runtimeのバージョン
49+
# platforms: Dockerのプラットフォームバリアント。カンマ区切り。 参考: https://docs.docker.com/build/building/multi-platform/
5150
include:
5251
# Ubuntu 20.04
53-
- tag: ""
52+
- prefixes: ",cpu,cpu-ubuntu20.04"
53+
buildcache_prefix: "cpu-ubuntu20.04"
5454
target: runtime-env
5555
base_image: ubuntu:20.04
5656
base_runtime_image: ubuntu:20.04
5757
onnxruntime_version: 1.13.1
5858
platforms: linux/amd64,linux/arm64/v8
59-
- tag: cpu
60-
target: runtime-env
61-
base_image: ubuntu:20.04
62-
base_runtime_image: ubuntu:20.04
63-
onnxruntime_version: 1.13.1
64-
platforms: linux/amd64,linux/arm64/v8
65-
- tag: cpu-ubuntu20.04
66-
target: runtime-env
67-
base_image: ubuntu:20.04
68-
base_runtime_image: ubuntu:20.04
69-
onnxruntime_version: 1.13.1
70-
platforms: linux/amd64,linux/arm64/v8
71-
- tag: nvidia
72-
target: runtime-nvidia-env
73-
base_image: ubuntu:20.04
74-
base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04
75-
onnxruntime_version: 1.13.1
76-
platforms: linux/amd64
77-
- tag: nvidia-ubuntu20.04
59+
- prefixes: "nvidia,nvidia-ubuntu20.04"
60+
buildcache_prefix: "nvidia-ubuntu20.04"
7861
target: runtime-nvidia-env
7962
base_image: ubuntu:20.04
8063
base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04
8164
onnxruntime_version: 1.13.1
8265
platforms: linux/amd64
8366
# Ubuntu 22.04
84-
- tag: cpu-ubuntu22.04
67+
- prefixes: "cpu-ubuntu22.04"
68+
buildcache_prefix: "cpu-ubuntu22.04"
8569
target: runtime-env
8670
base_image: ubuntu:22.04
8771
base_runtime_image: ubuntu:22.04
8872
onnxruntime_version: 1.13.1
8973
platforms: linux/amd64,linux/arm64/v8
90-
- tag: nvidia-ubuntu22.04
74+
- prefixes: "nvidia-ubuntu22.04"
75+
buildcache_prefix: "nvidia-ubuntu22.04"
9176
target: runtime-nvidia-env
9277
base_image: ubuntu:22.04
9378
base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04
@@ -137,32 +122,43 @@ jobs:
137122
DOWNLOAD_RESOURCE_PATH: download/resource
138123
run: bash tools/process_voicevox_resource.bash
139124

125+
- name: <Build> Generate Docker image names
126+
id: generate-docker-image-names
127+
run: |
128+
# Dockerイメージ名を outputs.tags に改行区切りで格納する
129+
{
130+
echo "tags<<EOF"
131+
132+
python3 tools/generate_docker_image_names.py \
133+
--repository "${{ env.IMAGE_NAME }}" \
134+
--version "${{ needs.config.outputs.version_or_latest }}" \
135+
--prefix "${{ matrix.prefixes }}"
136+
137+
echo "EOF"
138+
} >> "$GITHUB_OUTPUT"
139+
140+
# ビルドキャッシュに指定するためのDockerイメージ名を生成する
141+
# NOTE: デフォルトブランチへのコミットの場合のみキャッシュを作成する
142+
- name: <Build> Generate Docker buildcache image names
143+
id: generate-docker-buildcache-image-names
144+
run: |
145+
# --cache-from に指定するためのDockerイメージ名
146+
# 常にデフォルトブランチのビルドキャッシュ(*-latest-buildcache)を使用する
147+
cache_from="type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_prefix }}-latest-buildcache"
148+
149+
# --cache-to に指定するためのDockerイメージ名
150+
# リリースの場合、ビルドキャッシュを作成しないため、空文字列を格納する
151+
cache_to=""
152+
if [ "${{ needs.config.outputs.version_or_latest }}" = "latest" ]; then
153+
cache_to="type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_prefix }}-latest-buildcache,mode=max"
154+
fi
155+
156+
# outputs に格納する
157+
echo "cache-from=$cache_from" >> "$GITHUB_OUTPUT"
158+
echo "cache-to=$cache_to" >> "$GITHUB_OUTPUT"
159+
140160
- name: <Build/Deploy> Build and Deploy Docker image
141161
uses: docker/build-push-action@v5
142-
env:
143-
IMAGE_TAG:
144-
|- # If it's a release, add the version, otherwise add the `latest`
145-
${{ (
146-
matrix.tag != '' && (
147-
format('{0}:{1}-{2}', env.IMAGE_NAME, matrix.tag, needs.config.outputs.version_or_latest)
148-
) || format('{0}:{1}', env.IMAGE_NAME, needs.config.outputs.version_or_latest)
149-
) }}
150-
IMAGE_CACHE_FROM:
151-
|- # Always use the `latest` buildcache. :latest-buildcache or :{tag}-latest-buildcache
152-
${{ (
153-
matrix.tag != '' && (
154-
format('type=registry,ref={0}:{1}-latest-buildcache', env.IMAGE_NAME, matrix.tag)
155-
) || format('type=registry,ref={0}:latest-buildcache', env.IMAGE_NAME)
156-
) }}
157-
IMAGE_CACHE_TO:
158-
|- # If it's a release, do not create buildcache, otherwise create the `latest` buildcache. :latest-buildcache or :{tag}-latest-buildcache
159-
${{ (
160-
needs.config.outputs.version_or_latest == 'latest' && (
161-
matrix.tag != '' && (
162-
format('type=registry,ref={0}:{1}-latest-buildcache,mode=max', env.IMAGE_NAME, matrix.tag)
163-
) || format('type=registry,ref={0}:latest-buildcache,mode=max', env.IMAGE_NAME)
164-
) || ''
165-
) }}
166162
with:
167163
context: .
168164
builder: ${{ steps.buildx.outputs.name }}
@@ -178,9 +174,9 @@ jobs:
178174
ONNXRUNTIME_VERSION=${{ matrix.onnxruntime_version }}
179175
target: ${{ matrix.target }}
180176
push: true
181-
tags: ${{ env.IMAGE_TAG }}
182-
cache-from: ${{ env.IMAGE_CACHE_FROM }}
183-
cache-to: ${{ env.IMAGE_CACHE_TO }}
177+
tags: ${{ steps.generate-docker-image-names.outputs.tags }}
178+
cache-from: ${{ steps.generate-docker-buildcache-image-names.outputs.cache-from }}
179+
cache-to: ${{ steps.generate-docker-buildcache-image-names.outputs.cache-to }}
184180
platforms: ${{ matrix.platforms }}
185181

186182
run-release-test-workflow:

tools/generate_docker_image_names.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Dockerリポジトリ名、バージョン文字列、カンマ区切りのプレフィックスを受け取り、
3+
バージョン文字列付きのDockerイメージ名を改行区切りで標準出力に出力する
4+
5+
6+
$ python3 ./tools/generate_docker_image_names.py \
7+
--repository "REPO" \
8+
--version "VER" \
9+
--prefix ",A,B"
10+
REPO:VER
11+
REPO:A-VER
12+
REPO:B-VER
13+
14+
$ python3 ./tools/generate_docker_image_names.py \
15+
--repository "REPO" \
16+
--version "VER" \
17+
--prefix ""
18+
REPO:VER
19+
"""
20+
21+
from argparse import ArgumentParser
22+
23+
24+
def generate_docker_image_names(
25+
repository: str,
26+
version: str,
27+
comma_separated_prefix: str,
28+
) -> list[str]:
29+
"""
30+
Dockerリポジトリ名、バージョン文字列、カンマ区切りのプレフィックスを受け取り、
31+
バージョン文字列付きのDockerイメージ名を配列で返す
32+
33+
prefixが空文字列でない場合、"{prefix}-{version}"をタグにする
34+
35+
- 例: repository="REPO", version="VER", prefix="A" -> "REPO:A-VER"
36+
37+
prefixが空文字列の場合、"{version}"をタグにする
38+
39+
- 例: repository="REPO", version="VER", prefix="" -> "REPO:VER"
40+
41+
Parameters
42+
----------
43+
repository : str
44+
Dockerリポジトリ名
45+
version : str
46+
バージョン文字列
47+
comma_separated_prefix : str
48+
カンマ区切りのプレフィックス
49+
50+
Returns
51+
-------
52+
list[str]
53+
Dockerイメージ名の配列。
54+
55+
Examples
56+
--------
57+
>>> generate_docker_image_names("voicevox/voicevox_engine", "0.22.0", "cpu,cpu-ubuntu22.04")
58+
['voicevox/voicevox_engine:0.22.0',
59+
'voicevox/voicevox_engine:cpu-0.22.0',
60+
'voicevox/voicevox_engine:cpu-ubuntu22.04-0.22.0']
61+
"""
62+
# カンマ区切りのタグ名を配列に変換
63+
prefixes = comma_separated_prefix.split(",")
64+
65+
# 戻り値の配列
66+
docker_image_names: list[str] = []
67+
68+
for prefix in prefixes:
69+
# プレフィックスが空文字列でない場合、末尾にハイフンを付ける
70+
if prefix:
71+
prefix = f"{prefix}-"
72+
docker_image_names.append(f"{repository}:{prefix}{version}")
73+
74+
return docker_image_names
75+
76+
77+
def main() -> None:
78+
parser = ArgumentParser()
79+
parser.add_argument(
80+
"--repository",
81+
type=str,
82+
required=True,
83+
help="Dockerリポジトリ名(例: voicevox/voicevox_engine)",
84+
)
85+
parser.add_argument(
86+
"--version",
87+
type=str,
88+
default="latest",
89+
help='バージョン文字列(例: "0.22.0", "latest")',
90+
)
91+
parser.add_argument(
92+
"--prefix",
93+
type=str,
94+
default="",
95+
help='カンマ区切りのプレフィックス(例: ",cpu,cpu-ubuntu22.04", "nvidia,nvidia-ubuntu22.04")',
96+
)
97+
98+
args = parser.parse_args()
99+
100+
repository: str = args.repository
101+
version: str = args.version
102+
comma_separated_prefix: str = args.prefix
103+
104+
# Dockerイメージ名を生成
105+
docker_image_names = generate_docker_image_names(
106+
repository=repository,
107+
version=version,
108+
comma_separated_prefix=comma_separated_prefix,
109+
)
110+
111+
# 標準出力に出力
112+
for docker_image_name in docker_image_names:
113+
print(docker_image_name)
114+
115+
116+
if __name__ == "__main__":
117+
main()

0 commit comments

Comments
 (0)