feat: update the yao base image

This commit is contained in:
sunjuzhong 2025-03-09 16:19:26 +08:00
parent df3dd3fb34
commit 11a05a93c2
5 changed files with 103 additions and 63 deletions

View file

@ -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 }}
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 }}

1
.gitignore vendored
View file

@ -43,4 +43,5 @@ docker/build/test
db
*.sh
!get-yao.sh
!docker/base/**
__debug*

View file

@ -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
# Set working directory
WORKDIR /app

View file

@ -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 "操作完成"

View file

@ -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