name: Build and Push Docker Image # 当代码推送到 main 分支时触发 on: push: branches: [ main ] jobs: build-and-push: runs-on: ubuntu-latest steps: # 步骤 1: 检出你的代码仓库 - name: Checkout code uses: actions/checkout@v4 # 步骤 2: 设置 Docker Buildx (推荐,支持多平台构建等高级功能) - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 # 步骤 3: 登录到你的 Docker 仓库 # 你需要在 Gitea/GitHub 的 Secrets 中配置好 DOCKERHUB_USERNAME 和 DOCKERHUB_TOKEN - name: Login to Docker Hub uses: docker/login-action@v3 with: registry: registry.bmycode.help username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} # 步骤 4: 构建并推送 Docker 镜像 (核心步骤) - name: Build and push uses: docker/build-push-action@v5 with: # context: . 表示 Docker 构建上下文为当前仓库的根目录 # 这意味着 Dockerfile 中 COPY . . 会复制整个项目代码 context: . # push: true 表示构建成功后将镜像推送到仓库 push: true # tags: 定义镜像的名称和标签 # 推荐使用 commit sha 作为标签,便于版本追溯 tags: | registry.bmycode.help/app-product:latest registry.bmycode.help/app-product:${{ github.sha }}