Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DockerビルドCI: イメージタグの別名を別々にビルドしないようにする #1498

Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2e0d450
Docker: イメージタグの別名を別々にビルドしないようにする
aoirint Dec 2, 2024
3e180d6
create tools/generate_docker_image_names.bash
aoirint Dec 2, 2024
fa77f87
comment: quote shell args
aoirint Dec 2, 2024
d4bb31f
comment: improve examples
aoirint Dec 2, 2024
3ef0338
comment: improve examples
aoirint Dec 2, 2024
bfaefce
comment: improve examples
aoirint Dec 2, 2024
cff34d6
comment: 冗長な例を削除
aoirint Dec 2, 2024
bdf3ec4
Merge branch 'master' into feature/docker-commonize-hash-of-same-feat…
aoirint Dec 2, 2024
7cdda64
-eq -> =
aoirint Dec 2, 2024
0269f25
fix SC2086: double quote $GITHUB_OUTPUT
aoirint Dec 2, 2024
45f9db6
fix wrong step output reference
aoirint Dec 2, 2024
6c9e331
remove shell: bash
aoirint Dec 2, 2024
8e45a97
コンフリクト解消時にUbuntu 22.04のincludeが増殖していたのを削除
aoirint Dec 2, 2024
45fe81e
print usage to stderr
aoirint Dec 2, 2024
74415b5
add matrix var descriptions
aoirint Dec 2, 2024
a1cefe8
add comment
aoirint Dec 2, 2024
4a9bd5f
fix comment
aoirint Dec 2, 2024
5185c6a
update comment
aoirint Dec 2, 2024
5d694c3
fix buildcache image name: add repository
aoirint Dec 2, 2024
2553d7e
remove typo quote
aoirint Dec 2, 2024
ccf960c
create tools/generate_docker_image_names.py
aoirint Dec 3, 2024
42e98d3
pysen run format
aoirint Dec 3, 2024
06ea55f
update usage
aoirint Dec 3, 2024
4f68687
fix B950
aoirint Dec 3, 2024
6f2a03c
Bashの行コメント"#"をdocstringに変えたときに消し忘れていたので削除
aoirint Dec 3, 2024
d0967b7
コメントの表現修正: タグ名 -> Dockerイメージ名
aoirint Dec 3, 2024
7f195e9
コメントの表現修正: タグ名 -> Dockerイメージ名
aoirint Dec 3, 2024
a7d0ce4
コメントの表現修正: タグ名 -> Dockerイメージ名
aoirint Dec 3, 2024
ca8b31b
add --repository argument help
aoirint Dec 3, 2024
71e6e99
コメントの表現修正: 空文字 -> 空文字列
aoirint Dec 3, 2024
8695207
helpの出力を末尾句点なしに統一
aoirint Dec 3, 2024
7acfa20
Update .github/workflows/build-engine-container.yml
aoirint Dec 10, 2024
f15175f
Update .github/workflows/build-engine-container.yml
aoirint Dec 10, 2024
97a6d9b
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
ca5583c
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
e4749ad
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
a480d21
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
8a46527
Update .github/workflows/build-engine-container.yml
aoirint Dec 10, 2024
a6fbb82
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
2c6b51c
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
5f6de21
Update tools/generate_docker_image_names.py
aoirint Dec 10, 2024
57e2f2d
pysen run format
aoirint Dec 10, 2024
07568fe
Merge remote-tracking branch 'origin/master' into feature/docker-comm…
aoirint Dec 10, 2024
f4ca78d
Update .github/workflows/build-engine-container.yml
aoirint Dec 12, 2024
093a46d
Update .github/workflows/build-engine-container.yml
aoirint Dec 12, 2024
262f25d
Update tools/generate_docker_image_names.py
aoirint Dec 12, 2024
039bf8c
Update .github/workflows/build-engine-container.yml
aoirint Dec 12, 2024
887e252
Update .github/workflows/build-engine-container.yml
aoirint Dec 12, 2024
bc57edf
Update tools/generate_docker_image_names.py
aoirint Dec 12, 2024
b6336d4
Update .github/workflows/build-engine-container.yml
aoirint Dec 12, 2024
260ff69
rename matrix.tags -> matrix.prefixes
aoirint Dec 12, 2024
195ec6c
rename buildcache_tag -> buildcache_prefix
aoirint Dec 12, 2024
83020ef
lockファイルに更新があったっぽいので追従
Hiroshiba Dec 12, 2024
1c577c6
generate_docker_image_names: styling arguments
aoirint Dec 12, 2024
d4b66a9
Merge remote-tracking branch 'hiroshiba/lockファイルに更新があったっぽいので追従' into …
aoirint Dec 12, 2024
c60808b
Update .github/workflows/build-engine-container.yml
Hiroshiba Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 74 additions & 59 deletions .github/workflows/build-engine-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 各変数の説明
# tags: カンマ区切りのタグ名(バージョン文字列の前のプレフィックス)。空文字列の場合、バージョン文字列のみのタグ名になる
aoirint marked this conversation as resolved.
Show resolved Hide resolved
# buildcache_tag: ビルドキャッシュのタグ名(latestの前のプレフィックス)。空文字列やカンマは使用不可
aoirint marked this conversation as resolved.
Show resolved Hide resolved
# target: Dockerfile のビルドステージ名
# base_image: Dockerfile のビルド変数 BASE_IMAGE に渡すベースイメージ
# base_runtime_image: Dockerfile のビルド変数 BASE_RUNTIME_IMAGE に渡す実行ステージ用のベースイメージ
# onnxruntime_version: Dockerfile のビルド変数 ONNXRUNTIME_VERSION に渡すONNX Runtimeのバージョン
# platforms: docker build コマンドのオプション --platform に渡すプラットフォーム
aoirint marked this conversation as resolved.
Show resolved Hide resolved
include:
# Ubuntu 20.04
- tag: ""
- tags: ",cpu,cpu-ubuntu20.04"
buildcache_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: 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
- tags: "nvidia,nvidia-ubuntu20.04"
buildcache_tag: "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
- tags: "cpu-ubuntu22.04"
buildcache_tag: "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
- tags: "nvidia-ubuntu22.04"
buildcache_tag: "nvidia-ubuntu22.04"
target: runtime-nvidia-env
base_image: ubuntu:22.04
base_runtime_image: nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04
Expand Down Expand Up @@ -137,32 +122,62 @@ jobs:
DOWNLOAD_RESOURCE_PATH: download/resource
run: bash tools/process_voicevox_resource.bash

# Matrixのtags(カンマ区切りのタグ名)から、バージョン文字列付きのタグ名を生成する
#
# 例1: tags=",cpu,cpu-ubuntu22.04", version="0.22.0"
# voicevox/voicevox_engine:0.22.0
# voicevox/voicevox_engine:cpu-0.22.0
# voicevox/voicevox_engine:cpu-ubuntu22.04-0.22.0
#
# 例2: tags="nvidia,nvidia-ubuntu22.04", version="latest"
# voicevox/voicevox_engine:nvidia-latest
# voicevox/voicevox_engine:nvidia-ubuntu22.04-latest
- name: <Build> Generate Docker image names
id: generate-docker-image-names
run: |
# バージョン文字列付きのタグ名を steps.generate-docker-image-names.outputs.tags に改行区切りで格納する
{
echo "tags<<EOF"

# Usage: $0 <repository> [version_or_latest] [comma_separated_prefixes]
bash tools/generate_docker_image_names.bash "${{ env.IMAGE_NAME }}" "${{ needs.config.outputs.version_or_latest }}" "${{ matrix.tags }}"

echo "EOF"
} >> "$GITHUB_OUTPUT"

# ビルドキャッシュ(docker build コマンドのオプション --cache-from と --cache-to)に指定するためのタグ名を生成する
#
# 例1: buildcache_tag="cpu-ubuntu22.04", version="latest"
# cache-from: type=registry,ref=voicevox/voicevox_engine:cpu-ubuntu22.04-latest-buildcache
# cache-to: type=registry,ref=voicevox/voicevox_engine:cpu-ubuntu22.04-latest-buildcache,mode=max
#
# 例2: buildcache_tag="nvidia-ubuntu22.04", version="0.22.0"
# cache-from: type=registry,ref=voicevox/voicevox_engine:nvidia-ubuntu22.04-latest-buildcache
# cache-to: (空文字列)
- name: <Build> Generate Docker buildcache image names
id: generate-docker-buildcache-image-names
run: |
# --cache-from に指定するためのタグ名を steps.generate-docker-buildcache-image-names.outputs.cache-from に格納する
# 常にデフォルトブランチのビルドキャッシュ(*-latest-buildcache)を使用する
# 例: type=registry,ref=voicevox/voicevox_engine:cpu-ubuntu22.04-latest-buildcache
echo "cache-from=type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_tag }}-latest-buildcache" >> "$GITHUB_OUTPUT"

# --cache-to に指定するためのタグ名を steps.generate-docker-buildcache-image-names.outputs.cache-to に格納する
# リリースの場合、ビルドキャッシュを作成しないため、空文字列を格納する
# 例: type=registry,ref=voicevox/voicevox_engine:cpu-ubuntu22.04-latest-buildcache,mode=max
{
echo "cache-to<<EOF"

if [ "${{ needs.config.outputs.version_or_latest }}" = "latest" ]; then
# デフォルトブランチへのコミットの場合( needs.config.outputs.version_or_latest = "latest" )、ビルドキャッシュを作成する
echo "type=registry,ref=${{ env.IMAGE_NAME }}:${{ matrix.buildcache_tag }}-latest-buildcache,mode=max"
fi

echo "EOF"
} >> "$GITHUB_OUTPUT"

- name: <Build/Deploy> 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 }}
Expand All @@ -178,9 +193,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:
Expand Down
57 changes: 57 additions & 0 deletions tools/generate_docker_image_names.bash
aoirint marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Dockerリポジトリ名、バージョン文字列、カンマ区切りのプレフィックスを受け取り、
# バージョン文字列付きのDockerイメージ名を改行区切りで標準出力に出力する
#
# 例1
# $ ./tools/generate_docker_image_names.bash "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
#
# 例2
# $ ./tools/generate_docker_image_names.bash "voicevox/voicevox_engine" "latest" "nvidia,nvidia-ubuntu22.04"
# voicevox/voicevox_engine:nvidia-latest
# voicevox/voicevox_engine:nvidia-ubuntu22.04-latest
#
# 例3
# $ ./tools/generate_docker_image_names.bash "voicevox/voicevox_engine" "latest" ""
# voicevox/voicevox_engine:latest

set -eux

if [ $# -lt 1 ]; then
# 引数の数が1未満の場合、ヘルプを標準エラー出力に出力して終了する
echo "Usage: $0 <repository> [version_or_latest] [comma_separated_prefixes]" 1>&2
exit 1
fi

repository=$1
version_or_latest=${2:-latest}
comma_separated_prefixes=${3:-}

# カンマ区切りのタグ名を配列に読み込んで、forループで反復できるようにする
# 例1: "" -> ("")
# 例2: ",cpu,cpu-ubuntu22.04" -> ("" "cpu" "cpu-ubuntu22.04")
# 例3: "nvidia,nvidia-ubuntu22.04" -> ("nvidia" "nvidia-ubuntu22.04")
IFS=',' read -r -a prefixes <<< "${comma_separated_prefixes}"

# prefixesが空(comma_separated_prefixesが空文字列)の場合、prefixesに空文字列を追加する
if [ ${#prefixes[@]} = 0 ]; then
prefixes+=("")
fi

# バージョン文字列付きのDockerイメージ名を改行区切りで出力する
for prefix in "${prefixes[@]}"; do
if [ -z "${prefix}" ]; then
# prefixが空文字列の場合、"{バージョン文字列}"をタグにする
# 例1: prefix="", version="latest" -> "voicevox/voicevox_engine:latest"
# 例2: prefix="", version="0.22.0" -> "voicevox/voicevox_engine:0.22.0"
echo "${repository}:${version_or_latest}"
else
# prefixが空文字列でない場合、"{prefix}-{バージョン文字列}"をタグにする
# 例1: prefix="cpu", version="latest" -> "voicevox/voicevox_engine:cpu-latest"
# 例2: prefix="nvidia-ubuntu22.04", version="0.22.0" -> "voicevox/voicevox_engine:nvidia-ubuntu22.04-0.22.0"
echo "${repository}:${prefix}-${version_or_latest}"
fi
done