Skip to content

Commit

Permalink
ci: add Docker Compose and Deploy workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemSBulgakov committed Mar 31, 2024
1 parent 5b5358a commit 3c08d29
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Build and deploy a Docker image

on:
push:
branches:
- main
workflow_dispatch:
inputs:
environment:
description: Environment to deploy to
type: environment
required: true

jobs:
build-and-push-image:
name: Build and push Docker image
runs-on: ubuntu-latest
permissions: # Permissions granted to the 'GITHUB_TOKEN'
contents: read
packages: write
outputs:
imageid: ${{ steps.build.outputs.imageid }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/one-zero-eight/sport-bot

- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.repository_owner == 'one-zero-eight' && 'true' || 'false' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

deploy-staging:
if: github.repository_owner == 'one-zero-eight' && ((github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'staging') || (github.event_name == 'push' && github.ref == 'refs/heads/main'))
name: Deploy to staging server
needs: build-and-push-image
runs-on: self-hosted
environment:
name: staging
url: https://t.me/IUSportStagingBot
concurrency:
group: staging
cancel-in-progress: false
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
fingerprint: ${{ secrets.SSH_FINGERPRINT }}
script_stop: true # Stop script on error
script: |
cd "${{ secrets.DEPLOY_DIRECTORY }}"
bash "${{ secrets.DEPLOY_SCRIPT }}" "${{ needs.build-and-push-image.outputs.imageid }}"
deploy-production:
if: github.repository_owner == 'one-zero-eight' && (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production')
name: Deploy to production server
needs: build-and-push-image
runs-on: self-hosted
environment:
name: production
url: https://t.me/IUSportBot
concurrency:
group: production
cancel-in-progress: false
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
fingerprint: ${{ secrets.SSH_FINGERPRINT }}
script_stop: true # Stop script on error
script: |
cd "${{ secrets.DEPLOY_DIRECTORY }}"
bash "${{ secrets.DEPLOY_SCRIPT }}" "${{ needs.build-and-push-image.outputs.imageid }}"
17 changes: 17 additions & 0 deletions docker-compose.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This is a Docker Compose configuration for production use.
# For building locally use docker-compose.yaml

# Run all services:
# docker compose -f docker-compose.yaml -f docker-compose.prod.yaml up --pull always --detach

version: '3'

# Override the default settings for production
services:
bot:
# Pull the image from GitHub Container Registry instead of building locally
image: 'ghcr.io/one-zero-eight/sport-bot:main'
build: !reset null

db:
ports: !reset [] # Do not expose any ports
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This is a Docker Compose configuration for local use.
# In production use docker-compose.prod.yaml

# Run all services:
# docker compose up --build --detach

version: '3'

services:
bot:
build: # See bot.Dockerfile for more details
context: .
dockerfile: bot.Dockerfile
depends_on:
db:
# Wait for the database to be ready before starting the application
condition: service_healthy
restart: always
env_file: .env # Set bot settings in .env file

db:
# See more: https://hub.docker.com/_/postgres
image: 'postgres:16.2'
restart: always
# The commits were slow on our servers, so we turned off the synchronous_commit
command: postgres -c synchronous_commit=off
volumes:
- 'postgres:/var/lib/postgresql/data'
ports:
- '5432:5432'
env_file: .env # Set POSTGRES_PASSWORD in .env file
healthcheck:
test: [CMD-SHELL, pg_isready -U postgres]
interval: 5s
timeout: 5s
retries: 5

volumes:
postgres:

0 comments on commit 3c08d29

Please sign in to comment.