侧边栏壁纸
博主头像
Wood Chen博主等级

独唱独酬,独行独坐还独卧

  • 累计撰写 209 篇文章
  • 累计创建 127 个标签
  • 累计收到 6 条评论

目 录CONTENT

文章目录

Github action构建amd和arm镜像的两种docker.yaml写法

wood
2024-04-06 / 0 评论 / 0 点赞 / 13 阅读 / 3819 字

用作记录, 并作参考

第一种, 构建命令在dockerfile里, 直接构建就行

例如这个项目: 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

第二种, 全部在yaml里构建

例如这个项目: https://github.com/woodchen-ink/one-api

使用这个的原因是我用dockerfile​构建时, 总是奇怪的错误, 但是原作者的没问题, 就只能改改用了

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

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区