diff --git a/.github/workflows/auth_ci.yaml b/.github/workflows/auth_ci.yaml new file mode 100644 index 0000000..70fb3d0 --- /dev/null +++ b/.github/workflows/auth_ci.yaml @@ -0,0 +1,59 @@ +name: CI + +on: + push: + branches: [ main, master ] + pull_request: + branches: [ main, master ] + +env: + IMAGE_NAME: "auth" + CONTAINER_NAME: "auth" + +jobs: + image-build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout master + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Registery + run: echo ${{ secrets.CREADS }} | docker login --username AWS --password-stdin ${{ secrets.REGISTRY }} + + - name: Build and Push Docker Image + run: | + TAG_NAME=$(echo $GITHUB_SHA | head -c7) + docker buildx create --use + docker buildx build --no-cache --push --tag ${{ secrets.REGISTRY }}/$IMAGE_NAME:$TAG_NAME -f ./Dockerfile . + + deploy-image: + runs-on: ubuntu-latest + needs: image-build-and-push + + steps: + - name: Deploy to Selectel Cloud via SSH action + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSHKEY }} + envs: IMAGE_NAME,GITHUB_SHA,CONTAINER_NAME + script: | + # Set up variables + TAG_NAME=$(echo $GITHUB_SHA | head -c7) + + # Login into Selectel Registry + echo ${{ secrets.CREADS }} | docker login --username AWS --password-stdin ${{ secrets.REGISTRY }} + + # Stop running container + docker stop $CONTAINER_NAME + + # Remove old container + docker rm $CONTAINER_NAME + + # Run a new container from a new image + docker run -d -p 50051:50051 --name $CONTAINER_NAME -t ${{ secrets.REGISTRY }}/$IMAGE_NAME:$TAG_NAME \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..295cb93 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.21-alpine AS builder + +COPY . /github.com/Chuiko-GIT/chat/source/ +WORKDIR /github.com/Chuiko-GIT/chat/source/ + +RUN go mod download +RUN go build -o ./bin/auth cmd/grpc_server/main.go + +FROM alpine:latest + +WORKDIR /root/ +COPY --from=builder /github.com/Chuiko-GIT/chat/source/bin/auth . + +CMD ["./auth"] diff --git a/Makefile b/Makefile index 9582680..3bba730 100644 --- a/Makefile +++ b/Makefile @@ -18,4 +18,12 @@ generate-user-api: --plugin=protoc-gen-go=bin/protoc-gen-go \ --go-grpc_out=pkg/user_api --go-grpc_opt=paths=source_relative \ --plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \ - api/user_api/user_api.proto \ No newline at end of file + api/user_api/user_api.proto + +build: + GOOS=linux GOARCH=amd64 go build -o auth cmd/grpc_server/main.go + +docker-build-and-push: + docker buildx build --no-cache --platform linux/amd64 -t ${REGISTRY}/auth:v0.0.1 . + aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin ${REGISTRY} + docker push ${REGISTRY} diff --git a/cmd/grpc_server/main.go b/cmd/grpc_server/main.go index ee71f7c..9b3568b 100644 --- a/cmd/grpc_server/main.go +++ b/cmd/grpc_server/main.go @@ -40,7 +40,7 @@ func (s server) Update(ctx context.Context, req *user_api.UpdateRequest) (*empty func (s server) Get(ctx context.Context, req *user_api.GetRequest) (*user_api.GetResponse, error) { return &user_api.GetResponse{ User: &user_api.User{ - Id: gofakeit.Int64(), + Id: req.GetId(), User: &user_api.UserInfo{ Name: gofakeit.Name(), Email: gofakeit.Email(),