From 16474359ca6c3c96a75b661335d3cf625075278c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cachuyko=E2=80=9D?= <“achuyko@algoritmiclab.ai”> Date: Fri, 23 Aug 2024 15:27:50 +0300 Subject: [PATCH 1/2] Set up AWS Docker repository CI pipeline --- .github/workflows/chat_server_ci.yaml | 59 +++++++++++++++++++++++++++ Dockerfile | 14 +++++++ Makefile | 18 ++++++++ go.mod | 2 +- go.sum | 2 +- 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/chat_server_ci.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/chat_server_ci.yaml b/.github/workflows/chat_server_ci.yaml new file mode 100644 index 0000000..4d6e8f8 --- /dev/null +++ b/.github/workflows/chat_server_ci.yaml @@ -0,0 +1,59 @@ +name: CI + +on: + push: + branches: [ main,master ] + pull_request: + branches: [ main, master ] + +env: + IMAGE_NAME: "chat-server" + CONTAINER_NAME: "chat-server" + +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 50052:50052 --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..8387248 --- /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/chat-server cmd/grpc_server/main.go + +FROM alpine:latest + +WORKDIR /root/ +COPY --from=builder /github.com/Chuiko-GIT/chat/source/bin/chat-server . + +CMD ["./chat-server"] \ No newline at end of file diff --git a/Makefile b/Makefile index 8511c5d..450b6c4 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,21 @@ LOCAL_BIN:=$(CURDIR)/bin install-deps: GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1 GOBIN=$(LOCAL_BIN) go install -mod=mod google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 + GOBIN=$(LOCAL_BIN) go install github.com/pressly/goose/v3/cmd/goose@v3.14.0 get-deps: go get -u google.golang.org/protobuf/cmd/protoc-gen-go go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc +local-migration-status: + ${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} status -v + +local-migration-up: + ${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} up -v + +local-migration-down: + ${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} down -v + generate: make generate-chat-api @@ -19,3 +29,11 @@ generate-chat-api: --go-grpc_out=pkg/chat_api --go-grpc_opt=paths=source_relative \ --plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \ api/chat_api/chat_api.proto + +build: + GOOS=linux GOARCH=amd64 go build -o chat-server cmd/grpc_server/main.go + +docker-build-and-push: + docker buildx build --no-cache --platform linux/amd64 -t ${REGISTRY}/chat-server:v0.0.1 . + aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin ${REGISTRY} + docker push ${REGISTRY} \ No newline at end of file diff --git a/go.mod b/go.mod index aff4f5d..6bb05e5 100644 --- a/go.mod +++ b/go.mod @@ -16,4 +16,4 @@ require ( golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.16.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect -) +) \ No newline at end of file diff --git a/go.sum b/go.sum index b5bc188..d2021b9 100644 --- a/go.sum +++ b/go.sum @@ -22,4 +22,4 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go. google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= \ No newline at end of file From aa6bc302f514a5df87ec9300bd7ccf290294d92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cachuyko=E2=80=9D?= <“achuyko@algoritmiclab.ai”> Date: Fri, 23 Aug 2024 15:33:21 +0300 Subject: [PATCH 2/2] change dockerfile golang alpine version --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8387248..14047b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21-alpine AS builder +FROM golang:1.22.5-alpine AS builder COPY . /github.com/Chuiko-GIT/chat/source/ WORKDIR /github.com/Chuiko-GIT/chat/source/