feat: add Forgejo deployment workflows and update Nginx configuration with CORS support
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 32s

This commit is contained in:
vickytechkey 2026-10-03 17:31:57 +05:30
parent dc5e79667a
commit 9fc5d3813b
4 changed files with 37 additions and 82 deletions

View file

@ -52,7 +52,7 @@ jobs:
chmod 600 ~/.ssh/id_rsa
# Copy app archive and configuration files
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json crm-backend.service configure_nginx_proxy.py ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json crm-backend.service crmbackend.nginx.conf configure_nginx_proxy.py ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/
# Execute deployment commands on remote server
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }} << 'EOF'

View file

@ -52,7 +52,7 @@ jobs:
chmod 600 ~/.ssh/id_rsa
# Copy app archive and configuration files
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json crm-backend.service configure_nginx_proxy.py ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }}:/tmp/
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json crm-backend.service crmbackend.nginx.conf configure_nginx_proxy.py ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }}:/tmp/
# Execute deployment commands on remote server
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }} << 'EOF'

View file

@ -3,93 +3,36 @@ import sys
import subprocess
def main():
sites_dir = '/etc/nginx/sites-enabled'
if not os.path.exists(sites_dir):
print(f"Nginx sites-enabled directory not found: {sites_dir}")
source_conf = '/tmp/crmbackend.nginx.conf'
target_available = '/etc/nginx/sites-available/crmbackend'
target_enabled = '/etc/nginx/sites-enabled/crmbackend'
if not os.path.exists(source_conf):
source_conf = '/home/ubuntu/crmbackend/crmbackend.nginx.conf'
if not os.path.exists(source_conf):
print(f"Error: Nginx configuration source file not found at {source_conf}")
return
target_file = None
for filename in os.listdir(sites_dir):
filepath = os.path.join(sites_dir, filename)
if os.path.isfile(filepath):
try:
with open(filepath, 'r') as f:
content = f.read()
if '8080' in content:
target_file = filepath
break
except Exception as e:
print(f"Error reading {filepath}: {e}")
if not target_file:
print("Could not find active Nginx configuration proxying to port 8080.")
return
print(f"Found active proxy configuration in {target_file}")
with open(target_file, 'r') as f:
lines = f.readlines()
# Check if /crm proxy is already configured
content = "".join(lines)
if 'location /crm' in content:
print("Proxy path /crm is already configured in Nginx.")
return
# Find the server block and inject location /crm
# We will locate a spot inside a server block (e.g. right before location /)
injected = False
new_lines = []
proxy_block = """
location /crm {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
"""
for line in lines:
if 'location / ' in line or 'location /{' in line or 'location =' in line:
new_lines.append(proxy_block)
injected = True
new_lines.append(line)
if not injected:
# Fallback: append inside the last closing brace
for i in range(len(lines) - 1, -1, -1):
if '}' in lines[i]:
lines.insert(i, proxy_block)
new_lines = lines
injected = True
break
if not injected:
print("Could not find a suitable spot to inject location /crm block.")
return
# Write config to temp file
temp_conf = '/tmp/nginx_crm_test.conf'
with open(temp_conf, 'w') as f:
f.writelines(new_lines)
# Copy to original file location with sudo
try:
subprocess.run(['sudo', 'cp', temp_conf, target_file], check=True)
print("Updated Nginx configuration.")
# Test configuration
# Copy to sites-available
subprocess.run(['sudo', 'cp', source_conf, target_available], check=True)
print(f"Copied {source_conf} to {target_available}")
# Enable site symlink
if not os.path.exists(target_enabled):
subprocess.run(['sudo', 'ln', '-sf', target_available, target_enabled], check=True)
print(f"Symlinked {target_available} to {target_enabled}")
# Test Nginx configuration
test_res = subprocess.run(['sudo', 'nginx', '-t'], capture_output=True, text=True)
if test_res.returncode == 0:
subprocess.run(['sudo', 'systemctl', 'reload', 'nginx'], check=True)
print("Nginx reloaded successfully.")
print("Nginx configuration verified and reloaded successfully.")
else:
print(f"Nginx config test failed. Reverting changes. Output: {test_res.stderr}")
# Revert (assume we have a backup or we just copy back original - we will copy backup)
# For safety, since we just overwrote it, we should verify before reload.
print(f"Nginx config test failed: {test_res.stderr}")
except Exception as e:
print(f"Error updating Nginx config: {e}")
print(f"Error configuring Nginx: {e}")
if __name__ == '__main__':
main()

View file

@ -1,8 +1,20 @@
server {
listen 80;
server_name betaapicsa.tradhox.com apicsa.tradhox.com crmbackend.local;
server_name betaapicsa.tradhox.com apicsa.tradhox.com;
location / {
# Handle CORS preflight requests directly in Nginx
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '$http_origin' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, PATCH' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Content-Type' 'text/plain; charset=utf-8' always;
add_header 'Content-Length' 0 always;
return 204;
}
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;