# magic-docker 一个 action 搞定 Docker 镜像的构建与推送,支持: - `context`:指定构建目录(前端/后端在子目录时用) - `dockerfile`:指定 Dockerfile 的完整路径 - `preset`:内置方案,对应 action 根目录下的 `vue3.Dockerfile`、`vue3-hash.Dockerfile`、`dotnet10.Dockerfile` 优先级:**`dockerfile` > `preset` > `context` 下的默认 `Dockerfile`**。 设置了 `dockerfile` 时完全忽略 `preset`;设置了 `preset` 时用对应的 Dockerfile 构建,构建完自动 push 到 registry。 ## 内置 preset | preset | 对应 Dockerfile | 说明 | | --- | --- | --- | | `vue3` | `vue3.Dockerfile` | Vue3 + history 路由,nginx 做 `try_files` 回退并带跨域头 | | `vue3-hash` | `vue3-hash.Dockerfile` | Vue3 + hash 路由,nginx 默认配置 | | `dotnet10` | `dotnet10.Dockerfile` | .NET 10 self-contained 发布(linux-x64) | - 编译全部写在 Dockerfile 里(多阶段构建),action 本身只负责传 `--build-arg VERSION=`: - vue:`npm ci` → `npm run build-only`(没有该脚本时回退 `npm run build`)→ `npm pkg set version=` - dotnet10:`dotnet publish -r linux-x64 -c Release --self-contained -p:AssemblyName=App -p:Version=` - `vue3` 的 nginx 配置也内联在 Dockerfile 里:`context` 下有 `nginx.conf` 就用项目的,否则用内置默认配置(history 回退 + 跨域头)。 ## 参数 | 参数 | 必填 | 默认值 | 说明 | | --- | --- | --- | --- | | `registry` | 是 | `${{ vars.DOCKER_REGISTRY }}` | Docker registry 地址 | | `user` | 是 | `${{ vars.DOCKER_USER }}` | registry 用户名 | | `pass` | 是 | `${{ vars.DOCKER_PASS }}` | registry 密码 | | `image` | 是 | `${{ github.repository }}` | 镜像名,不含 registry(最终拼成 `/:`) | | `tag` | 否 | `${{ github.ref_name }}` | 镜像 tag | | `platform` | 否 | `linux/amd64` | 构建平台 | | `context` | 否 | `.` | 构建目录,相对仓库根目录 | | `dockerfile` | 否 | 空 | Dockerfile 完整路径(相对仓库根目录或绝对路径) | | `preset` | 否 | 空 | 内置方案:`vue3` / `vue3-hash` / `dotnet10` | ## 示例 ### 使用 preset ```yaml - uses: actions/magic-docker@main with: context: vue preset: vue3 ``` ```yaml - uses: actions/magic-docker@main with: context: vue preset: vue3-hash ``` ```yaml - uses: actions/magic-docker@main with: context: backend preset: dotnet10 ``` ### 使用自己的 Dockerfile 指定 `dockerfile` 后 `preset` 会被忽略: ```yaml - uses: actions/magic-docker@main with: context: backend dockerfile: backend/Dockerfile platform: linux/arm64 ``` ### 只给 context(默认用 context/Dockerfile) ```yaml - uses: actions/magic-docker@main with: context: . ``` ### 指定镜像名与 tag ```yaml - uses: actions/magic-docker@main with: context: vue preset: vue3 image: simcu/web tag: ${{ github.ref_name }} ```