Build Docker #102
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Docker | |
on: | |
push: | |
branches: | |
- master | |
release: | |
types: | |
- created | |
workflow_dispatch: | |
inputs: | |
version: | |
description: "バージョン情報(A.BB.C / A.BB.C-preview.D)" | |
required: true | |
env: | |
PYTHON_VERSION: "3.11.9" | |
IMAGE_NAME: ghcr.io/aivis-project/aivisspeech-engine | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
config: # 全 jobs で利用する定数の定義. `env` が利用できないコンテキストでも利用できる. | |
runs-on: ubuntu-latest | |
outputs: | |
version_or_latest: ${{ steps.vars.outputs.version_or_latest }} | |
steps: | |
- name: <Setup> Declare variables | |
id: vars | |
run: | | |
: # releaseタグ名か、workflow_dispatchでのバージョン名か、latestが入る | |
echo "version_or_latest=${{ github.event.release.tag_name || github.event.inputs.version || 'latest' }}" >> "$GITHUB_OUTPUT" | |
build-docker: | |
needs: [config] | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
# 各変数の説明 | |
# prefixes: Docker tagのプレフィックス。カンマ区切り。空文字列の場合、バージョン文字列のみがタグ名になる | |
# buildcache_prefix: ビルドキャッシュのプレフィックス。空文字列やカンマは使用不可 | |
# target: Dockerfileのビルドステージ名 | |
# base_image: Dockerfileのビルド用ステージのベースイメージ | |
# base_runtime_image: Dockerfileの実行用ステージのベースイメージ | |
# platforms: Dockerのプラットフォームバリアント。カンマ区切り。 参考: https://docs.docker.com/build/building/multi-platform/ | |
include: | |
# Ubuntu 20.04 | |
- prefixes: "cpu-ubuntu20.04" | |
buildcache_prefix: "cpu-ubuntu20.04" | |
target: runtime-env | |
base_image: ubuntu:20.04 | |
base_runtime_image: ubuntu:20.04 | |
platforms: linux/amd64,linux/arm64/v8 | |
- prefixes: "nvidia-ubuntu20.04" | |
buildcache_prefix: "nvidia-ubuntu20.04" | |
target: runtime-nvidia-env | |
base_image: ubuntu:20.04 | |
base_runtime_image: nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04 | |
platforms: linux/amd64 | |
# Ubuntu 22.04 | |
- prefixes: ",cpu,cpu-ubuntu22.04" | |
buildcache_prefix: "cpu-ubuntu22.04" | |
target: runtime-env | |
base_image: ubuntu:22.04 | |
base_runtime_image: ubuntu:22.04 | |
platforms: linux/amd64,linux/arm64/v8 | |
- prefixes: "nvidia,nvidia-ubuntu22.04" | |
buildcache_prefix: "nvidia-ubuntu22.04" | |
target: runtime-nvidia-env | |
base_image: ubuntu:22.04 | |
base_runtime_image: nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04 | |
platforms: linux/amd64 | |
steps: | |
- name: <Setup> Check out the repository | |
uses: actions/checkout@v4 | |
- name: <Setup> Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: <Setup> Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: <Setup> Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: <Build> Generate Docker image names | |
id: generate-docker-image-names | |
run: | | |
# Dockerイメージ名を outputs.tags に改行区切りで格納する | |
{ | |
echo "tags<<EOF" | |
python3 tools/generate_docker_image_names.py \ | |
--repository "${{ env.IMAGE_NAME }}" \ | |
--version "${{ needs.config.outputs.version_or_latest }}" \ | |
--prefix "${{ matrix.prefixes }}" | |
echo "EOF" | |
} >> "$GITHUB_OUTPUT" | |
# ビルドキャッシュに指定するためのDockerイメージ名を生成する | |
# NOTE: デフォルトブランチへのコミットの場合のみキャッシュを作成する | |
- name: <Build> 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/Deploy> Build and Deploy Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
builder: ${{ steps.buildx.outputs.name }} | |
file: ./Dockerfile | |
build-args: | | |
BASE_IMAGE=${{ matrix.base_image }} | |
BASE_RUNTIME_IMAGE=${{ matrix.base_runtime_image }} | |
PYTHON_VERSION=${{ env.PYTHON_VERSION }} | |
AIVISSPEECH_ENGINE_VERSION=${{ needs.config.outputs.version_or_latest }} | |
target: ${{ matrix.target }} | |
push: true | |
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: | |
# version が指定されている場合のみ実行する | |
if: needs.config.outputs.version_or_latest != 'latest' | |
needs: [config, build-docker] | |
uses: ./.github/workflows/test-engine-container.yml | |
with: | |
version: ${{ needs.config.outputs.version_or_latest }} | |
repo_url: ${{ format('{0}/{1}', github.server_url, github.repository) }} # このリポジトリのURL |