Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed CI CD #16

Merged
merged 27 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Build and Deploy Workflow

on:
push:
branches:
- develop
- main

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:
runs-on: [self-hosted]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Build container
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 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:
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 }}
password: ${{ secrets.PASSWORD }}
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 }} up -d --force-recreate;
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:
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
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

notification:
needs: deploy-check
runs-on: [self-hosted]
steps:
- 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 SHA: ${{ github.sha }} to ${{ env.ENVIRONMENT }} environment"
envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID
47 changes: 0 additions & 47 deletions .github/workflows/deploy.yml

This file was deleted.

47 changes: 0 additions & 47 deletions .github/workflows/develop.yml

This file was deleted.

10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ RUN poetry install --no-root
###############################################
# Production Image
###############################################

FROM base-image as production
RUN apt-get update && \
apt-get install -y python3-cairosvg
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
libffi-dev \
libcairo2 \
python3-cairosvg

COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH


ARG PYTHONPATH=/service
ENV PYTHONPATH=$PYTHONPATH \
POETRY_PROJECT_PATH=$PYTHONPATH/pyproject.toml
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.8'

services:
bot:
volumes:
- sqlite_data_dev:/database
volumes:
sqlite_data_dev:
11 changes: 11 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
bot:
volumes:
- sqlite_data:/database

volumes:
sqlite_data:

# Only define services specific to development or production in their respective override files
36 changes: 0 additions & 36 deletions docker-compose.yaml

This file was deleted.

18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
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