Skip to content

Деплой на сервер #11

Деплой на сервер

Деплой на сервер #11

Workflow file for this run

name: Build and deploy to stage server
on:
pull_request:
branches:
- develop
# types: [ closed ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: b2b_marketplace_backend
DEPLOY_PATH: /b2b_marketplace
defaults:
run:
working-directory: .
jobs:
buld_and_push_image_to_github_packages:
name: Build and push image to GitHub Packages
# if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
environment:
name: Stage
steps:
-
name: Check out the repo
uses: actions/checkout@v3
-
name: Login to GutHub Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Set REP_OWNER variable
run: |
echo REP_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
-
name: Build and push image
uses: docker/build-push-action@v4
with:
context: .
file: infra/Dockerfile.stage
labels: runnumber=${GITHUB_RUN_ID}
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:stage,
${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:latest,
${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
deploy:
name: Deploy
# if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
needs: buld_and_push_image_to_github_packages
environment:
name: Stage
steps:
-
name: Check out the repo
uses: actions/checkout@v3
-
name: Copy docker-compose and nginx files to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
port: ${{ secrets.SSH_PORT }}
source: "infra/docker-compose.stage.yaml,infra/nginx/nginx.stage.conf"
target: ${{ env.DEPLOY_PATH }}
-
name: Execute remote commands to deploy and start app
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
port: ${{ secrets.SSH_PORT }}
script: |
cd ${{ env.DEPLOY_PATH }}
rm -f .env
touch .env
echo DJANGO_SECRET_KEY=${{ secrets.DJANGO_SECRET_KEY }} >> .env
echo DJANGO_ALLOWED_HOSTS=${{ secrets.DJANGO_ALLOWED_HOSTS }} >> .env
echo CSRF_TRUSTED_ORIGINS=${{ secrets.CSRF_TRUSTED_ORIGINS }} >> .env
echo DJANGO_DEBUG=${{ secrets.DJANGO_DEBUG }} >> .env
echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
echo POSTGRES_HOST=${{ secrets.POSTGRES_HOST }} >> .env
echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT }} >> .env
sudo docker image prune -f
sudo docker pull ${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:latest
sudo docker-compose --file infra/docker-compose.stage.yaml stop
sudo docker-compose --file infra/docker-compose.stage.yaml rm backend
sudo docker-compose --file infra/docker-compose.stage.yaml up -d