From e7664a6183de64c07ce5c45e2ba0fbabf84c7a91 Mon Sep 17 00:00:00 2001 From: tosuke <13393900+tosuke@users.noreply.github.com> Date: Tue, 24 Dec 2024 15:55:12 +0900 Subject: [PATCH] feat: build docker image w/ GHA --- .github/workflows/on-pull-request.yaml | 12 ++++++ .github/workflows/on-push-main.yaml | 13 ++++++ .github/workflows/wf-docker-build.yaml | 55 ++++++++++++++++++++++++++ docker-bake.hcl | 24 +++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .github/workflows/on-pull-request.yaml create mode 100644 .github/workflows/on-push-main.yaml create mode 100644 .github/workflows/wf-docker-build.yaml create mode 100644 docker-bake.hcl diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml new file mode 100644 index 00000000..7480d5f8 --- /dev/null +++ b/.github/workflows/on-pull-request.yaml @@ -0,0 +1,12 @@ +name: On Pull Request + +on: + pull_request: + +jobs: + build: + name: Build + uses: ./.github/workflows/wf-docker-build.yaml + with: + push-ghcr: true + save-cache: true diff --git a/.github/workflows/on-push-main.yaml b/.github/workflows/on-push-main.yaml new file mode 100644 index 00000000..0ce609f4 --- /dev/null +++ b/.github/workflows/on-push-main.yaml @@ -0,0 +1,13 @@ +name: On Push Main + +on: + push: + branches: [main] + +jobs: + build: + name: Build + uses: ./.github/workflows/wf-docker-build.yaml + with: + push-ghcr: false + save-cache: false diff --git a/.github/workflows/wf-docker-build.yaml b/.github/workflows/wf-docker-build.yaml new file mode 100644 index 00000000..fc4587a6 --- /dev/null +++ b/.github/workflows/wf-docker-build.yaml @@ -0,0 +1,55 @@ +name: Build Docker Images + +on: + workflow_call: + inputs: + push-ghcr: + type: boolean + default: false + save-cache: + type: boolean + default: false + +jobs: + build_and_push: + name: Build and Push + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v3 + - uses: docker/setup-qemu-action@v3 + + - if: inputs.push-ghcr + name: Login to ghcr.io + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Docker Metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ inputs.push-ghcr && format('ghcr.io/{0}', github.repository) || '' }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=ref,event=pr + type=sha + - name: Build and Push Artifacts + uses: docker/bake-action@v5 + with: + push: true + # Metadata Action のタグ出力を加工したいのでここではラベルのみを渡す + # タグの情報は環境変数 DOCKER_METADATA_OUTPUT_TAGS を使って受け取る + files: | + docker-bake.hcl + ${{ steps.meta.outputs.bake-file-labels }} + # 基本的に main ブランチのキャッシュしか共有できないのでキャッシュの保存はオプトインにしておく + set: | + *.cache-from=type=gha + ${{ inputs.save-cache && '*.cache-to=type=gha,mode=max' || '' }} diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 00000000..9e80ac86 --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,24 @@ +group "default" { + targets = ["backend"] +} + +target "docker-metadata-action" {} + +target "backend" { + inherits = ["docker-metadata-action"] + context = "./backend" + matrix = { + image = ["scoreserver"] + } + name = "backend-${image}" + tags = make_tags("${image}") + target = "${image}" +} + +variable "DOCKER_METADATA_OUTPUT_TAGS" { + default = "" +} +function "make_tags" { + params = [ns] + result = split("\n", replace("${DOCKER_METADATA_OUTPUT_TAGS}", ":", "/${ns}:")) +}