Skip to content

Commit

Permalink
feat: build docker image w/ GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Dec 24, 2024
1 parent b859c48 commit e7664a6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/on-pull-request.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions .github/workflows/on-push-main.yaml
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions .github/workflows/wf-docker-build.yaml
Original file line number Diff line number Diff line change
@@ -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' || '' }}
24 changes: 24 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -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}:"))
}

0 comments on commit e7664a6

Please sign in to comment.