Fastgpt学习(4)- 使用Github Action构建Docker镜像

1. 背景

本地开发修改代码后需要构建适用于不用系统架构的Docker镜像,在本地MacOS (m1) 上一直报错,无法完成镜像构建,遂转使用Github Action构建镜像。

报错信息:failed to compute cache key: "/app/projects/app/.next/standalone" not found: not found
尝试解决方案:

1.本地重新运行pnpm build

  1. 修改DockerFile 文件中的 ENV NODE_OPTIONS="--max-old-space-size=4096"的值
    参考issue:

  2. https://github.com/labring/FastGPT/issues/1457

  3. https://github.com/labring/FastGPT/issues/1468

Mac打包可能存在的问题:

2. 操作步骤

2.1 fork仓库 & 配置仓库信息

FastGPT- github仓库

Github Action工作流的相关文件存储在
.github -> workflows 文件夹下,构建docker镜像的有两个文件build-sandbox-image.ymlfastgpt-image.yml

配置仓库信息

通过查看上述两个文件,发现有几个变量需要根据自身情况进行配置

以具体的dockerhub仓库为例

GH_PAT是个人访问令牌:

登录到你的 GitHub 账户。转到 Settings (设置) > Developer settings (开发者设置) > Personal access tokens (个人访问令牌)。如果你还没有令牌,可以点击 Generate new token (生成新令牌)。如果已有令牌,点击它以编辑权限。

2.2 修改配置文件

fastgpt-image.ymlbuild-sandbox-images.yml文件修改:删除所有Ali Hub相关的代码

yml 复制代码
#   fastgpt-image.yml文件
name: Build FastGPT images and copy image to docker hub
on:
  workflow_dispatch:
  push:
    paths:
      - 'projects/app/**'
      - 'packages/**'
    tags:
      - 'v*'
jobs:
  build-fastgpt-images:
    runs-on: ubuntu-20.04
    steps:
      # install env
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1
      - name: Install Dependencies
        run: |
          sudo apt update && sudo apt install -y nodejs npm
      - name: Set up QEMU (optional)
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          driver-opts: network=host
      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      # login docker
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GH_PAT }}
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_NAME }}
          password: ${{ secrets.DOCKER_HUB_PASSWORD }}

      # Set tag
      - name: Set image name and tag
        run: |
          if [[ "${{ github.ref_name }}" == "main" ]]; then
            echo "Git_Tag=ghcr.io/${{ github.repository_owner }}/fastgpt:latest" >> $GITHUB_ENV
            echo "Git_Latest=ghcr.io/${{ github.repository_owner }}/fastgpt:latest" >> $GITHUB_ENV
            echo "Docker_Hub_Tag=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt:latest" >> $GITHUB_ENV
            echo "Docker_Hub_Latest=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt:latest" >> $GITHUB_ENV
          else
            echo "Git_Tag=ghcr.io/${{ github.repository_owner }}/fastgpt:${{ github.ref_name }}" >> $GITHUB_ENV
            echo "Git_Latest=ghcr.io/${{ github.repository_owner }}/fastgpt:latest" >> $GITHUB_ENV
            echo "Docker_Hub_Tag=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt:${{ github.ref_name }}" >> $GITHUB_ENV
            echo "Docker_Hub_Latest=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt:latest" >> $GITHUB_ENV
          fi

      - name: Build and publish image for main branch or tag push event
        env:
          DOCKER_REPO_TAGGED: ${{ env.DOCKER_REPO_TAGGED }}
        run: |
          docker buildx build \
          -f projects/app/Dockerfile \
          --platform linux/amd64,linux/arm64 \
          --label "org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/FastGPT" \
          --label "org.opencontainers.image.description=fastgpt image" \
          --push \
          --cache-from=type=local,src=/tmp/.buildx-cache \
          --cache-to=type=local,dest=/tmp/.buildx-cache \
          -t ${Git_Tag} \
          -t ${Git_Latest} \
          -t ${Docker_Hub_Tag} \
          -t ${Docker_Hub_Latest} \
          .
yml 复制代码
#  build-sandbox-images.yml文件
name: Build fastgpt-sandbox images and copy image to docker hub
on:
workflow_dispatch:
push:
 paths:
   - 'projects/sandbox/**'
 tags:
   - 'v*'
jobs:
build-fastgpt-sandbox-images:
 runs-on: ubuntu-20.04
 steps:
   # install env
   - name: Checkout
     uses: actions/checkout@v3
     with:
       fetch-depth: 0
   - name: Install Dependencies
     run: |
       sudo apt update && sudo apt install -y nodejs npm
   - name: Set up QEMU (optional)
     uses: docker/setup-qemu-action@v2
   - name: Set up Docker Buildx
     uses: docker/setup-buildx-action@v2
     with:
       driver-opts: network=host
   - name: Cache Docker layers
     uses: actions/cache@v2
     with:
       path: /tmp/.buildx-cache
       key: ${{ runner.os }}-buildx-${{ github.sha }}
       restore-keys: |
         ${{ runner.os }}-buildx-

   # login docker
   - name: Login to GitHub Container Registry
     uses: docker/login-action@v2
     with:
       registry: ghcr.io
       username: ${{ github.repository_owner }}
       password: ${{ secrets.GH_PAT }}
   - name: Login to Docker Hub
     uses: docker/login-action@v2
     with:
       username: ${{ secrets.DOCKER_HUB_NAME }}
       password: ${{ secrets.DOCKER_HUB_PASSWORD }}

   # Set tag
   - name: Set image name and tag
     run: |
       if [[ "${{ github.ref_name }}" == "main" ]]; then
         echo "Git_Tag=ghcr.io/${{ github.repository_owner }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
         echo "Git_Latest=ghcr.io/${{ github.repository_owner }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
         echo "Docker_Hub_Tag=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
         echo "Docker_Hub_Latest=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
       else
         echo "Git_Tag=ghcr.io/${{ github.repository_owner }}/fastgpt-sandbox:${{ github.ref_name }}" >> $GITHUB_ENV
         echo "Git_Latest=ghcr.io/${{ github.repository_owner }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
         echo "Docker_Hub_Tag=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-sandbox:${{ github.ref_name }}" >> $GITHUB_ENV
         echo "Docker_Hub_Latest=${{ secrets.DOCKER_IMAGE_NAME }}/fastgpt-sandbox:latest" >> $GITHUB_ENV
       fi

   - name: Build and publish image for main branch or tag push event
     env:
       Git_Tag: ${{ env.Git_Tag }}
       Git_Latest: ${{ env.Git_Latest }}
       Docker_Hub_Tag: ${{ env.Docker_Hub_Tag }}
       Docker_Hub_Latest: ${{ env.Docker_Hub_Latest }}
     run: |
       docker buildx build \
       -f projects/sandbox/Dockerfile \
       --platform linux/amd64,linux/arm64 \
       --label "org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/fastgpt-sandbox" \
       --label "org.opencontainers.image.description=fastgpt-sandbox image" \
       --push \
       --cache-from=type=local,src=/tmp/.buildx-cache \
       --cache-to=type=local,dest=/tmp/.buildx-cache \
       -t ${Git_Tag} \
       -t ${Git_Latest} \
       -t ${Docker_Hub_Tag} \
       -t ${Docker_Hub_Latest} \
       .

2.3 删除无关配置文件(可选)

因为fork仓库只是用来构建镜像并双上传至dockerhub仓库,所以可以精简相关github action的文件。可只保留下面3个文件。

2.4 推送代码&线上构建镜像

将代码打标签并提交至github仓库,示例如下:

git 复制代码
git tag v1.0.0
git push origin v1.0.0

到github仓库中手动构建镜像:

完成后即可到dockerhub中查看镜像。

因为删除了自动化处理的配置文件,所以此处只能去手动处理。

相关推荐
practical_sharp9 分钟前
如何初始化一个线上的GitHub仓库,在本地已有的仓库中上传到线上
github
长潇若雪9 分钟前
结构体(C 语言)
c语言·开发语言·经验分享·1024程序员节
DARLING Zero two♡44 分钟前
关于我、重生到500年前凭借C语言改变世界科技vlog.12——深入理解指针(2)
c语言·开发语言·科技·1024程序员节
独行soc1 小时前
#渗透测试#SRC漏洞挖掘# 信息收集-Shodan进阶之Jenkins组件
安全·jenkins·安全威胁分析·1024程序员节·shodan
梓羽玩Python1 小时前
推荐一款基于快手Kolors模型开发的虚拟试衣浏览器插件!妥妥的安排上!
人工智能·python·github
梓羽玩Python1 小时前
推荐6款效果逼真的AI 3D建模工具!
开源·github·产品
dawn1912282 小时前
Java 中的正则表达式详解
java·开发语言·算法·正则表达式·1024程序员节
CloudJourney2 小时前
初始Docker
运维·docker·容器
黑不拉几的小白兔2 小时前
PTA L1系列题解(C语言)(L1_097 -- L1_104)
数据结构·算法·1024程序员节
小言从不摸鱼2 小时前
【Python】元组、字典与集合详解:数据容器的实战应用
人工智能·python·1024程序员节