Backend CD Pipeline #25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Backend CD Pipeline | |
on: | |
push: | |
branches: | |
- deployment | |
paths: | |
- 'backend/**' | |
workflow_dispatch: | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
# Backend Sensitive Variables | |
SECRET_KEY: ${{ secrets.SECRET_KEY }} | |
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }} | |
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }} | |
# PostgreSQL Sensitive Variables | |
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} | |
# Frontend Sensitive Variables | |
VITE_API_URL: ${{ secrets.FRONTEND_ENV }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set backend env file | |
run: | | |
# Backend Insensitive Variables | |
echo "DOMAIN=production" >> backend.env | |
echo "ENVIRONMENT=production" >> backend.env | |
echo "PROJECT_NAME=\"Full Stack FastAPI Project\"" >> backend.env | |
echo "STACK_NAME=full-stack-fastapi-project" >> backend.env | |
echo "BACKEND_CORS_ORIGINS=\"http://localhost,http://localhost:5173,https://localhost,https://localhost:5173\"" >> backend.env | |
echo "SMTP_TLS=true" >> backend.env | |
echo "SMTP_SSL=false" >> backend.env | |
echo "SMTP_PORT=587" >> backend.env | |
echo "[email protected]" >> backend.env | |
echo "POSTGRES_SERVER=db" >> backend.env | |
echo "POSTGRES_USER=app" >> backend.env | |
echo "POSTGRES_PORT=5432" >> backend.env | |
echo "POSTGRES_DB=app" >> backend.env | |
echo "USERS_OPEN_REGISTRATION=True" >> backend.env | |
# Sensitive Variables | |
echo "SECRET_KEY=${SECRET_KEY}" >> backend.env | |
echo "FIRST_SUPERUSER=${FIRST_SUPERUSER}" >> backend.env | |
echo "FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD}" >> backend.env | |
echo "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" >> backend.env | |
- name: Prepare Frontend and PostgreSQL Env | |
run: | | |
# Frontend env file | |
echo "VITE_API_URL=${VITE_API_URL}" > frontend.env | |
# PostgreSQL env file | |
echo "POSTGRES_USER=app" >> postgres.env | |
echo "POSTGRES_DB=app" >> postgres.env | |
echo "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" >> postgres.env | |
- name: Get terraform-apply.yml Run ID | |
id: get-run-id | |
run: | | |
RUN_ID=$(curl -s \ | |
-H "Authorization: Bearer ${{ secrets.TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/terraform-apply.yml/runs?branch=infra_main&per_page=1" \ | |
| jq -r '.workflow_runs[0].id') | |
echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT | |
echo "$RUN_ID" | |
- name: Download Public_IP File | |
uses: actions/download-artifact@v4 | |
with: | |
name: Public_IP | |
github-token: ${{ secrets.TOKEN }} | |
run-id: ${{ steps.get-run-id.outputs.run_id }} | |
- name: Read public IP | |
id: read_ip | |
run: | | |
PUBLIC_IP=$(grep -oP '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b' public_ip_env.txt | head -n 1) | |
echo "PUBLIC_IP=$PUBLIC_IP" >> $GITHUB_ENV | |
- name: Copy files to Server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ env.PUBLIC_IP }} | |
username: ${{ vars.EC2_USER }} | |
key: ${{ secrets.PRIVATE_KEY }} | |
source: "backend.env, frontend.env, postgres.env, compose.yml" | |
target: "~/" | |
overwrite: true | |
- name: Use SSH Action | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ env.PUBLIC_IP }} | |
username: ${{ vars.EC2_USER }} | |
key: ${{ secrets.PRIVATE_KEY }} | |
script: | | |
mv backend.env backend/.env | |
mv frontend.env frontend/.env | |
mv postgres.env .env | |
docker compose down backend db adminer | |
docker compose up -d --no-deps --force-recreate backend db adminer | |
rm frontend/.env backend/.env .env |