From ed4caa515f7e21ada1886b09193e31064f6b968d Mon Sep 17 00:00:00 2001 From: linuxMate Date: Thu, 9 Nov 2023 12:55:58 +0300 Subject: [PATCH] CD test --- .github/workflows/CD.yaml | 30 +++++--- .github/workflows/CI.yaml | 4 +- .github/workflows/config.yaml | 136 ---------------------------------- build/Dockerfile | 22 ++++-- docker-compose.yml | 51 ++++++------- 5 files changed, 65 insertions(+), 178 deletions(-) delete mode 100644 .github/workflows/config.yaml diff --git a/.github/workflows/CD.yaml b/.github/workflows/CD.yaml index 216e2adb..4c40b010 100644 --- a/.github/workflows/CD.yaml +++ b/.github/workflows/CD.yaml @@ -2,7 +2,7 @@ name: CD env: REGISTRY: "codemaster482" - IMAGE_NAME: "HammyWallet-Server" + IMAGE_NAME: "hammywallet" CONTAINER_NAME: "HammyWallet-API" FOLDER_COMPOSE: "backend/2023_2_Hamster" @@ -65,7 +65,7 @@ jobs: context: . push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - file: ./build/Dockerfile.multistage.main + file: ./build/Dockerfile build-args: | IMAGE_NAME=${{ env.IMAGE_NAME }} REGISTRY=${{ env.REGISTRY }} @@ -100,19 +100,29 @@ jobs: sudo bash cd $(echo $FOLDER_COMPOSE) - # Make .env variables file cat <.env - POSTGRES_DB=${{ secrets.POSTGRES_DB }} - POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} + DB_NAME=${{ secrets.DB_NAME }} + DB_USER=${{ secrets.POSTGRES_USER }} + DB_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} + DB_HOST=${{ secrets.DB_HOST }} DB_PORT=${{ secrets.DB_PORT }} - POSTGRES_USER=${{ secrets.POSTGRES_USER }} - PGADMIN_DEFAULT_EMAIL=${{ secrets.PGADMIN_DEFAULT_EMAIL }} - PGADMIN_DEFAULT_PASSWORD=${{ secrets.PGADMIN_DEFAULT_PASSWORD }} - JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }} + + + SECRET=${{ secrets.SECRET }} IMAGE_NAME=${{ env.IMAGE_NAME }} REGISTRY=${{ env.REGISTRY }} CONTAINER_NAME=${{ env.CONTAINER_NAME }} - EOF \ No newline at end of file + EOF + + echo "GITHUB_SHA_SHORT=`echo $GITHUB_SHA | head -c8`" >> .env + + echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin + + docker pull $REGISTRY/$IMAGE_NAME:latest + + docker system prune -f + docker compose down + docker compose up -d diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index ee14f52b..c288b826 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -12,7 +12,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v3 with: - go-version: '1.21' + go-version: '1.21.x' - name: Cache Go modules uses: actions/cache@v3 @@ -46,7 +46,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v3 with: - go-version: '1.21' + go-version: '1.21.x' - name: Install dependencies run: go mod tidy diff --git a/.github/workflows/config.yaml b/.github/workflows/config.yaml deleted file mode 100644 index 643d3bba..00000000 --- a/.github/workflows/config.yaml +++ /dev/null @@ -1,136 +0,0 @@ -#name: ci -# -#on: -# push: -# branches: -# - develop -# - main -# pull_request: -# -#env: -# REGISTRY: "mrdjeb" -# IMAGE_NAME: "zuzu-main-web" -# CONTAINER_NAME: "zuzu-main" -# FOLDER_COMPOSE: "zuzu-main-deploy" -# GITHUB_SHA_SHORT: -# -# -jobs: - lint_and_test: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Go - uses: actions/setup-go@v4 - with: - go-version: '1.21.x' - cache: false - - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - - - name: Test with the Go CLI - run: go test ./... - -# build_and_push: -# runs-on: ubuntu-latest -# needs: lint_and_test -# -# steps: -# - name: Checkout -# uses: actions/checkout@v4 -# -# - name: Set up QEMU -# uses: docker/setup-qemu-action@v2 -# -# - name: Set up Docker Buildx -# uses: docker/setup-buildx-action@v2 -# -# - name: Login to DockerHub -# uses: docker/login-action@v2 -# with: -# username: ${{ secrets.DOCKERHUB_USERNAME }} -# password: ${{ secrets.DOCKERHUB_TOKEN }} -# -# - name: Set env -# run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | head -c 8)" >> $GITHUB_ENV -# -# - name: Build and push -# uses: docker/build-push-action@v4 -# with: -# context: . -# push: true -# tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.GITHUB_SHA_SHORT }}, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest -# file: ./build/Dockerfile.multistage.main -# build-args: | -# IMAGE_NAME=${{ env.IMAGE_NAME }} -# REGISTRY=${{ env.REGISTRY }} -# GITHUB_SHA_SHORT=${{ env.GITHUB_SHA_SHORT }} -# -# deploy: -# runs-on: ubuntu-latest -# needs: build_and_push -# -# steps: -# - name: Checkout -# uses: actions/checkout@v3 -# -# - name: Deploy docker-compose.yml via SSH action -# uses: appleboy/scp-action@v0.1.4 -# with: -# host: ${{ secrets.HOST }} -# port: ${{ secrets.PORT }} -# username: ${{ secrets.USERNAME }} -# key: ${{ secrets.SSHKEY }} -# rm: true -# source: docker-compose.yaml, build/sql/create.sql -# target: ~/${{ env.FOLDER_COMPOSE }} -# -# -# - name: Deploy to DockerHub via SSH action -# uses: appleboy/ssh-action@v1.0.0 -# with: -# host: ${{ secrets.HOST }} -# port: ${{ secrets.PORT }} -# username: ${{ secrets.USERNAME }} -# key: ${{ secrets.SSHKEY }} -# envs: REGISTRY, IMAGE_NAME, CONTAINER_NAME, GITHUB_SHA, FOLDER_COMPOSE -# script: | -# sudo bash -# cd $(echo $FOLDER_COMPOSE) -# -# # Make .env variables file -# cat <.env -# -# POSTGRES_DB=${{ secrets.POSTGRES_DB }} -# POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} -# DB_HOST=${{ secrets.DB_HOST }} -# DB_PORT=${{ secrets.DB_PORT }} -# POSTGRES_USER=${{ secrets.POSTGRES_USER }} -# PGADMIN_DEFAULT_EMAIL=${{ secrets.PGADMIN_DEFAULT_EMAIL }} -# PGADMIN_DEFAULT_PASSWORD=${{ secrets.PGADMIN_DEFAULT_PASSWORD }} -# JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }} -# -# IMAGE_NAME=${{ env.IMAGE_NAME }} -# REGISTRY=${{ env.REGISTRY }} -# CONTAINER_NAME=${{ env.CONTAINER_NAME }} -# EOF -# echo "GITHUB_SHA_SHORT=`echo $GITHUB_SHA | head -c8`" >> .env -# -# # Make init script1 for postgres container, install the extension "uuid-ossp" for postgres -# echo "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" > init_uuid.sql -# -# # <==========!==========> -# # Log in to DockerHub -# echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin -# -# # Pull image DockerHub -# docker pull $REGISTRY/$IMAGE_NAME:latest -# -# # Restart docker-compose -# docker system prune -f -# docker compose down -# docker compose up -d -# # <==========!==========> \ No newline at end of file diff --git a/build/Dockerfile b/build/Dockerfile index 6c6c37cc..a995c64a 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,6 +1,18 @@ -FROM golang:latest -COPY . /app -WORKDIR /app +FROM golang:1.21.0-alpine AS build + +COPY . /github.com/go-park-mail-ru/2023_2_Hamster/ +WORKDIR /github.com/go-park-mail-ru/2023_2_Hamster/ + RUN go mod download -RUN go build -o app ./cmd/api/main.go -ENTRYPOINT ["./app"] \ No newline at end of file +RUN go clean --modcache +RUN go build -mod=readonly -o ./.server ./cmd/api/main.go + +FROM scratch AS run + +WORKDIR /docker-HammyWallet/ + +COPY --from=builder /github.com/go-park-mail-ru/2023_2_Hamster/.server . + +EXPOSE 80 433 + +ENTRYPOINT ["./server"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 9802ac4b..19987202 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,25 +5,8 @@ networks: driver: bridge services: - - server: - container_name: ${SERVER_NAME_PROD} - build: - context: . - dockerfile: ./build/Dockerfile - restart: on-failure - networks: - - app-network - ports: - - "8080:8080" - depends_on: - - postgres - - redis - env_file: - - .env - - postgres: - container_name: ${DB_CONTAINER_NAME} + hammy-postgres: + container_name: hammy-postgres image: postgres:latest restart: on-failure env_file: @@ -59,11 +42,11 @@ services: - "-c" - "pg_stat_statements.track=all" ports: - - "5436:5432" + - "${DB_PORT}:5432" environment: - POSTGRES_DB: ${DB_NAME_PROD} - POSTGRES_USER: ${DB_USER_PROD} - POSTGRES_PASSWORD: ${DB_PASSWORD_PROD} + POSTGRES_DB: ${DB_NAME} + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} volumes: - ./cmd/api/init/db/postgresql/schema/:/docker-entrypoint-initdb.d/ healthcheck: @@ -75,11 +58,29 @@ services: networks: - app-network - redis: - container_name: ${REDIS_CONTAINER_NAME} + + hammy-redis: + container_name: hammy-redis image: redis:latest restart: on-failure networks: - app-network ports: - "6379:6379" + + + hammywallet-api: + container_name: hammywallet-api + env_file: + - .env + build: + context: . + dockerfile: ./build/Dockerfile + restart: on-failure + ports: + - "8080:8080" + depends_on: + - postgres + - redis + networks: + - app-network