Skip to content

Commit 80cdfa1

Browse files
committed
feat: 自动化推送 Dockerhub
1 parent 92c3eb7 commit 80cdfa1

File tree

4 files changed

+106
-15
lines changed

4 files changed

+106
-15
lines changed

.github/workflows/build.yml

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,58 @@ jobs:
1919
strategy:
2020
matrix:
2121
os: [ "ubuntu-latest", "windows-latest" ]
22+
python-version: [ "3.12" ] # 指定 Python 版本
2223
runs-on: ${{ matrix.os }}
2324
steps:
2425
- name: Checkout code
25-
uses: actions/checkout@v3
26+
uses: actions/checkout@v4 # 推荐使用最新版本
2627

27-
- name: Set up environment for Linux (Ubuntu)
28-
if: matrix.os == 'ubuntu-latest'
28+
- name: Set up Python ${{ matrix.python-version }}
29+
uses: actions/setup-python@v5 # 使用 setup-python action
30+
with:
31+
python-version: ${{ matrix.python-version }}
32+
# cache: 'pip' # 如果有 requirements.txt 可以开启 pip 缓存
33+
34+
- name: Create and activate virtual environment
35+
shell: bash # Linux和Windows通用Shell,方便执行venv命令
2936
run: |
30-
sudo apt update
31-
sudo apt install -y python3-venv build-essential
32-
echo "Environment setup complete."
37+
# 创建虚拟环境
38+
python -m venv .venv
39+
# 激活虚拟环境 (对于当前runner的Shell有效)
40+
# 对于bash/zsh
41+
if [[ "${{ runner.os }}" == "Linux" ]]; then
42+
echo "source .venv/bin/activate" >> "$GITHUB_ENV"
43+
# 对于PowerShell
44+
elif [[ "${{ runner.os }}" == "Windows" ]]; then
45+
echo ".venv/Scripts/Activate.ps1" >> "$GITHUB_ENV"
46+
fi
47+
# 更新 PATH 确保后续步骤使用激活的 venv
48+
echo "PATH=$PWD/.venv/bin:$PATH" >> "$GITHUB_ENV" # Linux
49+
echo "PATH=$PWD/.venv/Scripts;$PATH" >> "$GITHUB_ENV" # Windows
3350
34-
- name: Set up environment for Windows
35-
if: matrix.os == 'windows-latest'
51+
- name: Install dependencies from requirements.txt
52+
# 确保在激活的虚拟环境中安装依赖
3653
run: |
37-
choco install python312 --yes
38-
python -m ensurepip
39-
echo "Environment setup complete."
54+
pip install --upgrade pip
55+
pip install -r requirements.txt # 假设你的依赖在 requirements.txt 中
4056
4157
- name: Install dependencies and build for Linux
4258
if: matrix.os == 'ubuntu-latest'
4359
shell: bash
4460
run: |
45-
chmod +x build.sh # 确保脚本可执行
46-
./build.sh # 运行Bash脚本
61+
chmod +x build.sh
62+
./build.sh
4763
4864
- name: Install dependencies and build for Windows
4965
if: matrix.os == 'windows-latest'
5066
shell: pwsh
5167
run: |
52-
.\build.ps1 # 运行PowerShell脚本
68+
.\build.ps1
5369
5470
- name: Upload build output as artifact
5571
uses: actions/upload-artifact@v4
5672
with:
5773
name: app-${{ matrix.os }}
5874
path: |
5975
build/app.dist/**
60-
if-no-files-found: error
76+
if-no-files-found: error

.github/workflows/image.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Docker Image CI/CD
2+
3+
# 当推送到 main 分支时自动触发
4+
# 也可以手动触发,允许输入自定义 tagname
5+
on:
6+
push:
7+
branches: [master]
8+
workflow_dispatch:
9+
inputs:
10+
tagname:
11+
description: "Custom Docker image tag (e.g., v1.0.0). If empty, uses commit SHA."
12+
required: false
13+
type: string
14+
15+
jobs:
16+
build_and_publish:
17+
runs-on: ubuntu-latest
18+
permissions:
19+
contents: read # 允许读取仓库内容
20+
packages: write # 如果推送到 GitHub Packages Registry (ghcr.io) 则需要此权限,Docker Hub 不需要但保留无害
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Login to Docker Hub
30+
uses: docker/login-action@v3
31+
with:
32+
username: ${{ secrets.DOCKER_USERNAME }}
33+
password: ${{ secrets.DOCKER_PASSWORD }} # 使用 GitHub Secrets 存储 Docker Hub 密码或访问令牌
34+
35+
- name: Set up image tags
36+
id: set_tags
37+
run: |
38+
IMAGE_NAME="gamernotitle/welearn-brain-burst"
39+
CUSTOM_TAG="${{ github.event.inputs.tagname }}" # 从 workflow_dispatch 获取自定义标签
40+
COMMIT_SHA_SHORT=$(echo "${{ github.sha }}" | cut -c1-7) # 获取短 SHA
41+
42+
# 默认标签总是包含短 SHA
43+
ALL_TAGS="$IMAGE_NAME:$COMMIT_SHA_SHORT"
44+
45+
# 如果提供了自定义标签,则添加自定义标签
46+
if [ -n "$CUSTOM_TAG" ]; then
47+
ALL_TAGS="$ALL_TAGS,$IMAGE_NAME:$CUSTOM_TAG"
48+
echo "Using custom tag: $CUSTOM_TAG"
49+
else
50+
echo "No custom tag provided, using commit SHA: $COMMIT_SHA_SHORT"
51+
# 如果是推送到 main 分支且没有自定义标签,则添加 'latest' 标签
52+
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
53+
ALL_TAGS="$ALL_TAGS,$IMAGE_NAME:latest"
54+
echo "Adding 'latest' tag for main branch push."
55+
fi
56+
fi
57+
58+
echo "Generated tags: $ALL_TAGS"
59+
# 将生成的标签字符串设置为步骤输出,供后续步骤使用
60+
echo "tags=$ALL_TAGS" >> "$GITHUB_OUTPUT"
61+
62+
- name: Build and push Docker image
63+
uses: docker/build-push-action@v5
64+
with:
65+
context: .
66+
push: true
67+
tags: ${{ steps.set_tags.outputs.tags }}

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "welearnbrainburst"
3+
version = "1.4.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = []

0 commit comments

Comments
 (0)