ffc4152fe6f3fe28cc265f2a6cdb06a12f35d035
magic-docker
一个 action 搞定 Docker 镜像的构建与推送,支持:
context:指定构建目录(前端/后端在子目录时用)dockerfile:指定 Dockerfile 的完整路径preset:内置方案,对应 action 根目录下的vue3.Dockerfile、vue3-hash.Dockerfile、dotnet.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 默认配置 |
dotnet |
dotnet.Dockerfile |
.NET self-contained 发布(linux-x64) |
- 编译全部写在 Dockerfile 里(多阶段构建),action 本身只负责传
--build-arg VERSION=<tag>:- vue:
npm ci→npm run build-only(没有该脚本时回退npm run build)→npm pkg set version=<tag> - dotnet:
dotnet publish -r linux-x64 -c Release --self-contained -p:AssemblyName=App -p:Version=<tag>
- vue:
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 }} |
镜像名 |
tag |
否 | ${{ github.ref_name }} |
镜像 tag |
platform |
否 | linux/amd64 |
构建平台 |
context |
否 | . |
构建目录,相对仓库根目录 |
dockerfile |
否 | 空 | Dockerfile 完整路径(相对仓库根目录或绝对路径) |
preset |
否 | 空 | 内置方案:vue3 / vue3-hash / dotnet |
proxy |
否 | 空 | 构建时使用的 HTTP(S) 代理 |
no_proxy |
否 | 见 action.yml | 不使用代理的地址 |
示例
使用 preset
- uses: actions/magic-docker@main
with:
context: vue
preset: vue3
- uses: actions/magic-docker@main
with:
context: vue
preset: vue3-hash
- uses: actions/magic-docker@main
with:
context: backend
preset: dotnet
使用自己的 Dockerfile
指定 dockerfile 后 preset 会被忽略:
- uses: actions/magic-docker@main
with:
context: backend
dockerfile: backend/Dockerfile
platform: linux/arm64
只给 context(默认用 context/Dockerfile)
- uses: actions/magic-docker@main
with:
context: .
指定镜像名与 tag
- uses: actions/magic-docker@main
with:
context: vue
preset: vue3
image: simcu/web
tag: ${{ github.ref_name }}
Languages
Dockerfile
100%