From 8e23ec740b0c59accf299fb560753a288df37e65 Mon Sep 17 00:00:00 2001 From: luochao <1055120207@qq.com> Date: Wed, 1 May 2024 00:31:49 +0800 Subject: [PATCH] feat: add build docker image --- .github/workflows/docker-image-ci.yml | 38 +++++++++++++++++++++++++++ Dockerfile | 17 ++++++++++++ Makefile | 16 +++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .github/workflows/docker-image-ci.yml create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/.github/workflows/docker-image-ci.yml b/.github/workflows/docker-image-ci.yml new file mode 100644 index 0000000..647564d --- /dev/null +++ b/.github/workflows/docker-image-ci.yml @@ -0,0 +1,38 @@ +name: Docker Image CI + +on: + push: + tags: + - v* + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: get version + id: vars + run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\/v/} + + - uses: actions/checkout@v4 + + - name: set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: set up docker buildx + uses: docker/setup-buildx-action@v3 + + - name: login ghrc hub + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: build and push + uses: docker/build-push-action@v5 + with: + push: true + platforms: linux/amd64,linux/arm64 + tags: | + ghcr.io/${{ github.repository_owner }}/douyin:${{ steps.vars.outputs.version }} + ghcr.io/${{ github.repository_owner }}/douyin:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b72e24a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# syntax = docker/dockerfile:experimental +FROM --platform=${BUILDPLATFORM:-linux/amd64,linux/arm64} node:20-buster AS builder + +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable + +WORKDIR /src +COPY ./ ./ + +# RUN两次方便观察install和build, 也可以用pnpm cache and locked +RUN pnpm install +RUN npm run build + +FROM --platform=${BUILDPLATFORM:-linux/amd64,linux/arm64} ghcr.io/rookie-luochao/nginx-runner:latest + +COPY --from=builder /src/dist /app \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..83afcb8 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +PKG = $(shell cat package.json | grep 'name' | sed -e 's/ "name": "//g' -e 's/",//g') +VERSION = $(shell cat package.json | grep 'version' | sed -e 's/ "version": "//g' -e 's/",//g') + +# 本地测试构建docker镜像建议删掉node_modules,node_modules存在有时会导致报错 +docker-build: + docker build . -t $(PKG):$(VERSION) + +# 请先创建自己的 buildx driver 实例,请看:https://juejin.cn/post/7296763284647542838 +# 显式指定可执行 docker buildx build --platform linux/amd64,linux/arm64 . -t $(PKG):$(VERSION) --push +docker-buildx-build: + docker buildx build --platform linux/amd64 . -t $(PKG):$(VERSION) --load + +docker-run: + docker run -d -p 80:80 $(PKG):$(VERSION) + +docker-build-run: docker-build docker-run \ No newline at end of file