Backend CD Pipeline #27
This file contains hidden or 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 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set backend env file | |
run: | | |
# Backend Insensitive Variables | |
echo "DOMAIN=localhost" >> backend.env | |
echo "ENVIRONMENT=local" >> 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 "EMAILS_FROM_EMAIL=info@example.com" >> 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=${{ secrets.SECRET_KEY }}" >> backend.env | |
echo "FIRST_SUPERUSER=${{ secrets.FIRST_SUPERUSER }}" >> backend.env | |
echo "FIRST_SUPERUSER_PASSWORD=${{ secrets.FIRST_SUPERUSER_PASSWORD }}" >> backend.env | |
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> backend.env | |
# echo "SMTP_HOST=" >> backend.env | |
# echo "SMTP_USER=" >> backend.env | |
# echo "SMTP_PASSWORD=" >> backend.env | |
- name: Prepare Frontend and PostgreSQL Env | |
run: | | |
# PostgreSQL env file | |
echo "POSTGRES_USER=app" >> postgres.env | |
echo "POSTGRES_DB=app" >> postgres.env | |
echo "POSTGRES_PASSWORD=${{ secrets.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/scp-action@v0.1.7 | |
with: | |
host: ${{ env.PUBLIC_IP }} | |
username: ${{ vars.EC2_USER }} | |
key: ${{ secrets.PRIVATE_KEY }} | |
source: "backend.env, postgres.env, compose.yml" | |
target: "~/" | |
overwrite: true | |
- name: Use SSH Action | |
uses: appleboy/ssh-action@v1.2.0 | |
with: | |
host: ${{ env.PUBLIC_IP }} | |
username: ${{ vars.EC2_USER }} | |
key: ${{ secrets.PRIVATE_KEY }} | |
script: | | |
mv backend.env backend/.env | |
mv postgres.env .env && touch frontend/.env | |
docker compose down backend db adminer | |
docker compose up -d --no-deps --force-recreate backend db adminer | |
# rm frontend/.env backend/.env .env |