From 53027c73ff964914d7510c2edef68d0420589ee6 Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Sat, 18 Mar 2023 11:10:35 +0800 Subject: [PATCH 1/4] fix: upload file path --- service/impl/attachment.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/service/impl/attachment.go b/service/impl/attachment.go index 8faf863..a1dd628 100644 --- a/service/impl/attachment.go +++ b/service/impl/attachment.go @@ -158,7 +158,7 @@ func (a *attachmentServiceImpl) Upload(ctx context.Context, fileHeader *multipar Path: strings.ReplaceAll(attachmentDTO.Path, string(os.PathSeparator), "/"), Size: attachmentDTO.Size, Suffix: attachmentDTO.Suffix, - ThumbPath: attachmentDTO.ThumbPath, + ThumbPath: strings.ReplaceAll(attachmentDTO.Path, string(os.PathSeparator), "/"), Type: attachmentDTO.AttachmentType, Width: attachmentDTO.Width, } @@ -168,6 +168,14 @@ func (a *attachmentServiceImpl) Upload(ctx context.Context, fileHeader *multipar return nil, WrapDBErr(err) } attachmentDTO.ID = attachmentEntity.ID + attachmentDTO.Path, err = fileStorage.GetFilePath(ctx, attachmentEntity.Path) + if err != nil { + return nil, err + } + attachmentDTO.ThumbPath, err = fileStorage.GetFilePath(ctx, attachmentEntity.ThumbPath) + if err != nil { + return nil, err + } return attachmentDTO, nil } From 0ec15edcd92613ae76e21f039771c2ad80b975c0 Mon Sep 17 00:00:00 2001 From: 1379 <1379@1379.com> Date: Sat, 18 Mar 2023 16:29:31 +0800 Subject: [PATCH 2/4] feat: assign a version number at compile time --- .github/workflows/release.yml | 3 ++- consts/consts.go | 12 +++++++----- event/listener/start.go | 2 -- service/impl/admin.go | 3 +-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e4818b1..0205082 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,6 +54,7 @@ jobs: - name: Show workflow information run: | echo "GOOS: $GOOS, GOARCH: $GOARCH" + echo "BUILD_TIME=$(date --rfc-3339=seconds)" >> $GITHUB_ENV - name: Build uses: crazy-max/ghaction-xgo@v2 @@ -66,7 +67,7 @@ jobs: v: true x: false race: false - ldflags: -s -w + ldflags: -s -w -X 'github.com/go-sonic/sonic/consts.SonicVersion=${{github.ref_name}}' -X 'github.com/go-sonic/sonic/consts.BuildCommit=${{github.sha}}' -X 'github.com/go-sonic/sonic/consts.BuildTime=${{env.BUILD_TIME}}' buildmode: default trimpath: true diff --git a/consts/consts.go b/consts/consts.go index 617433c..58fd491 100644 --- a/consts/consts.go +++ b/consts/consts.go @@ -19,7 +19,6 @@ const ( ) const ( - SonicVersion = "1.0.0" SonicBackupPrefix = "sonic-backup-" SonicDataExportPrefix = "sonic-data-export-" SonicBackupMarkdownPrefix = "sonic-backup-markdown-" @@ -40,7 +39,10 @@ const ( ThemeCustomPostPrefix = "post_" ) -// StartTime 系统启动时间 -var StartTime time.Time - -var DatabaseVersion string +var ( + StartTime time.Time = time.Now() + DatabaseVersion string + SonicVersion = "v1.0.0" + BuildTime string + BuildCommit string +) diff --git a/event/listener/start.go b/event/listener/start.go index f4e1d24..3cf1acf 100644 --- a/event/listener/start.go +++ b/event/listener/start.go @@ -3,7 +3,6 @@ package listener import ( "context" "fmt" - "time" "go.uber.org/zap" "gorm.io/gorm" @@ -37,7 +36,6 @@ func (s *StartListener) HandleEvent(ctx context.Context, startEvent event.Event) if _, ok := startEvent.(*event.StartEvent); !ok { return nil } - consts.StartTime = time.Now() err := s.createOptions() if err != nil { diff --git a/service/impl/admin.go b/service/impl/admin.go index 24a52be..20cf6b2 100644 --- a/service/impl/admin.go +++ b/service/impl/admin.go @@ -8,7 +8,6 @@ import ( "math/rand" "os" "path/filepath" - "runtime" "time" uuid2 "github.com/google/uuid" @@ -184,7 +183,7 @@ func (a *adminServiceImpl) RefreshToken(ctx context.Context, refreshToken string func (a *adminServiceImpl) GetEnvironments(ctx context.Context) *dto.EnvironmentDTO { environments := &dto.EnvironmentDTO{ Database: string(dal.DBType) + " " + consts.DatabaseVersion, - Version: runtime.Version(), + Version: consts.SonicVersion, StartTime: consts.StartTime.UnixMilli(), Mode: util.IfElse(a.Config.Sonic.Mode == "", "production", a.Config.Sonic.Mode).(string), } From 87910a7d7dd5ec6e4c46bc74ca2d8312eea0502a Mon Sep 17 00:00:00 2001 From: xuechangjiang Date: Sun, 19 Mar 2023 17:34:12 +0800 Subject: [PATCH 3/4] feat: use github action to build and release docker image --- .github/workflows/linter.yml | 4 --- .github/workflows/release-docker.yml | 38 ++++++++++++++++++++++++++++ .github/workflows/release.yml | 2 +- .gitignore | 1 + scripts/Dockerfile | 20 +++++++++------ 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/release-docker.yml diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 76f30ba..98842fa 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -1,10 +1,6 @@ name: Linter on: - push: - paths: - - "**/*.go" - - ".github/workflows/linter.yml" pull_request: types: [opened, synchronize, reopened] paths: diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml new file mode 100644 index 0000000..a89dfc8 --- /dev/null +++ b/.github/workflows/release-docker.yml @@ -0,0 +1,38 @@ +name: Release-Docker + +on: + release: + types: [published] + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - + name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Set build time + run: | + echo "BUILD_TIME=$(date +%FT%T%z)" >> $GITHUB_ENV + - + name: Build and push + uses: docker/build-push-action@v4 + with: + platforms: linux/arm64,linux/amd64 + push: true + file: ./scripts/Dockerfile + tags: gosonic/sonic:test + build-args: | + SONIC_VERSION=${{github.ref_name}} + BUILD_COMMIT=${{github.sha}} + BUILD_TIME=${{env.BUILD_TIME}} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0205082..e477d28 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,7 +54,7 @@ jobs: - name: Show workflow information run: | echo "GOOS: $GOOS, GOARCH: $GOARCH" - echo "BUILD_TIME=$(date --rfc-3339=seconds)" >> $GITHUB_ENV + echo "BUILD_TIME=$(date +%FT%T%z)" >> $GITHUB_ENV - name: Build uses: crazy-max/ghaction-xgo@v2 diff --git a/.gitignore b/.gitignore index f04e4b6..bb40d4f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /upload /.vscode sonic.db +sonic __debug_bin /resources/template/theme/ !/resources/template/theme/default-theme-anatole \ No newline at end of file diff --git a/scripts/Dockerfile b/scripts/Dockerfile index d8cb4c7..b221601 100644 --- a/scripts/Dockerfile +++ b/scripts/Dockerfile @@ -1,14 +1,16 @@ FROM golang:1.19.3-alpine as builder - RUN apk --no-cache add git ca-certificates gcc g++ -WORKDIR /go/src/github.com/go-sonic/ - -RUN git clone --recursive --depth 1 https://github.com/go-sonic/sonic.git - +COPY . /go/src/github.com/go-sonic/sonic/ WORKDIR /go/src/github.com/go-sonic/sonic -RUN GOPROXY=https://goproxy.cn CGO_ENABLED=1 GOOS=linux go build -o sonic -ldflags="-s -w" -trimpath . +ARG BUILD_COMMIT +ARG BUILD_TIME +ARG SONIC_VERSION + + +RUN CGO_ENABLED=1 GOOS=linux && \ +go build -o sonic -ldflags="-s -w -X github.com/go-sonic/sonic/consts.SonicVersion=${SONIC_VERSION} -X github.com/go-sonic/sonic/consts.BuildCommit=${BUILD_COMMIT} -X github.com/go-sonic/sonic/consts.BuildTime=${BUILD_TIME}" -trimpath . RUN mkdir -p /app/conf && \ mkdir /app/resources && \ @@ -17,12 +19,14 @@ RUN mkdir -p /app/conf && \ cp -r /go/src/github.com/go-sonic/sonic/resources /app/ && \ cp /go/src/github.com/go-sonic/sonic/scripts/docker_init.sh /app/ + + + FROM alpine:latest as prod COPY --from=builder /app/ /app/ -RUN apk update \ - && apk add --no-cache tzdata \ +RUN apk add --no-cache tzdata \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone From 1fdcccb1711fb5409040463d3c276ab0cfe0c4fa Mon Sep 17 00:00:00 2001 From: 1379 <1379@1379.com> Date: Sun, 19 Mar 2023 19:12:23 +0800 Subject: [PATCH 4/4] fix: docker image tag name --- .github/workflows/release-docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index a89dfc8..213a19b 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -31,7 +31,7 @@ jobs: platforms: linux/arm64,linux/amd64 push: true file: ./scripts/Dockerfile - tags: gosonic/sonic:test + tags: gosonic/sonic:latest,gosonic/sonic:${{github.ref_name}} build-args: | SONIC_VERSION=${{github.ref_name}} BUILD_COMMIT=${{github.sha}}