Skip to content

fix ci cd

fix ci cd #11

name: Build and Deploy Workflow
on:
push:
branches:
- develop
- main
env:
REPO_NAME: ${{ github.event.repository.name }}
COMPOSE_PROJECT_NAME: sh_bot_stat_collector
jobs:
build-and-deploy:
runs-on: [self-hosted]
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:
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
- 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 }}
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
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