Two Ways to Write docker.yaml for Building amd and arm Images with GitHub Actions
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
For my records and as a reference
Method 1: Put the build commands in the Dockerfile and build directly
For example, this project: https://github.com/woodchen-ink/sun-panel
name: Docker
on:
push:
branches:
- master
tags:
- v*
env:
IMAGE_NAME: sun-panel
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: woodchen
password: ${{ secrets.ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile
push: true
tags: woodchen/${{ env.IMAGE_NAME }}:latest
platforms: linux/amd64,linux/arm64,linux/arm/v7
Method 2: Run all build steps in the YAML workflow
For example, this project: https://github.com/woodchen-ink/one-api
I used this approach because building with dockerfile kept giving me strange errors, even though the original author's build worked fine, so I had to adapt it.
name: Docker
on:
push:
branches:
- main
tags:
- "v*"
env:
DOCKER_HUB_USERNAME: woodchen
DOCKER_HUB_REPO: czloapi
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: 检出代码仓库
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: 18
- name: 构建前端
env:
CI: ""
run: |
cd web
yarn install
yarn run build
cd ..
- name: 设置Go环境
uses: actions/setup-go@v3
with:
go-version: ">=1.18.0"
- name: 构建后端 (amd64)
run: |
go mod download
go build -ldflags "-s -w -extldflags '-static'" -o one-api-amd64
- name: 构建后端 (arm64)
run: |
sudo apt-get update
sudo apt-get install gcc-aarch64-linux-gnu
CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags '-static'" -o one-api-arm64
- name: 设置QEMU
uses: docker/setup-qemu-action@v2
- name: 设置Docker Buildx
uses: docker/setup-buildx-action@v2
- name: 登录Docker Hub
uses: docker/login-action@v2
with:
registry: docker.io
username: ${{ env.DOCKER_HUB_USERNAME }}
password: ${{ secrets.ACCESS_TOKEN }}
- name: Docker元数据
id: meta
uses: docker/metadata-action@v4
with:
images: |
docker.io/${{ env.DOCKER_HUB_USERNAME }}/${{ env.DOCKER_HUB_REPO }}
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/') }}
- name: 构建并推送
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: Dockerfile-action
Comments 0