Skip to content

Commit 2916faa

Browse files
committed
feat: fix issue and optimize build action
1 parent 35add90 commit 2916faa

File tree

15 files changed

+163
-86
lines changed

15 files changed

+163
-86
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
name: Build ECTS
22

33
on:
4-
push:
5-
branches:
6-
- master
4+
release:
5+
types: [published]
76

87
jobs:
9-
build:
10-
name: Build ECTS
8+
docker:
119
runs-on: ubuntu-latest
1210
steps:
13-
- uses: actions/checkout@master
14-
- uses: actions/setup-node@master
15-
- name: Build assets
16-
run: sudo apt-get install -y nodejs && cd web && npm install && npm run build
17-
- name: Build ECTS application
18-
run: |
19-
GO111MODULE=on go mod download
20-
GO111MODULE=on GOOS=darwin go build -ldflags "-s -w" -o "bin/ects_darwin" main.go
21-
GO111MODULE=on GOOS=linux go build -ldflags "-s -w" -o "bin/ects_linux" main.go
11+
- name: Checkout
12+
uses: actions/checkout@v3
13+
- name: Get Release Version
14+
run: echo "VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
15+
- name: Get Current Time
16+
run: echo "BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV
17+
- name: Get Current Time
18+
run: echo "GIT_COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV
19+
- name: Set up QEMU
20+
uses: docker/setup-qemu-action@v3
21+
- name: Set up Docker Buildx
22+
uses: docker/setup-buildx-action@v3
23+
- name: Login to Docker Hub
24+
uses: docker/login-action@v3
25+
with:
26+
username: ${{ secrets.DOCKERHUB_USERNAME }}
27+
password: ${{ secrets.DOCKERHUB_TOKEN }}
28+
- name: Build and push
29+
uses: docker/build-push-action@v6
30+
with:
31+
push: true
32+
tags:
33+
- betterde/ects:${{ env.VERSION }}
34+
- betterde/ects:latest
35+
context: .
36+
platforms:
37+
- linux/amd64
38+
- linux/arm64
39+
build-args: VERSION=${{ env.VERSION }} BUILD_TIME=${{ env.BUILD_TIME }} GIT_COMMIT=${{ env.GIT_COMMIT }}

Dockerfile

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
# 构建前端静态资源
2-
FROM alpine:latest AS web
3-
ARG NODEJS_VERSION=10.16.3-r0
4-
ARG YARN_VERSION=1.17.3
2+
FROM alpine:3.18 AS web
3+
ARG YARN_VERSION=1.22.19-r0
4+
ARG NODEJS_VERSION=18.20.1-r0
55
RUN apk update && \
6-
apk add nodejs=${NODEJS_VERSION} nodejs-npm=${NODEJS_VERSION} && \
7-
npm config set unsafe-perm true && \
8-
npm install --global yarn@${YARN_VERSION}
6+
apk add nodejs=${NODEJS_VERSION} yarn=${YARN_VERSION}
97
ADD web /web
108
WORKDIR /web
119
RUN yarn && yarn build
1210

1311
# 构建后端可执行文件
14-
FROM golang:1.13.0-alpine3.10 AS binary
12+
FROM golang:1.22-alpine3.18 AS binary
13+
14+
ARG VERSION=0.6.2
15+
1516
ADD . /go/src/ects
1617
WORKDIR /go/src/ects
1718
COPY --from=web /web/dist /go/src/ects/web/dist
1819
RUN apk update && \
1920
apk add --no-cache git
2021
RUN go mod tidy && \
21-
GOOS=linux go build -ldflags "-s -w" -o "bin/ects_linux" main.go
22+
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") && \
23+
GIT_COMMIT=$(git rev-parse HEAD) && \
24+
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w -X github.com/betterde/ects/internal/build.Version=${VERSION} -X github.com/betterde/ects/internal/build.Build=${BUILD_TIME} -X github.com/betterde/ects/internal/build.Commit=${GIT_COMMIT}" -o "bin/ects_linux" main.go
2225

2326
# 构建运行环境
24-
FROM alpine:3.10
25-
MAINTAINER George "[email protected]"
27+
FROM alpine:3.18
28+
29+
ARG VERSION=0.6.2
30+
31+
LABEL org.opencontainers.image.url="https://betterde.github.io/ects"
32+
LABEL org.opencontainers.image.titile="ECTS"
33+
LABEL org.opencontainers.image.vendor="Betterde Inc."
34+
LABEL org.opencontainers.image.source="https://github.com/betterde/ects"
35+
LABEL org.opencontainers.image.version="${VERSION}"
36+
LABEL org.opencontainers.image.authors="George <[email protected]>"
37+
LABEL org.opencontainers.image.created="2024-08-21 20:35:00"
38+
LABEL org.opencontainers.image.licenses="MIT"
39+
LABEL org.opencontainers.image.description="Elastic Crontab System"
40+
LABEL org.opencontainers.image.documentation="https://betterde.github.io/ects"
41+
2642
COPY --from=binary /go/src/ects/bin/ects_linux /usr/local/bin/ects
43+
44+
HEALTHCHECK --interval=60s CMD curl http://localhost/ || exit 1
45+
2746
EXPOSE 9701
2847
CMD ["master"]
2948
ENTRYPOINT ["/usr/local/bin/ects"]

cmd/master.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"github.com/betterde/ects/internal/build"
67
"github.com/betterde/ects/routes"
78
"github.com/kataras/iris/v12"
89
"github.com/satori/go.uuid"
@@ -35,7 +36,7 @@ var (
3536
master = &service.Instance{
3637
Mode: models.MASTER,
3738
Status: models.ONLINE,
38-
Version: rootCmd.Version,
39+
Version: build.Version,
3940
}
4041

4142
ctx, cancelFunc = context.WithCancel(context.Background())
@@ -130,7 +131,7 @@ func start() {
130131

131132
go register()
132133

133-
if err := app.Run(iris.Addr(addr), iris.WithoutInterruptHandler, iris.WithOptimizations, iris.WithCharset("UTF-8")); err != nil {
134+
if err := app.Run(iris.Addr(addr), iris.WithoutInterruptHandler, iris.WithOptimizations, iris.WithCharset("UTF-8"), iris.WithoutBodyConsumptionOnUnmarshal); err != nil {
134135
log.Fatal(err)
135136
}
136137
}

cmd/root.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
"github.com/betterde/ects/internal/build"
46
"github.com/spf13/cobra"
57
"os"
68
)
79

810
var rootCmd = &cobra.Command{
9-
Use: "ects",
10-
Short: "Elastic Crontab System",
11-
Long: "Elastic Crontab System",
12-
Version: "0.6.1",
11+
Use: build.Name,
12+
Short: build.Desc,
13+
Version: fmt.Sprintf("Version: %s\nBuild at: %s\nCommit hash: %s", build.Version, build.Build, build.Commit),
1314
}
1415

1516
func Execute() {
1617
if err := rootCmd.Execute(); err != nil {
1718
os.Exit(1)
1819
}
1920
}
21+
22+
func init() {
23+
// Print app version
24+
rootCmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")
25+
}

cmd/worker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"github.com/betterde/ects/config"
6+
"github.com/betterde/ects/internal/build"
67
"github.com/betterde/ects/internal/discover"
78
"github.com/betterde/ects/internal/pipeline"
89
"github.com/betterde/ects/internal/scheduler"
@@ -32,7 +33,7 @@ var (
3233
worker = &service.Instance{
3334
Mode: models.WORKER,
3435
Status: models.ONLINE,
35-
Version: rootCmd.Version,
36+
Version: build.Version,
3637
}
3738
)
3839

controllers/account/profile.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package account
22

33
import (
4-
"github.com/dgrijalva/jwt-go"
54
"github.com/kataras/iris/v12"
5+
"github.com/kataras/iris/v12/middleware/jwt"
66
"github.com/kataras/iris/v12/mvc"
77

88
"github.com/betterde/ects/internal/response"
@@ -24,10 +24,9 @@ type (
2424

2525
// Get 获取用户信息
2626
func (instance *Controller) Get(ctx iris.Context) mvc.Result {
27-
token := ctx.Values().Get("jwt").(*jwt.Token)
28-
claims, _ := token.Claims.(jwt.MapClaims)
29-
id := claims["sub"]
30-
user, err := instance.Service.FindByID(id.(string))
27+
claims := jwt.Get(ctx).(jwt.Claims)
28+
id := claims.Subject
29+
user, err := instance.Service.FindByID(id)
3130
if err != nil {
3231
return response.NotFound(err.Error())
3332
}

controllers/auth/authentication.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"github.com/betterde/ects/internal/response"
66
"github.com/betterde/ects/models"
77
"github.com/betterde/ects/services"
8+
"github.com/go-playground/validator/v10"
89
"github.com/kataras/iris/v12"
910
"github.com/kataras/iris/v12/mvc"
10-
"gopkg.in/go-playground/validator.v9"
1111
"log"
1212
)
1313

@@ -35,24 +35,22 @@ type (
3535
}
3636
)
3737

38-
// 路由分发
3938
func (instance *Controller) BeforeActivation(request mvc.BeforeActivation) {
4039
request.Handle("POST", "/signin", "SignInHandler")
4140
request.Handle("POST", "/signout", "SignOutHandler")
4241
}
4342

44-
// 用户登录逻辑
4543
func (instance *Controller) SignInHandler(ctx iris.Context) mvc.Response {
4644
var params SignIn
4745
validate := validator.New()
4846
if err := ctx.ReadJSON(&params); err != nil {
49-
// TODO Add logger
47+
return response.ValidationError(err.Error())
5048
}
5149

5250
err := validate.Struct(params)
5351

5452
if err != nil {
55-
return response.ValidationError("用户名和密码不能为空")
53+
return response.ValidationError(err.Error())
5654
}
5755

5856
token, err := instance.Service.Attempt(params.Username, params.Password)

docker-compose.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ services:
22
etcd:
33
image: gcr.io/etcd-development/etcd:v3.5.6
44
ports:
5-
- 2379:2379
6-
- 2380:2380
5+
- "0.0.0.0:2379:2379"
6+
- "0.0.0.0:2380:2380"
77
hostname: etcd
88
container_name: etcd
99
environment:
@@ -32,7 +32,16 @@ services:
3232
networks:
3333
- ects
3434
ects:
35-
image: ects:0.5.1
35+
image: betterde/ects:latest
36+
build:
37+
context: .
38+
dockerfile: Dockerfile
39+
args:
40+
- VERSION=${VERSION}
41+
- BUILD_TIME=${BUILD_TIME}
42+
- GIT_COMMIT=${GIT_COMMIT}
43+
- YARN_VERSION=1.22.19-r0
44+
- NODEJS_VERSION=18.20.1-r0
3645
hostname: ects
3746
container_name: ects
3847
command: ects master --etcd etcd:2379

go.mod

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.21
55
require (
66
github.com/coreos/etcd v3.3.13+incompatible
77
github.com/dgrijalva/jwt-go v3.2.0+incompatible
8+
github.com/go-playground/validator/v10 v10.25.0
89
github.com/go-sql-driver/mysql v1.4.1
910
github.com/go-xorm/builder v0.3.2
1011
github.com/go-xorm/core v0.6.1-0.20181008132326-6bc9412b1c4d
@@ -13,7 +14,7 @@ require (
1314
github.com/kataras/iris/v12 v12.2.7
1415
github.com/satori/go.uuid v1.2.0
1516
github.com/spf13/cobra v0.0.5
16-
golang.org/x/crypto v0.14.0
17+
golang.org/x/crypto v0.32.0
1718
gopkg.in/go-playground/validator.v9 v9.27.0
1819
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
1920
gopkg.in/yaml.v2 v2.4.0
@@ -35,9 +36,10 @@ require (
3536
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
3637
github.com/fatih/structs v1.1.0 // indirect
3738
github.com/flosch/pongo2/v4 v4.0.2 // indirect
39+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
3840
github.com/ghodss/yaml v1.0.0 // indirect
39-
github.com/go-playground/locales v0.12.1 // indirect
40-
github.com/go-playground/universal-translator v0.16.0 // indirect
41+
github.com/go-playground/locales v0.14.1 // indirect
42+
github.com/go-playground/universal-translator v0.18.1 // indirect
4143
github.com/gobwas/httphead v0.1.0 // indirect
4244
github.com/gobwas/pool v0.2.1 // indirect
4345
github.com/gobwas/ws v1.3.0 // indirect
@@ -66,7 +68,7 @@ require (
6668
github.com/kataras/tunnel v0.0.4 // indirect
6769
github.com/klauspost/compress v1.17.2 // indirect
6870
github.com/kr/pretty v0.2.1 // indirect
69-
github.com/leodido/go-urn v1.1.0 // indirect
71+
github.com/leodido/go-urn v1.4.0 // indirect
7072
github.com/mailgun/raymond/v2 v2.0.48 // indirect
7173
github.com/mailru/easyjson v0.7.7 // indirect
7274
github.com/mediocregopher/radix/v3 v3.8.1 // indirect
@@ -94,9 +96,9 @@ require (
9496
go.uber.org/multierr v1.1.0 // indirect
9597
go.uber.org/zap v1.10.0 // indirect
9698
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
97-
golang.org/x/net v0.17.0 // indirect
98-
golang.org/x/sys v0.13.0 // indirect
99-
golang.org/x/text v0.13.0 // indirect
99+
golang.org/x/net v0.34.0 // indirect
100+
golang.org/x/sys v0.29.0 // indirect
101+
golang.org/x/text v0.21.0 // indirect
100102
golang.org/x/time v0.3.0 // indirect
101103
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
102104
google.golang.org/appengine v1.6.1 // indirect

0 commit comments

Comments
 (0)