A production-ready template for telegram bots using Aiogram 3.x and docker compose for auto deployment by GitHub Actions. By default, this is echo bot with /help
command to get user's data from database. Also you need to create self-hosted runner for GitHub Actions, there your bot will be deployed. For that you will need your own server.
- Built with Aiogram 3.x
- PostgreSQL database integration
- Docker compose containerization
- Multiple deployment modes
- GitHub Actions CI/CD
- Environment variables configuration
The template supports three deployment modes:
-
Default Mode (
DEPLOY_MODE="default"
)- Uses an existing PostgreSQL database
- Suitable for production with managed databases
-
Local Mode (
DEPLOY_MODE="local"
)- Creates a new PostgreSQL container
- Perfect for fast deploy and testing
- Includes database persistence
-
Development Mode (
DEPLOY_MODE="dev"
)- Similar to local mode but optimized for development
- Includes hot-reload capabilities
- Best for active development
-
Create new repository from existing template
-
Setup local hosted runner for GitHub Actions:
- Goto https://github.com/your_username/your_repo/settings/actions/runners/new?arch=x64&os=linux (change
your_username
on your github username andyour_repo
on repo_name, created from this template) - Create new local runner on your server
- Instead of running
./run.sh
file install github runner as service, here is code are:sudo ./svc.sh install sudo ./svc.sh start
- Download docker and docker compose if they are not installed,:
sudo curl -fsSL https://get.docker.com | sh sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
- Goto https://github.com/your_username/your_repo/settings/actions/runners/new?arch=x64&os=linux (change
-
Create environment for your bot:
- Goto https://github.com/your_username/your_repo/settings/environments/new (change
your_username
on your github username andyour_repo
on repo_name, created from this template) - In name write
production
and continue - In environment secrets write:
TELEGRAM_BOT_TOKEN
: Your bot token, get it from @BotFatherPOSTGRES_HOST
: Postgres host, if using local profile set heredb
POSTGRES_PORT
: 5432 by default, but if you already have postgres container you can set it to5433
, or usedefault
deploy_modePOSTGRES_USER
: User for postgres databasePOSTGRES_PASSWORD
: Password for postgres databasePOSTGRES_DB
: Database nameDEPLOY_MODE
:default
,local
ordev
- All variables are required!
- Goto https://github.com/your_username/your_repo/settings/environments/new (change
-
Deploy your bot:
- Goto https://github.com/your_username/your_repo/actions/workflows/deploy.yml (change
your_username
on your github username andyour_repo
on repo_name, created from this template) - Press Run workflow and start your bot! This workflow will be triggered any time you push any code to
main
.
- Goto https://github.com/your_username/your_repo/actions/workflows/deploy.yml (change
- Create a new secret in
production
environment, in our case this will beVARIABLE_NAME
. - Change
deploy.yml
file, addecho "VARIABLE_NAME=\"${{ secrets.VARIABLE_NAME }}\"" >> .env
line - After you can get your variable value in python using
os.getenv("VARIABLE_NAME")