diff --git a/action.yml b/action.yml index 0cdf862..d5d8c04 100644 --- a/action.yml +++ b/action.yml @@ -2,39 +2,25 @@ name: "build docker image and push to code" description: "build docker image and push to simcu code" inputs: - dockerfile: - description: "if set, will use this to build" + context: + description: "构建目录(相对仓库根目录,默认 .)。例如后端在 dotnet 目录下时填 dotnet" required: false - default: "" + default: "." runs: using: "composite" steps: - name: build .net app uses: docker://mcr.microsoft.com/dotnet/sdk:10.0 - with: - args: dotnet publish -r linux-x64 -c Release --self-contained -o dist -p:AssemblyName=App -p:Version=${{ github.ref_name }} - - - name: create dockerfile - if: ${{ inputs.dockerfile == '' }} - uses: docker://ubuntu:latest with: entrypoint: "/bin/bash" args: | - -c 'echo "FROM mcr.microsoft.com/dotnet/runtime:10.0" > Dockerfile && \ - echo "ENV TZ Asia/Shanghai" >> Dockerfile && \ - echo "COPY dist /home" >> Dockerfile && \ - echo "WORKDIR /home" >> Dockerfile && \ - echo "ENTRYPOINT [\"./App\"]" >> Dockerfile && \ - echo "已经构建完DOCKERFILE了" && \ - cat Dockerfile' + -c 'cd "${{ inputs.context }}" && \ + dotnet publish -r linux-x64 -c Release --self-contained -o dist -p:AssemblyName=App -p:Version=${{ github.ref_name }}' - - name: build docker image use custom dockerfile - uses: docker://docker:git + # -f 直接指向本 action 内置的 dotnet.Dockerfile,不用再准备/拷贝文件; + # 构建上下文仍是 context,所以 Dockerfile 里的 COPY dist 取的是构建目录下的发布产物 + - name: build docker image and push + uses: actions/docker-build-push@main with: - entrypoint: "/bin/sh" - args: | - -c 'cat ${{ inputs.dockerfile || 'Dockerfile' }} && docker build -t ${{vars.DOCKER_REGISTRY}}/${{github.repository}}:${{ github.ref_name }} -f ${{ inputs.dockerfile || 'Dockerfile' }} . && \ - echo ${{vars.DOCKER_REGISTRY}}/${{github.repository}}:${{ github.ref_name }}已经构建完了 && \ - docker login ${{vars.DOCKER_REGISTRY}} -u ${{vars.DOCKER_USER}} -p ${{vars.DOCKER_PASS}} && \ - docker push ${{vars.DOCKER_REGISTRY}}/${{github.repository}}:${{ github.ref_name }} && \ - echo ${{vars.DOCKER_REGISTRY}}/${{github.repository}}:${{ github.ref_name }}推送成功' + context: ${{ inputs.context }} + dockerfile: ${{ github.action_path }}/dotnet.Dockerfile diff --git a/dotnet.Dockerfile b/dotnet.Dockerfile new file mode 100644 index 0000000..d275448 --- /dev/null +++ b/dotnet.Dockerfile @@ -0,0 +1,5 @@ +FROM mcr.microsoft.com/dotnet/runtime:10.0 +ENV TZ Asia/Shanghai +COPY dist /home +WORKDIR /home +ENTRYPOINT ["./App"]