Skip to content

fix ci cd

fix ci cd #6

name: Build and Deploy Workflow
on:
push:
branches:
- develop
- main
env:
REPO_NAME: ${{ github.event.repository.name }}
COMPOSE_PROJECT_NAME: ${{ github.event.repository.name }}
jobs:
build:
runs-on: [self-hosted]
strategy:
matrix:
include:
- environment: develop
compose_files: "docker-compose.yml -f docker-compose.dev.yml"
- environment: main
compose_files: "docker-compose.yml -f docker-compose.prod.yml"
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 ${{ env.REPO_NAME }}_${{ matrix.environment }} ${{ matrix.compose_files }} build --no-cache
deploy:
needs: build
runs-on: [self-hosted]
strategy:
matrix:
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"
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 }}
password: ${{ secrets.PASSWORD }}
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
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
echo "One or more containers are not running as expected."
exit 1
fi