diff --git a/.github/workflows/build-engine-container.yml b/.github/workflows/build-engine-container.yml index 06ffc444b..f859995df 100644 --- a/.github/workflows/build-engine-container.yml +++ b/.github/workflows/build-engine-container.yml @@ -35,59 +35,44 @@ jobs: build-docker: needs: [config] - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest strategy: matrix: - os: [ubuntu-latest] - tag: - - "" - - cpu - - cpu-ubuntu20.04 - - nvidia - - nvidia-ubuntu20.04 - - cpu-ubuntu22.04 - - nvidia-ubuntu22.04 + # 各変数の説明 + # prefixes: Docker tagのプレフィックス。カンマ区切り。空文字列の場合、バージョン文字列のみがタグ名になる + # buildcache_prefix: ビルドキャッシュのプレフィックス。空文字列やカンマは使用不可 + # target: Dockerfileのビルドステージ名 + # base_image: Dockerfileのビルド用ステージのベースイメージ + # base_runtime_image: Dockerfileの実行用ステージのベースイメージ + # onnxruntime_version: ONNX Runtimeのバージョン + # platforms: Dockerのプラットフォームバリアント。カンマ区切り。 参考: https://docs.docker.com/build/building/multi-platform/ include: # Ubuntu 20.04 - - tag: "" + - prefixes: ",cpu,cpu-ubuntu20.04" + buildcache_prefix: "cpu-ubuntu20.04" target: runtime-env base_image: ubuntu:20.04 base_runtime_image: ubuntu:20.04 onnxruntime_version: 1.13.1 platforms: linux/amd64,linux/arm64/v8 - - tag: cpu - target: runtime-env - base_image: ubuntu:20.04 - base_runtime_image: ubuntu:20.04 - onnxruntime_version: 1.13.1 - platforms: linux/amd64,linux/arm64/v8 - - tag: cpu-ubuntu20.04 - target: runtime-env - base_image: ubuntu:20.04 - base_runtime_image: ubuntu:20.04 - onnxruntime_version: 1.13.1 - platforms: linux/amd64,linux/arm64/v8 - - tag: nvidia - target: runtime-nvidia-env - base_image: ubuntu:20.04 - base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04 - onnxruntime_version: 1.13.1 - platforms: linux/amd64 - - tag: nvidia-ubuntu20.04 + - prefixes: "nvidia,nvidia-ubuntu20.04" + buildcache_prefix: "nvidia-ubuntu20.04" target: runtime-nvidia-env base_image: ubuntu:20.04 base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04 onnxruntime_version: 1.13.1 platforms: linux/amd64 # Ubuntu 22.04 - - tag: cpu-ubuntu22.04 + - prefixes: "cpu-ubuntu22.04" + buildcache_prefix: "cpu-ubuntu22.04" target: runtime-env base_image: ubuntu:22.04 base_runtime_image: ubuntu:22.04 onnxruntime_version: 1.13.1 platforms: linux/amd64,linux/arm64/v8 - - tag: nvidia-ubuntu22.04 + - prefixes: "nvidia-ubuntu22.04" + buildcache_prefix: "nvidia-ubuntu22.04" target: runtime-nvidia-env base_image: ubuntu:22.04 base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04 @@ -137,32 +122,43 @@ jobs: DOWNLOAD_RESOURCE_PATH: download/resource run: bash tools/process_voicevox_resource.bash + - name: Generate Docker image names + id: generate-docker-image-names + run: | + # Dockerイメージ名を outputs.tags に改行区切りで格納する + { + echo "tags<> "$GITHUB_OUTPUT" + + # ビルドキャッシュに指定するためのDockerイメージ名を生成する + # NOTE: デフォルトブランチへのコミットの場合のみキャッシュを作成する + - name: Generate Docker buildcache image names + id: generate-docker-buildcache-image-names + run: | + # --cache-from に指定するためのDockerイメージ名 + # 常にデフォルトブランチのビルドキャッシュ(*-latest-buildcache)を使用する + cache_from="type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_prefix }}-latest-buildcache" + + # --cache-to に指定するためのDockerイメージ名 + # リリースの場合、ビルドキャッシュを作成しないため、空文字列を格納する + cache_to="" + if [ "${{ needs.config.outputs.version_or_latest }}" = "latest" ]; then + cache_to="type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_prefix }}-latest-buildcache,mode=max" + fi + + # outputs に格納する + echo "cache-from=$cache_from" >> "$GITHUB_OUTPUT" + echo "cache-to=$cache_to" >> "$GITHUB_OUTPUT" + - name: Build and Deploy Docker image uses: docker/build-push-action@v5 - env: - IMAGE_TAG: - |- # If it's a release, add the version, otherwise add the `latest` - ${{ ( - matrix.tag != '' && ( - format('{0}:{1}-{2}', env.IMAGE_NAME, matrix.tag, needs.config.outputs.version_or_latest) - ) || format('{0}:{1}', env.IMAGE_NAME, needs.config.outputs.version_or_latest) - ) }} - IMAGE_CACHE_FROM: - |- # Always use the `latest` buildcache. :latest-buildcache or :{tag}-latest-buildcache - ${{ ( - matrix.tag != '' && ( - format('type=registry,ref={0}:{1}-latest-buildcache', env.IMAGE_NAME, matrix.tag) - ) || format('type=registry,ref={0}:latest-buildcache', env.IMAGE_NAME) - ) }} - IMAGE_CACHE_TO: - |- # If it's a release, do not create buildcache, otherwise create the `latest` buildcache. :latest-buildcache or :{tag}-latest-buildcache - ${{ ( - needs.config.outputs.version_or_latest == 'latest' && ( - matrix.tag != '' && ( - format('type=registry,ref={0}:{1}-latest-buildcache,mode=max', env.IMAGE_NAME, matrix.tag) - ) || format('type=registry,ref={0}:latest-buildcache,mode=max', env.IMAGE_NAME) - ) || '' - ) }} with: context: . builder: ${{ steps.buildx.outputs.name }} @@ -178,9 +174,9 @@ jobs: ONNXRUNTIME_VERSION=${{ matrix.onnxruntime_version }} target: ${{ matrix.target }} push: true - tags: ${{ env.IMAGE_TAG }} - cache-from: ${{ env.IMAGE_CACHE_FROM }} - cache-to: ${{ env.IMAGE_CACHE_TO }} + tags: ${{ steps.generate-docker-image-names.outputs.tags }} + cache-from: ${{ steps.generate-docker-buildcache-image-names.outputs.cache-from }} + cache-to: ${{ steps.generate-docker-buildcache-image-names.outputs.cache-to }} platforms: ${{ matrix.platforms }} run-release-test-workflow: diff --git a/poetry.lock b/poetry.lock index ae5887a3c..584b69eea 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2320,6 +2320,7 @@ files = [ {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f66efbc1caa63c088dead1c4170d148eabc9b80d95fb75b6c92ac0aad2437d76"}, {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:22353049ba4181685023b25b5b51a574bce33e7f51c759371a7422dcae5402a6"}, {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:932205970b9f9991b34f55136be327501903f7c66830e9760a8ffb15b07f05cd"}, + {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:a52d48f4e7bf9005e8f0a89209bf9a73f7190ddf0489eee5eb51377385f59f2a"}, {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-win32.whl", hash = "sha256:3eac5a91891ceb88138c113f9db04f3cebdae277f5d44eaa3651a4f573e6a5da"}, {file = "ruamel.yaml.clib-0.2.12-cp310-cp310-win_amd64.whl", hash = "sha256:ab007f2f5a87bd08ab1499bdf96f3d5c6ad4dcfa364884cb4549aa0154b13a28"}, {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-macosx_13_0_arm64.whl", hash = "sha256:4a6679521a58256a90b0d89e03992c15144c5f3858f40d7c18886023d7943db6"}, @@ -2328,6 +2329,7 @@ files = [ {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:811ea1594b8a0fb466172c384267a4e5e367298af6b228931f273b111f17ef52"}, {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:cf12567a7b565cbf65d438dec6cfbe2917d3c1bdddfce84a9930b7d35ea59642"}, {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:7dd5adc8b930b12c8fc5b99e2d535a09889941aa0d0bd06f4749e9a9397c71d2"}, + {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:1492a6051dab8d912fc2adeef0e8c72216b24d57bd896ea607cb90bb0c4981d3"}, {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-win32.whl", hash = "sha256:bd0a08f0bab19093c54e18a14a10b4322e1eacc5217056f3c063bd2f59853ce4"}, {file = "ruamel.yaml.clib-0.2.12-cp311-cp311-win_amd64.whl", hash = "sha256:a274fb2cb086c7a3dea4322ec27f4cb5cc4b6298adb583ab0e211a4682f241eb"}, {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:20b0f8dc160ba83b6dcc0e256846e1a02d044e13f7ea74a3d1d56ede4e48c632"}, @@ -2336,6 +2338,7 @@ files = [ {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:749c16fcc4a2b09f28843cda5a193e0283e47454b63ec4b81eaa2242f50e4ccd"}, {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:bf165fef1f223beae7333275156ab2022cffe255dcc51c27f066b4370da81e31"}, {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:32621c177bbf782ca5a18ba4d7af0f1082a3f6e517ac2a18b3974d4edf349680"}, + {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:b82a7c94a498853aa0b272fd5bc67f29008da798d4f93a2f9f289feb8426a58d"}, {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-win32.whl", hash = "sha256:e8c4ebfcfd57177b572e2040777b8abc537cdef58a2120e830124946aa9b42c5"}, {file = "ruamel.yaml.clib-0.2.12-cp312-cp312-win_amd64.whl", hash = "sha256:0467c5965282c62203273b838ae77c0d29d7638c8a4e3a1c8bdd3602c10904e4"}, {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:4c8c5d82f50bb53986a5e02d1b3092b03622c02c2eb78e29bec33fd9593bae1a"}, @@ -2344,6 +2347,7 @@ files = [ {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:96777d473c05ee3e5e3c3e999f5d23c6f4ec5b0c38c098b3a5229085f74236c6"}, {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-musllinux_1_1_i686.whl", hash = "sha256:3bc2a80e6420ca8b7d3590791e2dfc709c88ab9152c00eeb511c9875ce5778bf"}, {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:e188d2699864c11c36cdfdada94d781fd5d6b0071cd9c427bceb08ad3d7c70e1"}, + {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:4f6f3eac23941b32afccc23081e1f50612bdbe4e982012ef4f5797986828cd01"}, {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-win32.whl", hash = "sha256:6442cb36270b3afb1b4951f060eccca1ce49f3d087ca1ca4563a6eb479cb3de6"}, {file = "ruamel.yaml.clib-0.2.12-cp313-cp313-win_amd64.whl", hash = "sha256:e5b8daf27af0b90da7bb903a876477a9e6d7270be6146906b276605997c7e9a3"}, {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-macosx_12_0_arm64.whl", hash = "sha256:fc4b630cd3fa2cf7fce38afa91d7cfe844a9f75d7f0f36393fa98815e911d987"}, @@ -2352,6 +2356,7 @@ files = [ {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e2f1c3765db32be59d18ab3953f43ab62a761327aafc1594a2a1fbe038b8b8a7"}, {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:d85252669dc32f98ebcd5d36768f5d4faeaeaa2d655ac0473be490ecdae3c285"}, {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:e143ada795c341b56de9418c58d028989093ee611aa27ffb9b7f609c00d813ed"}, + {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:2c59aa6170b990d8d2719323e628aaf36f3bfbc1c26279c0eeeb24d05d2d11c7"}, {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-win32.whl", hash = "sha256:beffaed67936fbbeffd10966a4eb53c402fafd3d6833770516bf7314bc6ffa12"}, {file = "ruamel.yaml.clib-0.2.12-cp39-cp39-win_amd64.whl", hash = "sha256:040ae85536960525ea62868b642bdb0c2cc6021c9f9d507810c0c604e66f5a7b"}, {file = "ruamel.yaml.clib-0.2.12.tar.gz", hash = "sha256:6c8fbb13ec503f99a91901ab46e0b07ae7941cd527393187039aec586fdfd36f"}, diff --git a/tools/generate_docker_image_names.py b/tools/generate_docker_image_names.py new file mode 100644 index 000000000..c3abc34b9 --- /dev/null +++ b/tools/generate_docker_image_names.py @@ -0,0 +1,117 @@ +""" +Dockerリポジトリ名、バージョン文字列、カンマ区切りのプレフィックスを受け取り、 +バージョン文字列付きのDockerイメージ名を改行区切りで標準出力に出力する + +例 +$ python3 ./tools/generate_docker_image_names.py \ + --repository "REPO" \ + --version "VER" \ + --prefix ",A,B" +REPO:VER +REPO:A-VER +REPO:B-VER + +$ python3 ./tools/generate_docker_image_names.py \ + --repository "REPO" \ + --version "VER" \ + --prefix "" +REPO:VER +""" + +from argparse import ArgumentParser + + +def generate_docker_image_names( + repository: str, + version: str, + comma_separated_prefix: str, +) -> list[str]: + """ + Dockerリポジトリ名、バージョン文字列、カンマ区切りのプレフィックスを受け取り、 + バージョン文字列付きのDockerイメージ名を配列で返す + + prefixが空文字列でない場合、"{prefix}-{version}"をタグにする + + - 例: repository="REPO", version="VER", prefix="A" -> "REPO:A-VER" + + prefixが空文字列の場合、"{version}"をタグにする + + - 例: repository="REPO", version="VER", prefix="" -> "REPO:VER" + + Parameters + ---------- + repository : str + Dockerリポジトリ名 + version : str + バージョン文字列 + comma_separated_prefix : str + カンマ区切りのプレフィックス + + Returns + ------- + list[str] + Dockerイメージ名の配列。 + + Examples + -------- + >>> generate_docker_image_names("voicevox/voicevox_engine", "0.22.0", "cpu,cpu-ubuntu22.04") + ['voicevox/voicevox_engine:0.22.0', + 'voicevox/voicevox_engine:cpu-0.22.0', + 'voicevox/voicevox_engine:cpu-ubuntu22.04-0.22.0'] + """ + # カンマ区切りのタグ名を配列に変換 + prefixes = comma_separated_prefix.split(",") + + # 戻り値の配列 + docker_image_names: list[str] = [] + + for prefix in prefixes: + # プレフィックスが空文字列でない場合、末尾にハイフンを付ける + if prefix: + prefix = f"{prefix}-" + docker_image_names.append(f"{repository}:{prefix}{version}") + + return docker_image_names + + +def main() -> None: + parser = ArgumentParser() + parser.add_argument( + "--repository", + type=str, + required=True, + help="Dockerリポジトリ名(例: voicevox/voicevox_engine)", + ) + parser.add_argument( + "--version", + type=str, + default="latest", + help='バージョン文字列(例: "0.22.0", "latest")', + ) + parser.add_argument( + "--prefix", + type=str, + default="", + help='カンマ区切りのプレフィックス(例: ",cpu,cpu-ubuntu22.04", "nvidia,nvidia-ubuntu22.04")', + ) + + args = parser.parse_args() + + repository: str = args.repository + version: str = args.version + comma_separated_prefix: str = args.prefix + + # Dockerイメージ名を生成 + docker_image_names = generate_docker_image_names( + repository=repository, + version=version, + comma_separated_prefix=comma_separated_prefix, + ) + + # 標準出力に出力 + for docker_image_name in docker_image_names: + print(docker_image_name) + + +if __name__ == "__main__": + main()