From f45109e27f6db00849ab64aae7d3f2794ee7bb81 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 11:09:57 +0700 Subject: [PATCH 1/6] feat: create ci workflow --- .github/workflows/build-deploy.yml | 53 +++++++++++++++++++++++++++++ .github/workflows/run-unit-test.yml | 33 ++++++++++++++++++ .github/workflows/test-build.yml | 28 +++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/build-deploy.yml create mode 100644 .github/workflows/run-unit-test.yml create mode 100644 .github/workflows/test-build.yml diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml new file mode 100644 index 0000000..ee0882c --- /dev/null +++ b/.github/workflows/build-deploy.yml @@ -0,0 +1,53 @@ +name: "Build and Deploy" + +on: + push: + branches: + - main + - beta + - dev + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21 + check-latest: true + cache: true + + - name: Download dependencies + run: go mod download + + - name: Vet + run: | + go vet ./... + + - name: Test + run: | + go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/... + go tool cover -func="./coverage.out" + + build: + runs-on: ubuntu-latest + needs: + - test + + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build Docker image + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + push: false + tags: test \ No newline at end of file diff --git a/.github/workflows/run-unit-test.yml b/.github/workflows/run-unit-test.yml new file mode 100644 index 0000000..3ec7c01 --- /dev/null +++ b/.github/workflows/run-unit-test.yml @@ -0,0 +1,33 @@ +name: "Pull request: Run unit test" + +on: + pull_request: + branches: + - main + - beta + - dev + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21 + check-latest: true + cache: true + + - name: Download dependencies + run: go mod download + + - name: Vet + run: | + go vet ./... + + - name: Test + run: | + go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/... + go tool cover -func="./coverage.out" diff --git a/.github/workflows/test-build.yml b/.github/workflows/test-build.yml new file mode 100644 index 0000000..a05821c --- /dev/null +++ b/.github/workflows/test-build.yml @@ -0,0 +1,28 @@ +name: "Pull request: Build test" + +on: + pull_request: + branches: + - main + - beta + - dev + +jobs: + docker-build: + name: "Docker build and create an image" + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build Docker image + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + push: false + tags: test \ No newline at end of file From 416c36f613dba01a3a5ef453085eabed18fa3fc3 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 11:22:41 +0700 Subject: [PATCH 2/6] fix: import package --- src/internal/service/jwt/jwt.service.go | 6 +++--- src/pkg/service/jwt/jwt.service.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/internal/service/jwt/jwt.service.go b/src/internal/service/jwt/jwt.service.go index 8cb607b..ea77094 100644 --- a/src/internal/service/jwt/jwt.service.go +++ b/src/internal/service/jwt/jwt.service.go @@ -3,7 +3,7 @@ package jwt import ( _jwt "github.com/golang-jwt/jwt/v4" "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" + "github.com/isd-sgcu/johnjud-auth/src/pkg/strategy" ) type serviceImpl struct { @@ -11,8 +11,8 @@ type serviceImpl struct { strategy strategy.JwtStrategy } -func NewService(confif config.Jwt, strategy strategy.JwtStrategy) *serviceImpl { - return &serviceImpl{config: confif, strategy: strategy} +func NewService(config config.Jwt, strategy strategy.JwtStrategy) *serviceImpl { + return &serviceImpl{config: config, strategy: strategy} } func (s *serviceImpl) SignAuth(userId string) (string, error) { diff --git a/src/pkg/service/jwt/jwt.service.go b/src/pkg/service/jwt/jwt.service.go index 72aba61..db66000 100644 --- a/src/pkg/service/jwt/jwt.service.go +++ b/src/pkg/service/jwt/jwt.service.go @@ -4,7 +4,7 @@ import ( _jwt "github.com/golang-jwt/jwt/v4" "github.com/isd-sgcu/johnjud-auth/src/config" "github.com/isd-sgcu/johnjud-auth/src/internal/service/jwt" - "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" + "github.com/isd-sgcu/johnjud-auth/src/pkg/strategy" ) type Service interface { From 2c0b40e11da4316915fc3b8976f878b660cf3546 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 11:22:57 +0700 Subject: [PATCH 3/6] feat: create Dockerfile --- Dockerfile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1139f33 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +# Base Image +FROM golang:1.21.4-alpine3.18 as base + +# Working directory +WORKDIR /app + +# Copy go.mod and go.sum files +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy the source code +COPY ./src ./src + +# Build the application +RUN go build -o server ./src/. + +# Copy config files +COPY ./config ./config + +# Adding the grpc_health_probe +RUN GRPC_HEALTH_PROBE_VERSION=v0.3.1 && \ + wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ + chmod +x /bin/grpc_health_probe + +# Create master image +FROM alpine AS master + +# Working directory +WORKDIR /app + +# Copy grpc_heath_prob +COPY --from=base /bin/grpc_health_probe ./ + +# Copy execute file +COPY --from=base /app/server ./ + +# Copy config files +COPY --from=base /app/config ./config + +# Set ENV to production +ENV GO_ENV production + +# Expose port 3001 +EXPOSE 3001 + +# Run the application +CMD ["./server"] \ No newline at end of file From 0bf1c354c671e8c4a0b6fc440ba3b0e2cc9ef560 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 11:26:38 +0700 Subject: [PATCH 4/6] fix: import package --- src/internal/service/jwt/jwt.service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/service/jwt/jwt.service_test.go b/src/internal/service/jwt/jwt.service_test.go index 65627c5..3b00a66 100644 --- a/src/internal/service/jwt/jwt.service_test.go +++ b/src/internal/service/jwt/jwt.service_test.go @@ -2,7 +2,7 @@ package jwt import ( "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" + "github.com/isd-sgcu/johnjud-auth/src/pkg/strategy" "github.com/stretchr/testify/suite" "testing" ) From 4299dbf2689079de282437f6fc5e8d2371709a05 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 11:29:44 +0700 Subject: [PATCH 5/6] fix: ci workflow --- .github/workflows/build-deploy.yml | 2 +- .github/workflows/run-unit-test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml index ee0882c..9e31663 100644 --- a/.github/workflows/build-deploy.yml +++ b/.github/workflows/build-deploy.yml @@ -29,7 +29,7 @@ jobs: - name: Test run: | - go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/... + go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... go tool cover -func="./coverage.out" build: diff --git a/.github/workflows/run-unit-test.yml b/.github/workflows/run-unit-test.yml index 3ec7c01..1bc13b0 100644 --- a/.github/workflows/run-unit-test.yml +++ b/.github/workflows/run-unit-test.yml @@ -29,5 +29,5 @@ jobs: - name: Test run: | - go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/... + go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... go tool cover -func="./coverage.out" From afa13e4c189d1fa4a8f5591edcff502d214cc8a0 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Sat, 23 Dec 2023 13:08:00 +0700 Subject: [PATCH 6/6] fix: port --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1139f33..4fc6518 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,8 +42,8 @@ COPY --from=base /app/config ./config # Set ENV to production ENV GO_ENV production -# Expose port 3001 -EXPOSE 3001 +# Expose port 3002 +EXPOSE 3002 # Run the application CMD ["./server"] \ No newline at end of file