Skip to content

Commit

Permalink
modified workflows to install nginx not as container
Browse files Browse the repository at this point in the history
  • Loading branch information
leoBitto committed Apr 11, 2024
1 parent 77bbdfa commit 2ff86d0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 116 deletions.
41 changes: 18 additions & 23 deletions .github/workflows/CI-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ env:
jobs:
build_and_push:
runs-on: ubuntu-latest

steps:

- name: Checkout the repo
uses: actions/checkout@v3
with:
Expand All @@ -38,14 +40,9 @@ jobs:

- name: Display content of .env file
run: cat ./config/.env

- name: Update Nginx configuration file
run: |
sed -i "s/server_name localhost/server_name ${{ secrets.DOMAIN }}/g" ./nginx/nginx.conf
working-directory: .

- name: Build Docker image using Docker Compose
run: docker-compose -f docker-compose.dev.yml build
run: docker-compose -f docker-compose.prod.yml build

- name: verify that all the container have been built
run: docker images
Expand Down Expand Up @@ -84,11 +81,13 @@ jobs:
submodules: recursive
token: ${{ secrets.GITHUB_TOKEN }}


- name: Set ENV variables
run: |
echo "REPO_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/}" >> $GITHUB_ENV
echo $GITHUB_ENV

- name: Create project directory and conf file
uses: appleboy/[email protected]
with:
Expand All @@ -113,6 +112,7 @@ jobs:
echo "DOMAIN=${{ secrets.DOMAIN }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
cat "/opt/${{ env.REPO_NAME }}/config/.env"
- name: SCP Docker Compose file and Nginx conf
uses: appleboy/scp-action@master
with:
Expand All @@ -122,23 +122,6 @@ jobs:
source: "./docker-compose.prod.yml"
target: "/opt/${{ env.REPO_NAME }}/"

- name: SCP Nginx conf file
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
source: "./nginx/nginx.prod.conf"
target: "/opt/${{ env.REPO_NAME }}/"

- name: Update Nginx configuration file
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sed -i "s/yourdomain.com/${{ secrets.DOMAIN }}/g" /opt/${{ env.REPO_NAME }}/nginx/nginx.prod.conf

- name: Upgrade server and install Docker
uses: appleboy/[email protected]
Expand All @@ -164,7 +147,9 @@ jobs:
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose docker-compose-plugin -y
sudo apt upgrade -y
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
Expand Down Expand Up @@ -192,4 +177,14 @@ jobs:
docker exec web python manage.py migrate
docker exec web python manage.py collectstatic --noinput --clear
- name: Restart Nginx Service
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sudo systemctl restart nginx
16 changes: 4 additions & 12 deletions .github/workflows/server_setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ name: Setup Server

on:
workflow_dispatch: # Trigger manual workflow run
inputs:
superuser-name:
description: 'Superuser username'
required: true
superuser-email:
description: 'Superuser email'
required: true
superuser-password:
description: 'Superuser password'
required: true


jobs:
setup-server:
Expand Down Expand Up @@ -66,7 +57,7 @@ jobs:
script: |
mkdir -p "/opt/${{ env.REPO_NAME }}"
mkdir -p "/opt/${{ env.REPO_NAME }}/config"
rm /opt/${{ env.REPO_NAME }}/config/.env
echo "DEBUG=False" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SECRET_KEY=foo" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] *" >> "/opt/${{ env.REPO_NAME }}/config/.env"
Expand All @@ -79,6 +70,7 @@ jobs:
echo "DATABASE=postgres" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "EMAIL=${{ secrets.EMAIL }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DOMAIN=${{ secrets.DOMAIN }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
cat "/opt/${{ env.REPO_NAME }}/config/.env"
- name: SCP Docker Compose file and Nginx conf
Expand Down Expand Up @@ -108,7 +100,7 @@ jobs:
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sed -i "s/yourdomain.com/${{ secrets.DOMAIN }}/g" /etc/nginx/sites-available/${{ env.REPO_NAME }}/nginx.conf
sed -i "s/localhost/${{ secrets.DOMAIN }}/g" /etc/nginx/sites-available/${{ env.REPO_NAME }}/nginx.conf
ln -s /etc/nginx/sites-available/${{ env.REPO_NAME }} /etc/nginx/sites-enabled
nginx -t
systemctl restart nginx
Expand Down
1 change: 0 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ services:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro # metti il file di conf di nginx sul suo container e impostalo in read only
ports:
- 80:80
#- 443:443
restart: always
depends_on:
- web
Expand Down
30 changes: 0 additions & 30 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,8 @@ services:
env_file:
- ./config/.env

nginx:
image: nginx:latest
container_name: nginx_reverse_proxy
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
- ./nginx/nginx.prod.conf:/etc/nginx/nginx.conf:ro
- certbot_volume:/etc/letsencrypt
ports:
- 80:80
- 443:443
restart: always
depends_on:
- web


certbot:
image: certbot/certbot
container_name: certbot
volumes:
- certbot_volume:/etc/letsencrypt
- ./config:/etc/letsencrypt/config
- certbot_webroot:/var/www/certbot
env_file:
- ./config/.env
entrypoint: /bin/sh -c "trap exit TERM; while :; do certbot certonly --webroot -w /var/www/certbot --email [email protected] --agree-tos --no-eff-email --domains h1v3.xyz; sleep 12h & wait $${!}; done;"



volumes:
postgres_data:
static_volume:
media_volume:
certbot_volume:
certbot_webroot:
7 changes: 4 additions & 3 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

events { worker_connections 1024; }
events {
worker_connections 1024;
}

http{

Expand All @@ -10,7 +11,7 @@ http{
server {

listen 80;
server_name yourdomain.com www.yourdomain.com;
server_name localhost;

location / {
proxy_pass http://base;
Expand Down
47 changes: 0 additions & 47 deletions nginx/nginx.prod.conf

This file was deleted.

0 comments on commit 2ff86d0

Please sign in to comment.