From 9648aadbf987413d23fa5416024d037ea1382ec2 Mon Sep 17 00:00:00 2001 From: JustSong Date: Tue, 6 Dec 2022 15:16:48 +0800 Subject: [PATCH] Use GitHub Actions to build docker images --- .github/workflows/docker-image.yml | 50 ++++++++++++++++++++++++++++++ Dockerfile | 7 +++-- README.md | 22 +++++++++++++ common/constants.go | 2 +- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..73b7972 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,50 @@ +name: Publish Docker image + +on: + push: + tags: + - '*' + workflow_dispatch: + inputs: + name: + description: 'reason' + required: false +jobs: + push_to_registries: + name: Push Docker image to multiple registries + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Log in to the Container registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: | + justsong/wechat-server + ghcr.io/${{ github.repository }} + + - name: Build and push Docker images + uses: docker/build-push-action@v3 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c08a53f..9415116 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,20 +6,21 @@ RUN npm install RUN npm run build FROM golang AS builder2 + ENV GO111MODULE=on \ CGO_ENABLED=1 \ GOOS=linux \ GOARCH=amd64 - WORKDIR /build COPY . . COPY --from=builder /build/build ./web/build RUN go mod download -RUN go build -ldflags "-s -w" -o wechat-server +RUN go build -ldflags "-s -w -extldflags '-static'" -o wechat-server -FROM scratch +FROM alpine ENV PORT=3000 COPY --from=builder2 /build/wechat-server / EXPOSE 3000 +WORKDIR /data ENTRYPOINT ["/wechat-server"] diff --git a/README.md b/README.md index 9bbccb4..424c83f 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,28 @@ ![demo1](https://user-images.githubusercontent.com/39998050/200124147-3338a2eb-8193-4068-ae6f-276cfe16a708.png) ![demo2](https://user-images.githubusercontent.com/39998050/200124177-78636b4c-0aac-4860-a138-68f3d92477b9.png) +## 部署 +### 手动部署 +1. 从 [GitHub Releases](https://github.com/songquanpeng/wechat-server/releases/latest) 下载可执行文件或者从源码编译: + ```shell + git clone https://github.com/songquanpeng/wechat-server.git + go mod download + go build -ldflags "-s -w" -o wechat-server + ```` +2. 运行: + ```shell + chmod u+x wechat-server + ./wechat-server --port 3000 --log-dir ./logs + ``` +3. 访问 [http://localhost:3000/](http://localhost:3000/) 并登录。初始账号用户名为 `root`,密码为 `123456`。 + +更加详细的部署教程[参见此处](https://iamazing.cn/page/how-to-deploy-a-website)。 + +### 基于 Docker 进行部署 +执行:`docker run -d --restart always -p 3000:3000 -v /home/ubuntu/data/wechat-server:/data justsong/wechat-server` + +数据将会保存在宿主机的 `/home/ubuntu/data/wechat-server` 目录。 + ## 配置 1. 从 [GitHub Releases](https://github.com/songquanpeng/wechat-server/releases/latest) 下载可执行文件。 2. 系统本身开箱即用,有一些环境变量可供配置: diff --git a/common/constants.go b/common/constants.go index 2f8f1c8..8c8989d 100644 --- a/common/constants.go +++ b/common/constants.go @@ -30,7 +30,7 @@ var WeChatMenu = `{ }` var SessionSecret = uuid.New().String() -var SQLitePath = ".wechat-server.db" +var SQLitePath = "wechat-server.db" var OptionMap map[string]string var OptionMapRWMutex sync.RWMutex