mirror of
https://github.com/Tencent/WeKnora.git
synced 2025-11-25 19:37:45 +08:00
chore: upspeed build docker
This commit is contained in:
201
.github/workflows/docker-image.yml
vendored
201
.github/workflows/docker-image.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: Build and Push Docker Image
|
name: Build and Push Docker Image
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
@@ -9,50 +11,128 @@ concurrency:
|
|||||||
cancel-in-progress: false
|
cancel-in-progress: false
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-images:
|
build-ui:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- service_name: ui
|
|
||||||
file: frontend/Dockerfile
|
|
||||||
context: ./frontend
|
|
||||||
platform: linux/amd64,linux/arm64
|
|
||||||
- service_name: app
|
|
||||||
file: docker/Dockerfile.app
|
|
||||||
context: .
|
|
||||||
platform: linux/amd64,linux/arm64
|
|
||||||
- service_name: docreader
|
|
||||||
file: docker/Dockerfile.docreader
|
|
||||||
context: .
|
|
||||||
platform: linux/amd64,linux/arm64
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-ui
|
||||||
|
|
||||||
- name: Prepare version info
|
- name: Prepare version info
|
||||||
id: version
|
id: version
|
||||||
run: |
|
run: |
|
||||||
# 使用统一的版本管理脚本
|
# 使用统一的版本管理脚本
|
||||||
eval "$(./scripts/get_version.sh env)"
|
eval "$(./scripts/get_version.sh env)"
|
||||||
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Build ui Image
|
||||||
|
uses: docker/build-push-action@v3
|
||||||
|
with:
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: frontend/Dockerfile
|
||||||
|
context: ./frontend
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-ui:cache
|
||||||
|
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-ui:cache,mode=max
|
||||||
|
|
||||||
|
build-docreader:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-docreader
|
||||||
|
|
||||||
|
- name: Prepare version info
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
eval "$(./scripts/get_version.sh env)"
|
||||||
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Build docreader Image
|
||||||
|
uses: docker/build-push-action@v3
|
||||||
|
with:
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: docker/Dockerfile.docreader
|
||||||
|
context: .
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-docreader:cache
|
||||||
|
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-docreader:cache,mode=max
|
||||||
|
|
||||||
|
build-app:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- arch: amd64
|
||||||
|
platform: linux/amd64
|
||||||
|
runs: ubuntu-latest
|
||||||
|
- arch: arm64
|
||||||
|
platform: linux/arm64
|
||||||
|
runs: ubuntu-24.04-arm
|
||||||
|
runs-on: ${{ matrix.runs }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
id: setup-buildx
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-app
|
||||||
|
|
||||||
|
- name: Prepare version info
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
# 使用统一的版本管理脚本
|
||||||
|
eval "$(./scripts/get_version.sh env)"
|
||||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
||||||
echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT
|
echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT
|
||||||
echo "build_time=$BUILD_TIME" >> $GITHUB_OUTPUT
|
echo "build_time=$BUILD_TIME" >> $GITHUB_OUTPUT
|
||||||
echo "go_version=$GO_VERSION" >> $GITHUB_OUTPUT
|
echo "go_version=$GO_VERSION" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
platform=${{ matrix.platform }}
|
||||||
|
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
|
||||||
# 显示版本信息
|
# 显示版本信息
|
||||||
./scripts/get_version.sh info
|
./scripts/get_version.sh info
|
||||||
|
|
||||||
@@ -60,34 +140,85 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
id: cache
|
id: cache
|
||||||
with:
|
with:
|
||||||
path: go-build-cache
|
path: go-pkg-mod
|
||||||
key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }}
|
key: ${{ env.PLATFORM_PAIR }}-go-build-cache-${{ hashFiles('**/go.sum') }}
|
||||||
|
|
||||||
- name: Inject go-build-cache
|
- name: Inject go-build-cache
|
||||||
uses: reproducible-containers/buildkit-cache-dance@v3
|
uses: reproducible-containers/buildkit-cache-dance@v3
|
||||||
with:
|
with:
|
||||||
cache-source: go-build-cache
|
builder: ${{ steps.setup-buildx.outputs.name }}
|
||||||
cache-map: |
|
cache-map: |
|
||||||
{
|
{
|
||||||
"go-pkg-mod": "/go/pkg/mod"
|
"go-pkg-mod": "/go/pkg/mod"
|
||||||
}
|
}
|
||||||
skip-extraction: ${{ steps.cache.outputs.cache-hit }}
|
skip-extraction: ${{ steps.cache.outputs.cache-hit }}
|
||||||
|
|
||||||
- name: Build ${{ matrix.service_name }} Image
|
- name: Build app Image
|
||||||
|
id: build
|
||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v3
|
||||||
with:
|
with:
|
||||||
push: true
|
push: true
|
||||||
platforms: ${{ matrix.platform }}
|
platforms: ${{ matrix.platform }}
|
||||||
file: ${{ matrix.file }}
|
file: docker/Dockerfile.app
|
||||||
context: ${{ matrix.context }}
|
context: .
|
||||||
build-args: |
|
build-args: |
|
||||||
${{ matrix.service_name == 'app' && format('VERSION_ARG={0}', steps.version.outputs.version) || '' }}
|
${{ format('VERSION_ARG={0}', steps.version.outputs.version) }}
|
||||||
${{ matrix.service_name == 'app' && format('COMMIT_ID_ARG={0}', steps.version.outputs.commit_id) || '' }}
|
${{ format('COMMIT_ID_ARG={0}', steps.version.outputs.commit_id) }}
|
||||||
${{ matrix.service_name == 'app' && format('BUILD_TIME_ARG={0}', steps.version.outputs.build_time) || '' }}
|
${{ format('BUILD_TIME_ARG={0}', steps.version.outputs.build_time) }}
|
||||||
${{ matrix.service_name == 'app' && format('GO_VERSION_ARG={0}', steps.version.outputs.go_version) || '' }}
|
${{ format('GO_VERSION_ARG={0}', steps.version.outputs.go_version) }}
|
||||||
tags: |
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:latest
|
tags: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-app
|
||||||
${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:${{ steps.version.outputs.version }}
|
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-app:cache-${{ env.PLATFORM_PAIR }}
|
||||||
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache
|
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-app:cache-${{ env.PLATFORM_PAIR }},mode=max
|
||||||
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache,mode=max
|
outputs: type=image,push-by-digest=true,name-canonical=true,push=true
|
||||||
|
|
||||||
|
- name: Export digest
|
||||||
|
run: |
|
||||||
|
mkdir -p ${{ runner.temp }}/digests
|
||||||
|
digest="${{ steps.build.outputs.digest }}"
|
||||||
|
touch "${{ runner.temp }}/digests/${digest#sha256:}"
|
||||||
|
|
||||||
|
- name: Upload digest
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: digests-${{ env.PLATFORM_PAIR }}
|
||||||
|
path: ${{ runner.temp }}/digests/*
|
||||||
|
if-no-files-found: error
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
merge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs:
|
||||||
|
- build-app
|
||||||
|
steps:
|
||||||
|
- name: Download digests
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
path: ${{ runner.temp }}/digests
|
||||||
|
pattern: digests-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ secrets.DOCKERHUB_USERNAME }}/weknora-app
|
||||||
|
|
||||||
|
- name: Create manifest list and push
|
||||||
|
working-directory: ${{ runner.temp }}/digests
|
||||||
|
run: |
|
||||||
|
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
|
||||||
|
$(printf '${{ secrets.DOCKERHUB_USERNAME }}/weknora-app@sha256:%s ' *)
|
||||||
|
|
||||||
|
- name: Inspect image
|
||||||
|
run: |
|
||||||
|
docker buildx imagetools inspect ${{ secrets.DOCKERHUB_USERNAME }}/weknora-app:${{ steps.meta.outputs.version }}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate
|
|||||||
# Copy go mod and sum files
|
# Copy go mod and sum files
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
||||||
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Get version and commit info for build injection
|
# Get version and commit info for build injection
|
||||||
@@ -40,6 +39,7 @@ ENV GO_VERSION=${GO_VERSION_ARG}
|
|||||||
|
|
||||||
# Build the application with version info
|
# Build the application with version info
|
||||||
RUN --mount=type=cache,target=/go/pkg/mod make build-prod
|
RUN --mount=type=cache,target=/go/pkg/mod make build-prod
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
|
||||||
|
|
||||||
# Final stage
|
# Final stage
|
||||||
FROM alpine:3.17
|
FROM alpine:3.17
|
||||||
|
|||||||
Reference in New Issue
Block a user