Files
LangBot/.github/workflows/build-docker-image.yml
Copilot 7a10dfdac1 refactor: parallelize Docker multi-arch builds (arm64/amd64) (#1774)
* Initial plan

* refactor: parallelize Docker image builds for arm64 and amd64

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* security: add explicit GITHUB_TOKEN permissions to workflow jobs

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* refactor: use build cache instead of intermediate tags

Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>

* ci: perf trigger

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
Co-authored-by: Junyan Qin <rockchinq@gmail.com>
2025-11-11 18:19:35 +08:00

120 lines
3.9 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Build Docker Image
on:
## 发布release的时候会自动构建
release:
types: [published]
jobs:
prepare:
runs-on: ubuntu-latest
name: Prepare build metadata
permissions:
contents: read
outputs:
version: ${{ steps.check_version.outputs.version }}
is_prerelease: ${{ github.event.release.prerelease }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false
- name: judge has env GITHUB_REF # 如果没有GITHUB_REF环境变量则把github.ref变量赋值给GITHUB_REF
run: |
if [ -z "$GITHUB_REF" ]; then
export GITHUB_REF=${{ github.ref }}
echo $GITHUB_REF
fi
- name: Check version
id: check_version
run: |
echo $GITHUB_REF
# 如果是tag则去掉refs/tags/前缀
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "It's a tag"
echo $GITHUB_REF
echo $GITHUB_REF | awk -F '/' '{print $3}'
echo ::set-output name=version::$(echo $GITHUB_REF | awk -F '/' '{print $3}')
else
echo "It's not a tag"
echo $GITHUB_REF
echo ::set-output name=version::${GITHUB_REF}
fi
build-images:
runs-on: ubuntu-latest
needs: prepare
name: Build ${{ matrix.platform }} image
permissions:
contents: read
strategy:
matrix:
platform: [linux/amd64, linux/arm64]
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false
- name: Set platform tag
id: platform_tag
run: |
# Convert platform to tag suffix (e.g., linux/amd64 -> amd64)
PLATFORM_TAG=$(echo ${{ matrix.platform }} | sed 's/linux\///g')
echo ::set-output name=tag::${PLATFORM_TAG}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Registry
run: docker login --username=${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }}
- name: Build and cache
run: |
docker buildx build \
--platform ${{ matrix.platform }} \
--cache-to type=registry,ref=rockchin/langbot:cache-${{ steps.platform_tag.outputs.tag }},mode=max \
--cache-from type=registry,ref=rockchin/langbot:cache-${{ steps.platform_tag.outputs.tag }} \
-t rockchin/langbot:${{ needs.prepare.outputs.version }} \
.
push-multiarch:
runs-on: ubuntu-latest
needs: [prepare, build-images]
name: Build and push multi-arch images
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Registry
run: docker login --username=${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push for Release
if: ${{ needs.prepare.outputs.is_prerelease == 'false' }}
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=rockchin/langbot:cache-amd64 \
--cache-from type=registry,ref=rockchin/langbot:cache-arm64 \
-t rockchin/langbot:${{ needs.prepare.outputs.version }} \
-t rockchin/langbot:latest \
--push \
.
- name: Build and push for Pre-release
if: ${{ needs.prepare.outputs.is_prerelease == 'true' }}
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=rockchin/langbot:cache-amd64 \
--cache-from type=registry,ref=rockchin/langbot:cache-arm64 \
-t rockchin/langbot:${{ needs.prepare.outputs.version }} \
--push \
.