From 11a05a93c20cd0d15e13e0ecbf107037cb2717df Mon Sep 17 00:00:00 2001 From: sunjuzhong <129019668+sunjuzhong@users.noreply.github.com> Date: Sun, 9 Mar 2025 16:19:26 +0800 Subject: [PATCH] feat: update the yao base image --- .github/workflows/publish-docker.yml | 54 ++++++--------------- .gitignore | 1 + docker/base/Dockerfile | 29 ++++-------- docker/base/build-docker-and-publish.sh | 62 +++++++++++++++++++++++++ get-yao.sh | 20 ++++++-- 5 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 docker/base/build-docker-and-publish.sh diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml index 509506f6..436f01cf 100644 --- a/.github/workflows/publish-docker.yml +++ b/.github/workflows/publish-docker.yml @@ -2,58 +2,32 @@ name: Publish Docker Image on: workflow_dispatch: - inputs: - run_id: - description: "Build Linux Artifacts Run ID" - required: false - type: string workflow_run: workflows: ["Build Linux Artifacts"] types: - completed -env: - DOCKER_IMAGE: yaoapp/yao - PLATFORMS: linux/amd64,linux/arm64 - jobs: publish: - if: ${{ github.event.workflow_run.conclusion == 'success' }} + if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest steps: - - name: Download artifacts - uses: actions/download-artifact@v4 - with: - name: yao-linux - path: dist/release - run-id: ${{ github.event.workflow_run.id || inputs.run_id }} - - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - + - 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 + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Get Version + - name: Run build script run: | - VERSION=$(ls dist/release/yao-*-linux-amd64 | sed 's/.*yao-\(.*\)-unstable-linux-amd64/\1/') - echo "VERSION=$VERSION" >> $GITHUB_ENV - - - name: Build and push - uses: docker/build-push-action@v5 - with: - context: . - file: docker/base/Dockerfile - platforms: ${{ env.PLATFORMS }} - push: true - tags: | - ${{ env.DOCKER_IMAGE }}:latest - ${{ env.DOCKER_IMAGE }}:${{ env.VERSION }} - build-args: | - VERSION=${{ env.VERSION }} \ No newline at end of file + chmod +x ./docker/base/build-docker-and-publish.sh + ./docker/base/build-docker-and-publish.sh + env: + DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 5c6d5461..88dc0939 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ docker/build/test db *.sh !get-yao.sh +!docker/base/** __debug* \ No newline at end of file diff --git a/docker/base/Dockerfile b/docker/base/Dockerfile index a84154c5..f9056a46 100644 --- a/docker/base/Dockerfile +++ b/docker/base/Dockerfile @@ -1,26 +1,17 @@ # =========================================== # Yao Base Image # A minimal base image for running Yao applications -# -# Build: -# docker build --platform linux/amd64 --build-arg VERSION=0.10.4 --build-arg ARCH=amd64 -t yaoapp/yao-base:0.10.4-amd64 . -# docker build --platform linux/arm64 --build-arg VERSION=0.10.4 --build-arg ARCH=arm64 -t yaoapp/yao-base:0.10.4-arm64 . -# -# Tests: -# docker run --rm yaoapp/yao-base:0.10.4-amd64 yao version -# # =========================================== -FROM alpine:latest -ARG VERSION -ARG ARCH +FROM debian:bullseye-slim -# Install minimal dependencies -RUN apk --no-cache add curl +# Install necessary tools and clean up in one layer +RUN apt-get update && apt-get install -y \ + curl \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* -# Copy and install Yao binary from build artifacts -COPY dist/yao-${VERSION}-linux-${ARCH} /usr/local/bin/yao -RUN chmod +x /usr/local/bin/yao && \ - addgroup -S yao && adduser -S -G yao yao +# Download and install Yao using the original script +RUN curl -fsSL https://raw.githubusercontent.com/sjzsdu/yao/refs/heads/main/get-yao.sh | bash -# Switch to non-root user -USER yao \ No newline at end of file +# Set working directory +WORKDIR /app diff --git a/docker/base/build-docker-and-publish.sh b/docker/base/build-docker-and-publish.sh new file mode 100644 index 00000000..aaf1badc --- /dev/null +++ b/docker/base/build-docker-and-publish.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# 获取脚本所在目录的绝对路径 +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +# 获取项目根目录路径 +PROJECT_ROOT="$( cd "$SCRIPT_DIR/../.." &> /dev/null && pwd )" + +# 设置变量 +IMAGE_NAME="sjzsdu/yao" +# 从 /share/const.go 文件中获取版本号 +VERSION=$(sed -n 's/^const VERSION = "\(.*\)"/\1/p' "${PROJECT_ROOT}/share/const.go") +IMAGE_TAG="${VERSION}" + +# 定义检查状态的函数 +check_status() { + if [ $? -ne 0 ]; then + echo "错误: $1" + exit 1 + fi +} +# 检查版本号是否为空 +if [ -z "$VERSION" ]; then + echo "错误: 无法从 ${PROJECT_ROOT}/share/const.go 文件中提取版本号" + exit 1 +fi + +echo "使用版本号: ${VERSION}" + +# 构建 Docker 镜像 +echo "正在构建 Docker 镜像..." +docker build -t ${IMAGE_NAME}:${IMAGE_TAG} -f "${SCRIPT_DIR}/Dockerfile" "${SCRIPT_DIR}" +check_status "Docker 镜像构建失败" + +# 检查镜像是否成功创建 +echo "检查镜像是否创建成功..." +docker image inspect ${IMAGE_NAME}:${IMAGE_TAG} > /dev/null 2>&1 +check_status "无法找到刚刚构建的镜像" + +# Docker Hub 登录 +echo "正在登录到 Docker Hub..." +if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then + # GitHub Actions 环境 + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin +else + # 本地环境 + docker login +fi +check_status "Docker Hub 登录失败" + +# 推送镜像到 Docker Hub +echo "正在推送镜像到 Docker Hub..." +docker push ${IMAGE_NAME}:${IMAGE_TAG} +check_status "推送镜像到 Docker Hub 失败" + +# 标记并推送 latest 标签 +echo "正在标记并推送 latest 标签..." +docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest +docker push ${IMAGE_NAME}:latest +check_status "推送 latest 标签失败" + +echo "操作完成" diff --git a/get-yao.sh b/get-yao.sh index dcf9891f..97712285 100644 --- a/get-yao.sh +++ b/get-yao.sh @@ -37,11 +37,17 @@ DOWNLOAD_URL="https://github.com/$REPO/releases/download/v$VERSION/yao-$VERSION- # 下载文件 echo "正在下载 yao-$OS-$ARCH 版本 $VERSION ..." -curl -L $DOWNLOAD_URL -o yao -echo "下载 URL: $DOWNLOAD_URL" +TEMP_FILE="yao_temp_$RANDOM" +if curl -L $DOWNLOAD_URL -o "$TEMP_FILE"; then + echo "下载完成。" +else + echo "下载失败,请检查网络连接或下载 URL。" + echo "下载 URL: $DOWNLOAD_URL" + exit 1 +fi # 使文件可执行 -chmod +x yao +chmod +x "$TEMP_FILE" # 确定安装目录 INSTALL_DIR="/usr/local/bin" @@ -52,7 +58,13 @@ fi # 移动文件到安装目录 echo "正在安装 yao 到 $INSTALL_DIR ..." -mv yao "$INSTALL_DIR/yao" +if mv "$TEMP_FILE" "$INSTALL_DIR/yao"; then + echo "yao 安装完成!" +else + echo "安装失败,无法移动文件到 $INSTALL_DIR。" + echo "请检查目录权限或手动移动文件。" + exit 1 +fi # 检查安装目录是否在 PATH 中 if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then