From 70831360b438306eede7afc007765a54c8c87082 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 18:07:03 +0400 Subject: [PATCH 01/27] new ci cd, separate dev and prod build --- .github/workflows/build_and_deploy.yml | 66 ++++++++++++++++++++++++++ .github/workflows/deploy.yml | 47 ------------------ .github/workflows/develop.yml | 47 ------------------ docker-compose.dev.yml | 8 ++++ docker-compose.prod.yml | 22 +++++++++ docker-compose.yaml | 36 -------------- docker-compose.yml | 17 +++++++ 7 files changed, 113 insertions(+), 130 deletions(-) create mode 100644 .github/workflows/build_and_deploy.yml delete mode 100644 .github/workflows/deploy.yml delete mode 100644 .github/workflows/develop.yml create mode 100644 docker-compose.dev.yml create mode 100644 docker-compose.prod.yml delete mode 100644 docker-compose.yaml create mode 100644 docker-compose.yml diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml new file mode 100644 index 0000000..12f1d91 --- /dev/null +++ b/.github/workflows/build_and_deploy.yml @@ -0,0 +1,66 @@ +name: CI/CD with Docker Compose + +on: + push: + branches: + - develop + - main + +env: + REPO_NAME: ${{ github.event.repository.name }} + +jobs: + build: + runs-on: [self-hosted] + strategy: + matrix: + environment: + - { name: "develop", compose_file: "docker-compose.dev.yml", project_name_suffix: "_dev" } + - { name: "main", compose_file: "docker-compose.prod.yml", project_name_suffix: "_prod" } + include: + - environment: "develop" + TELEGRAM_BOT_TOKEN: ${{ secrets.DEV_TELEGRAM_BOT_TOKEN }} + - environment: "main" + TELEGRAM_BOT_TOKEN: ${{ secrets.PROD_TELEGRAM_BOT_TOKEN }} + if: github.ref == 'refs/heads/${{ matrix.environment.name }}' + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Build with Docker Compose + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + key: ${{ secrets.PRIVATE_KEY }} + script: | + cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} + docker compose -p ${{ env.REPO_NAME }}${{ matrix.environment.project_name_suffix }} -f ${{ matrix.environment.compose_file }} build --no-cache + + deploy: + needs: build + runs-on: [ self-hosted ] + strategy: + matrix: + include: + - environment: dev + compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" + - environment: prod + compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" + if: github.ref == 'refs/heads/${{ matrix.environment }}' + steps: + - name: SSH into server and deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + key: ${{ secrets.PRIVATE_KEY }} + script: | + cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} + docker compose ${matrix.compose_files} -p ${{ env.REPO_NAME }}_${{ matrix.environment }} up -d + sleep 10 # Wait a bit for services to start + if ! docker compose ${matrix.compose_files} -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ps | grep -q 'Up'; then + echo "One or more containers are not running as expected." + exit 1 + fi diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index d0d98cc..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: CI/CD with Docker Compose -env: - REPO_NAME: ${{ github.event.repository.name }} - -on: - push: - branches: - - main - -jobs: - build: - runs-on: [self-hosted] - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: executing remote ssh commands using password - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - key: ${{ secrets.PRIVATE_KEY }} - script: | - cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose build --no-cache - - deploy: - needs: [build] - runs-on: [self-hosted] - steps: - - name: SSH into production server and deploy - uses: appleboy/ssh-action@master - env: - TELEGRAM_BOT_TOKEN: ${{ secrets.PROD_TELEGRAM_BOT_TOKEN }} - DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} - SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - key: ${{ secrets.PRIVATE_KEY }} - script: | - cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose down - docker compose up -d - docker compose ps - envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH \ No newline at end of file diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml deleted file mode 100644 index 2ca6182..0000000 --- a/.github/workflows/develop.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: CI/CD with Docker Compose -env: - REPO_NAME: ${{ github.event.repository.name }} - -on: - push: - branches: - - develop - -jobs: - build: - runs-on: [self-hosted] - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: executing remote ssh commands using password - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - key: ${{ secrets.PRIVATE_KEY }} - script: | - cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose build --no-cache - - deploy: - needs: [build] - runs-on: [self-hosted] - steps: - - name: SSH into production server and deploy - uses: appleboy/ssh-action@master - env: - TELEGRAM_BOT_TOKEN: ${{ secrets.DEV_TELEGRAM_BOT_TOKEN }} - DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} - SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - key: ${{ secrets.PRIVATE_KEY }} - script: | - cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose down - docker compose up -d - docker compose ps - envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..8908ead --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,8 @@ +version: '3.7' # Use a more recent version if needed + +services: + bot: + volumes: + - sqlite_data_dev:/database +volumes: + sqlite_data_dev: \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..9fd30ac --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,22 @@ +version: '3.7' # Use a more recent version if needed + +services: + bot: + build: . + environment: + SQLITE_DB_FILE_PATH: /database/db.sqlite + TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN} + DEVELOPER_CHAT_ID: ${DEVELOPER_CHAT_ID} + TZ: ${TZ-GMT} # Default to GMT if TZ is not specified + volumes: + - sqlite_data:/database + entrypoint: ["/bin/sh", "-c"] + command: | + python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; + python src/__main__.py; + restart: always + +volumes: + sqlite_data: + +# Only define services specific to development or production in their respective override files diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 498bd90..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,36 +0,0 @@ -version: '3' - -services: - bot: - build: . - environment: - - SQLITE_DB_FILE_PATH=/database/db.sqlite - - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN} - - DEVELOPER_CHAT_ID=${DEVELOPER_CHAT_ID} - - TZ=GMT - volumes: - - sqlite_data:/database - entrypoint: ["/bin/sh", "-c"] - command: - - | - python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; - python src/__main__.py; - restart: always - - sqlitebrowser: - image: linuxserver/sqlitebrowser - ports: - - "3000:3000" - environment: - - PUID=1000 - - PGID=1000 - - TZ=GMT - - ENABLE_UWSGI=true - volumes: - - sqlite_data:/database - profiles: - - sqlitebrowser - restart: unless-stopped - -volumes: - sqlite_data: \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e2c1294 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.7' # Use a more recent version if needed + +services: + bot: + build: . + environment: + SQLITE_DB_FILE_PATH: /database/db.sqlite + TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN} + DEVELOPER_CHAT_ID: ${DEVELOPER_CHAT_ID} + TZ: ${TZ-GMT} # Default to GMT if TZ is not specified + entrypoint: ["/bin/sh", "-c"] + command: | + python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; + python src/__main__.py; + restart: always + +# Only define services specific to development or production in their respective override files From 095faa9a52156a72fe88cf9f57188014961e059d Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 18:19:00 +0400 Subject: [PATCH 02/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 37 +++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 12f1d91..001f0d4 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -1,4 +1,4 @@ -name: CI/CD with Docker Compose +name: Build and Deploy Workflow on: push: @@ -14,19 +14,20 @@ jobs: runs-on: [self-hosted] strategy: matrix: - environment: - - { name: "develop", compose_file: "docker-compose.dev.yml", project_name_suffix: "_dev" } - - { name: "main", compose_file: "docker-compose.prod.yml", project_name_suffix: "_prod" } include: - - environment: "develop" + - environment: develop + compose_files: "docker-compose.yml -f docker-compose.dev.yml" TELEGRAM_BOT_TOKEN: ${{ secrets.DEV_TELEGRAM_BOT_TOKEN }} - - environment: "main" + - environment: main + compose_files: "docker-compose.yml -f docker-compose.prod.yml" TELEGRAM_BOT_TOKEN: ${{ secrets.PROD_TELEGRAM_BOT_TOKEN }} - if: github.ref == 'refs/heads/${{ matrix.environment.name }}' + env: + TELEGRAM_BOT_TOKEN: ${{ matrix.TELEGRAM_BOT_TOKEN }} steps: - name: Checkout repository uses: actions/checkout@v4 - - name: Build with Docker Compose + + - name: Build container uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST }} @@ -35,21 +36,21 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.REPO_NAME }}${{ matrix.environment.project_name_suffix }} -f ${{ matrix.environment.compose_file }} build --no-cache + docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} build --no-cache deploy: needs: build - runs-on: [ self-hosted ] + runs-on: [self-hosted] strategy: matrix: include: - - environment: dev - compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" - - environment: prod - compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" + - environment: develop + compose_files: "docker-compose.yml -f docker-compose.dev.yml" + - environment: main + compose_files: "docker-compose.yml -f docker-compose.prod.yml" if: github.ref == 'refs/heads/${{ matrix.environment }}' steps: - - name: SSH into server and deploy + - name: Deploy to server uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST }} @@ -58,9 +59,9 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose ${matrix.compose_files} -p ${{ env.REPO_NAME }}_${{ matrix.environment }} up -d - sleep 10 # Wait a bit for services to start - if ! docker compose ${matrix.compose_files} -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ps | grep -q 'Up'; then + docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} up -d + sleep 5 # Allow services to start + if ! docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 fi From aad22e79f0fce38ae65dc268d3caba9ed26bd906 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 18:32:50 +0400 Subject: [PATCH 03/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 001f0d4..45e9c4e 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -17,12 +17,8 @@ jobs: include: - environment: develop compose_files: "docker-compose.yml -f docker-compose.dev.yml" - TELEGRAM_BOT_TOKEN: ${{ secrets.DEV_TELEGRAM_BOT_TOKEN }} - environment: main compose_files: "docker-compose.yml -f docker-compose.prod.yml" - TELEGRAM_BOT_TOKEN: ${{ secrets.PROD_TELEGRAM_BOT_TOKEN }} - env: - TELEGRAM_BOT_TOKEN: ${{ matrix.TELEGRAM_BOT_TOKEN }} steps: - name: Checkout repository uses: actions/checkout@v4 @@ -46,12 +42,17 @@ jobs: include: - environment: develop compose_files: "docker-compose.yml -f docker-compose.dev.yml" + TELEGRAM_BOT_TOKEN: DEV_TELEGRAM_BOT_TOKEN - environment: main compose_files: "docker-compose.yml -f docker-compose.prod.yml" - if: github.ref == 'refs/heads/${{ matrix.environment }}' + TELEGRAM_BOT_TOKEN: PROD_TELEGRAM_BOT_TOKEN steps: - name: Deploy to server uses: appleboy/ssh-action@master + env: + TELEGRAM_BOT_TOKEN: ${{ secrets.${{ matrix.TELEGRAM_BOT_TOKEN }} }} + DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} + SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} From 8a0f8bed96a832f4668609185f622c91c5da0f11 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 18:34:52 +0400 Subject: [PATCH 04/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 45e9c4e..f20af51 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -50,7 +50,7 @@ jobs: - name: Deploy to server uses: appleboy/ssh-action@master env: - TELEGRAM_BOT_TOKEN: ${{ secrets.${{ matrix.TELEGRAM_BOT_TOKEN }} }} + TELEGRAM_BOT_TOKEN: ${{ secrets[matrix.TELEGRAM_BOT_TOKEN] }} DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} with: From 02e70e69b1157c14ede9c8b8e4ffd9cc651d6840 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 19:12:54 +0400 Subject: [PATCH 05/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index f20af51..c4b23f0 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -32,7 +32,7 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} build --no-cache + docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} build --no-cache deploy: needs: build @@ -60,9 +60,9 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} up -d + docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} up -d sleep 5 # Allow services to start - if ! docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${matrix.compose_files} ps | grep -q 'Up'; then + if ! docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 fi From 90a09f4be16fb9cb4d9eef84c43a1eb31e335987 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 20:27:49 +0400 Subject: [PATCH 06/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index c4b23f0..d28bea7 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -8,6 +8,7 @@ on: env: REPO_NAME: ${{ github.event.repository.name }} + COMPOSE_PROJECT_NAME: ${{ github.event.repository.name }} jobs: build: @@ -60,9 +61,9 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} up -d + docker compose -p $(echo "${{ env.REPO_NAME }}" | tr '[:upper:]' '[:lower:]')_${{ matrix.environment }} ${{ matrix.compose_files }} up -d sleep 5 # Allow services to start - if ! docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then + if ! ocker compose -p $(echo "${{ env.REPO_NAME }}" | tr '[:upper:]' '[:lower:]')_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 fi From be16afb431d275a39c7d5910652933a4b443eb82 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 3 Mar 2024 21:32:35 +0400 Subject: [PATCH 07/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index d28bea7..cf43d9e 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -8,7 +8,7 @@ on: env: REPO_NAME: ${{ github.event.repository.name }} - COMPOSE_PROJECT_NAME: ${{ github.event.repository.name }} + COMPOSE_PROJECT_NAME: sh_bot_stat_collector jobs: build: @@ -33,7 +33,7 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} build --no-cache + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} build --no-cache deploy: needs: build @@ -61,9 +61,9 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p $(echo "${{ env.REPO_NAME }}" | tr '[:upper:]' '[:lower:]')_${{ matrix.environment }} ${{ matrix.compose_files }} up -d + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} up -d sleep 5 # Allow services to start - if ! ocker compose -p $(echo "${{ env.REPO_NAME }}" | tr '[:upper:]' '[:lower:]')_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then + if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 fi From 0eb1f87f62437518d9114f90d0d16d894cd08a69 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 17:35:36 +0400 Subject: [PATCH 08/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index cf43d9e..4831eb9 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -42,10 +42,10 @@ jobs: matrix: include: - environment: develop - compose_files: "docker-compose.yml -f docker-compose.dev.yml" + compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" TELEGRAM_BOT_TOKEN: DEV_TELEGRAM_BOT_TOKEN - environment: main - compose_files: "docker-compose.yml -f docker-compose.prod.yml" + compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" TELEGRAM_BOT_TOKEN: PROD_TELEGRAM_BOT_TOKEN steps: - name: Deploy to server From 67a3cde271272b4db443c07a2f2d516fdd1a9871 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 18:00:18 +0400 Subject: [PATCH 09/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 4831eb9..56276ad 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -17,9 +17,9 @@ jobs: matrix: include: - environment: develop - compose_files: "docker-compose.yml -f docker-compose.dev.yml" + compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" - environment: main - compose_files: "docker-compose.yml -f docker-compose.prod.yml" + compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" steps: - name: Checkout repository uses: actions/checkout@v4 From f2c5b5e9059860291b61db69605c20ec68eeb37d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 18:10:01 +0400 Subject: [PATCH 10/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 46 +++++++++++--------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 56276ad..2097e5f 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -11,19 +11,26 @@ env: COMPOSE_PROJECT_NAME: sh_bot_stat_collector jobs: - build: + build-and-deploy: runs-on: [self-hosted] - strategy: - matrix: - include: - - environment: develop - compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" - - environment: main - compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" steps: - name: Checkout repository uses: actions/checkout@v4 + - name: Set environment for develop branch + if: github.ref == 'refs/heads/develop' + run: | + echo "COMPOSE_FILES=-f docker-compose.yml -f docker-compose.dev.yml" >> $GITHUB_ENV + echo "ENVIRONMENT=develop" >> $GITHUB_ENV + echo "TELEGRAM_BOT_TOKEN=${{ secrets.DEV_TELEGRAM_BOT_TOKEN }}" >> $GITHUB_ENV + + - name: Set environment for main branch + if: github.ref == 'refs/heads/main' + run: | + echo "COMPOSE_FILES=-f docker-compose.yml -f docker-compose.prod.yml" >> $GITHUB_ENV + echo "ENVIRONMENT=main" >> $GITHUB_ENV + echo "TELEGRAM_BOT_TOKEN=${{ secrets.PROD_TELEGRAM_BOT_TOKEN }}" >> $GITHUB_ENV + - name: Build container uses: appleboy/ssh-action@master with: @@ -33,27 +40,13 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} build --no-cache + docker compose -p sh_bot_stat_collector_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} build --no-cache - deploy: - needs: build - runs-on: [self-hosted] - strategy: - matrix: - include: - - environment: develop - compose_files: "-f docker-compose.yml -f docker-compose.dev.yml" - TELEGRAM_BOT_TOKEN: DEV_TELEGRAM_BOT_TOKEN - - environment: main - compose_files: "-f docker-compose.yml -f docker-compose.prod.yml" - TELEGRAM_BOT_TOKEN: PROD_TELEGRAM_BOT_TOKEN - steps: - name: Deploy to server uses: appleboy/ssh-action@master env: - TELEGRAM_BOT_TOKEN: ${{ secrets[matrix.TELEGRAM_BOT_TOKEN] }} DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} - SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} + SQLITE_DB_FILE_PATH: ${{ secrets.SQLITE_DB_FILE_PATH }} with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} @@ -61,9 +54,8 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} up -d + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d sleep 5 # Allow services to start - if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} ps | grep -q 'Up'; then + if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 - fi From ad81712daa8b580820e3cc50442e78c3538cb83d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 19:13:29 +0400 Subject: [PATCH 11/27] fix ci cd --- docker-compose.dev.yml | 2 +- docker-compose.prod.yml | 13 +------------ docker-compose.yml | 9 +++++---- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 8908ead..daf2602 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,4 +1,4 @@ -version: '3.7' # Use a more recent version if needed +version: '3.8' services: bot: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 9fd30ac..ee86948 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,20 +1,9 @@ -version: '3.7' # Use a more recent version if needed +version: '3.8' services: bot: - build: . - environment: - SQLITE_DB_FILE_PATH: /database/db.sqlite - TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN} - DEVELOPER_CHAT_ID: ${DEVELOPER_CHAT_ID} - TZ: ${TZ-GMT} # Default to GMT if TZ is not specified volumes: - sqlite_data:/database - entrypoint: ["/bin/sh", "-c"] - command: | - python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; - python src/__main__.py; - restart: always volumes: sqlite_data: diff --git a/docker-compose.yml b/docker-compose.yml index e2c1294..f24d12d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.7' # Use a more recent version if needed +version: '3.8' services: bot: @@ -9,9 +9,10 @@ services: DEVELOPER_CHAT_ID: ${DEVELOPER_CHAT_ID} TZ: ${TZ-GMT} # Default to GMT if TZ is not specified entrypoint: ["/bin/sh", "-c"] - command: | - python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; - python src/__main__.py; + command: + - | + python -m sqlite3 ${SQLITE_DB_FILE_PATH} < src/sql/init.sql; + python src/__main__.py; restart: always # Only define services specific to development or production in their respective override files From 158570edbce649c989120fea4712471c76c0ec43 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 19:29:57 +0400 Subject: [PATCH 12/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 2097e5f..828ae17 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -47,6 +47,7 @@ jobs: env: DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} SQLITE_DB_FILE_PATH: ${{ secrets.SQLITE_DB_FILE_PATH }} + TELEGRAM_BOT_TOKEN: ${{ env.TELEGRAM_BOT_TOKEN }} with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} From 3215db14e04575081f41c446a8457f2db4a8a207 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 19:36:41 +0400 Subject: [PATCH 13/27] fix ci cd --- Dockerfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 663739e..080319c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,9 +37,12 @@ RUN poetry install --no-root ############################################### # Production Image ############################################### +ARG CAIROSVG_VERSION=2.7.1 +ENV CAIROSVG_VERSION=$CAIROSVG_VERSION + FROM base-image as production RUN apt-get update && \ - apt-get install -y python3-cairosvg + apt-get install -y python3-cairosvg=$CAIROSVG_VERSION COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH ARG PYTHONPATH=/service From e52ab66cc53c6c0bebdb3378f74506d4faf3ebcd Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 19:44:10 +0400 Subject: [PATCH 14/27] rm cairosvg from Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 080319c..ac8353d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,8 +41,8 @@ ARG CAIROSVG_VERSION=2.7.1 ENV CAIROSVG_VERSION=$CAIROSVG_VERSION FROM base-image as production -RUN apt-get update && \ - apt-get install -y python3-cairosvg=$CAIROSVG_VERSION +#RUN apt-get update && \ +# apt-get install -y python3-cairosvg=$CAIROSVG_VERSION COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH ARG PYTHONPATH=/service From f7cb17e547a57913564e7c3f91891e2a09bdf8d3 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Mar 2024 19:49:49 +0400 Subject: [PATCH 15/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 828ae17..19d652e 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -60,3 +60,4 @@ jobs: if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q 'Up'; then echo "One or more containers are not running as expected." exit 1 + envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH \ No newline at end of file From 3fd319402ef481a7ff4fb009c3c2e7bf3845ad91 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 17:24:44 +0400 Subject: [PATCH 16/27] fix ci cd --- Dockerfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac8353d..2d2a868 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,14 +37,16 @@ RUN poetry install --no-root ############################################### # Production Image ############################################### -ARG CAIROSVG_VERSION=2.7.1 -ENV CAIROSVG_VERSION=$CAIROSVG_VERSION FROM base-image as production -#RUN apt-get update && \ -# apt-get install -y python3-cairosvg=$CAIROSVG_VERSION +RUN apt-get update \ + && apt-get install --no-install-recommends -y \ + libffi-dev \ + python3-cairosvg + COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH + ARG PYTHONPATH=/service ENV PYTHONPATH=$PYTHONPATH \ POETRY_PROJECT_PATH=$PYTHONPATH/pyproject.toml From da4baa1ed1154cd6682d987006f643333b4b4e90 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 17:40:33 +0400 Subject: [PATCH 17/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 19d652e..d0f08ec 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -55,6 +55,7 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d sleep 5 # Allow services to start if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q 'Up'; then From 2c4a27668dd20854a564c2824b077bb5c10040e4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 19:28:22 +0400 Subject: [PATCH 18/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index d0f08ec..56ccdbe 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -55,10 +55,10 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d - sleep 5 # Allow services to start + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down; + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d; + sleep 5; # Allow services to start if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q 'Up'; then - echo "One or more containers are not running as expected." - exit 1 + echo "One or more containers are not running as expected."; + exit 1; envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH \ No newline at end of file From 2104ef21b044802e99109fe690983f310a306989 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 19:55:04 +0400 Subject: [PATCH 19/27] fix ci cd --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 2d2a868..f640668 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ FROM base-image as production RUN apt-get update \ && apt-get install --no-install-recommends -y \ libffi-dev \ + libcairo2 \ python3-cairosvg COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH From 0c7efa65a6f48f44df3c96f34b484c8fef2d7689 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 21:21:57 +0400 Subject: [PATCH 20/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 40 +++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 56ccdbe..d58041f 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -43,6 +43,7 @@ jobs: docker compose -p sh_bot_stat_collector_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} build --no-cache - name: Deploy to server + depends-on: Build container uses: appleboy/ssh-action@master env: DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} @@ -57,8 +58,39 @@ jobs: cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down; docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d; + envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH + + - name: Docker Compose is up + uses: appleboy/ssh-action@master + env: + DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} + TELEGRAM_BOT_TOKEN: ${{ env.TELEGRAM_BOT_TOKEN }} + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + key: ${{ secrets.PRIVATE_KEY }} + script: | sleep 5; # Allow services to start - if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q 'Up'; then - echo "One or more containers are not running as expected."; - exit 1; - envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH \ No newline at end of file + service_name=$(docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps --services); + for service in $service_name; do + if ! docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} ps | grep -q "$service.* Up"; then + echo "$service not running as expected."; + exit 1; + fi; + done; + envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID + + - name: Notify developer + uses: appleboy/ssh-action@master + env: + DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} + TELEGRAM_BOT_TOKEN: ${{ env.TELEGRAM_BOT_TOKEN }} + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + key: ${{ secrets.PRIVATE_KEY }} + script: | + curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${DEVELOPER_CHAT_ID} -d text="Successfully deployed ${{ github. }} to ${{ env.ENVIRONMENT }} environment" + envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID \ No newline at end of file From 0ec19db7af715a326e32f344bd1ac48142d0ef35 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 21:25:16 +0400 Subject: [PATCH 21/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index d58041f..444b620 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -92,5 +92,5 @@ jobs: password: ${{ secrets.PASSWORD }} key: ${{ secrets.PRIVATE_KEY }} script: | - curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${DEVELOPER_CHAT_ID} -d text="Successfully deployed ${{ github. }} to ${{ env.ENVIRONMENT }} environment" + curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${DEVELOPER_CHAT_ID} -d text="Successfully deployed ${{ github.event.workflow_run.head_commit.message }} to ${{ env.ENVIRONMENT }} environment" envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID \ No newline at end of file From d37150ce8e99b0a9d9de01d89ade683b5c38ecb5 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 21:27:06 +0400 Subject: [PATCH 22/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 444b620..c758b59 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -43,7 +43,6 @@ jobs: docker compose -p sh_bot_stat_collector_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} build --no-cache - name: Deploy to server - depends-on: Build container uses: appleboy/ssh-action@master env: DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} From c8a3d54687168ab46baf4caf08544979011a66a2 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 21:39:52 +0400 Subject: [PATCH 23/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index c758b59..456a502 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -11,7 +11,7 @@ env: COMPOSE_PROJECT_NAME: sh_bot_stat_collector jobs: - build-and-deploy: + build: runs-on: [self-hosted] steps: - name: Checkout repository @@ -42,6 +42,10 @@ jobs: cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} docker compose -p sh_bot_stat_collector_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} build --no-cache + deploy: + needs: build + runs-on: [self-hosted] + steps: - name: Deploy to server uses: appleboy/ssh-action@master env: @@ -59,6 +63,10 @@ jobs: docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d; envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH + deploy-check: + needs: deploy + runs-on: [self-hosted] + steps: - name: Docker Compose is up uses: appleboy/ssh-action@master env: @@ -80,6 +88,10 @@ jobs: done; envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID + notification: + needs: deploy-check + runs-on: [self-hosted] + steps: - name: Notify developer uses: appleboy/ssh-action@master env: From c3023661175afd9c9453de0c62f69274a7fa5b1b Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 22:34:22 +0400 Subject: [PATCH 24/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 456a502..9a67aa2 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -59,8 +59,8 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down; - docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d; +# docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down; + docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d --force-recreate; envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH deploy-check: From 638b101a2db6356f149ed85498635c35161d1bea Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 22:35:27 +0400 Subject: [PATCH 25/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 9a67aa2..4e07508 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -59,7 +59,6 @@ jobs: key: ${{ secrets.PRIVATE_KEY }} script: | cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} -# docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} down; docker compose -p ${{ env.COMPOSE_PROJECT_NAME }}_${{ env.ENVIRONMENT }} ${{ env.COMPOSE_FILES }} up -d --force-recreate; envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH From 60197a979dc56ece473e48bb436969a4d1f644c4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 23:08:21 +0400 Subject: [PATCH 26/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 4e07508..7ea6b7f 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -9,6 +9,9 @@ on: env: REPO_NAME: ${{ github.event.repository.name }} COMPOSE_PROJECT_NAME: sh_bot_stat_collector + ENVIRONMENT: ${{ github.ref == 'refs/heads/develop' && 'dev' || 'prod' }} + COMPOSE_FILES: ${{ github.ref == 'refs/heads/develop' && '-f docker-compose.yml -f docker-compose.dev.yml' || '-f docker-compose.yml -f docker-compose.prod.yml' }} + TELEGRAM_BOT_TOKEN: ${{ github.ref == 'refs/heads/develop' && secrets.DEV_TELEGRAM_BOT_TOKEN || secrets.PROD_TELEGRAM_BOT_TOKEN }} jobs: build: @@ -17,20 +20,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set environment for develop branch - if: github.ref == 'refs/heads/develop' - run: | - echo "COMPOSE_FILES=-f docker-compose.yml -f docker-compose.dev.yml" >> $GITHUB_ENV - echo "ENVIRONMENT=develop" >> $GITHUB_ENV - echo "TELEGRAM_BOT_TOKEN=${{ secrets.DEV_TELEGRAM_BOT_TOKEN }}" >> $GITHUB_ENV - - - name: Set environment for main branch - if: github.ref == 'refs/heads/main' - run: | - echo "COMPOSE_FILES=-f docker-compose.yml -f docker-compose.prod.yml" >> $GITHUB_ENV - echo "ENVIRONMENT=main" >> $GITHUB_ENV - echo "TELEGRAM_BOT_TOKEN=${{ secrets.PROD_TELEGRAM_BOT_TOKEN }}" >> $GITHUB_ENV - - name: Build container uses: appleboy/ssh-action@master with: From ccdeb6cf5d40850826f39c4f28de4e8a726cef1d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Mar 2024 23:19:25 +0400 Subject: [PATCH 27/27] fix ci cd --- .github/workflows/build_and_deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 7ea6b7f..164775d 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -91,5 +91,5 @@ jobs: password: ${{ secrets.PASSWORD }} key: ${{ secrets.PRIVATE_KEY }} script: | - curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${DEVELOPER_CHAT_ID} -d text="Successfully deployed ${{ github.event.workflow_run.head_commit.message }} to ${{ env.ENVIRONMENT }} environment" + curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${DEVELOPER_CHAT_ID} -d text="Successfully deployed SHA: ${{ github.sha }} to ${{ env.ENVIRONMENT }} environment" envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID \ No newline at end of file