diff --git a/.env b/.env index bf8f669..c525288 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -GROUP_ID=00 +GROUP_ID=05 DOMAIN=localhost URL_PREFIX=http:// PORT_PREFIX=2 diff --git a/.github/workflows/deploy_dev.yml b/.github/workflows/deploy_dev.yml new file mode 100644 index 0000000..e83cacb --- /dev/null +++ b/.github/workflows/deploy_dev.yml @@ -0,0 +1,45 @@ +name: Deploy to development + +on: + push: + branches: + - main + +jobs: + deploy-dev: + runs-on: self-hosted # Ensure you're using your self-hosted runner + + steps: + - name: Checkout repository + uses: actions/checkout@v3 # Checkout the latest code + + # Optionally, set environment variables (if needed) + - name: Set up environment variables + uses: falti/dotenv-action@v1.1.4 + with: + export-variables: true + keys-case: bypass + + - name: Check environment variables + run: env + + # Stop and remove existing Docker containers + - name: Stop and remove existing containers + run: | + sudo docker compose down || true + + # Pull the latest Docker images (if using remote images) + - name: Pull latest Docker images + run: sudo docker compose pull || true + + # Build Docker images if there are any changes + - name: Build Docker images + run: sudo docker compose build --no-cache + + # Start Docker containers (detached mode) + - name: Start Docker containers + run: sudo docker compose up -d + + # Verify that the containers are running + - name: Verify running Docker containers + run: sudo docker ps -a diff --git a/.github/workflows/deploy_test.yml b/.github/workflows/deploy_test.yml index a14f485..ea79766 100644 --- a/.github/workflows/deploy_test.yml +++ b/.github/workflows/deploy_test.yml @@ -19,10 +19,11 @@ jobs: env: REPO_NAME: ${{ github.event.repository.name }} COMPOSE_FILE: ${{ github.event.inputs.config }} - + steps: - name: Checkout repository uses: actions/checkout@v3 + - name: Dotenv Action uses: falti/dotenv-action@v1.1.4 with: @@ -35,9 +36,9 @@ jobs: docker compose version cd $GITHUB_WORKSPACE docker compose -f $COMPOSE_FILE --verbose config && printf "OK\n" || exit 1 - - - name: Build and run docker image + + - name: Build and run docker image run: docker compose -f $COMPOSE_FILE up --force-recreate --build -d - + - name: Verify that gateway is available run: docker ps diff --git a/.github/workflows/update_primary_nginx.yml b/.github/workflows/update_primary_nginx.yml index 9733e17..55b9ede 100644 --- a/.github/workflows/update_primary_nginx.yml +++ b/.github/workflows/update_primary_nginx.yml @@ -1,31 +1,50 @@ -name: UNFINISHED - setup nginx from the configuration file +name: Deploy nginx configuration to TDT4242 server on: + push: + branches: + - main workflow_dispatch: jobs: - deploy: + deploy-nginx: runs-on: self-hosted - + steps: - name: Checkout repository uses: actions/checkout@v3 - - name: Dotenv Action - uses: falti/dotenv-action@v1.1.4 - with: - export-variables: true - keys-case: bypass - - - name: Check environment variables - run: env - - #Doesn't work properly, envsubst replaces all instances of $variable and ${variable}, but we want to keep e.g., $host as nginx variables - #- name: Create temporary nginx config file - # run: envsubst < ./nginx_template.txt > temp_default.conf - # Maybe look at https://www.baeldung.com/linux/envsubst-command to get an idea - - - name: Copy temporary file to correct place - run: cp temp_default.conf /etc/nginx/conf.d/default.conf - - - name: Show nginx file - run: cat /etc/nginx/conf.d/default.conf + + - name: Export environment variables + run: | + export GROUP_ID="05" + export PORT_PREFIX="2" + export DEV_POSTFIX="1" + export STAG_POSTFIX="2" + export PROD_POSTFIX="3" + export GATEWAY_POSTFIX="6" + + export DEV_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${DEV_POSTFIX}" + export STAG_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${STAG_POSTFIX}" + export PROD_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${PROD_POSTFIX}" + + echo "DEV_PORT_PREFIX=$DEV_PORT_PREFIX" >> $GITHUB_ENV + echo "STAG_PORT_PREFIX=$STAG_PORT_PREFIX" >> $GITHUB_ENV + echo "PROD_PORT_PREFIX=$PROD_PORT_PREFIX" >> $GITHUB_ENV + echo "GATEWAY_POSTFIX=$GATEWAY_POSTFIX" >> $GITHUB_ENV + + - name: Replace environment variables in Nginx config + run: | + envsubst '${DEV_PORT_PREFIX} ${PROD_PORT_PREFIX} ${STAG_PORT_PREFIX} ${GATEWAY_POSTFIX}' < nginx_template.txt > ./nginx_securefit.conf + + - name: Copy variables to conf file + run: | + sudo cp ./nginx_securefit.conf /etc/nginx/conf.d/nginx.conf + + - name: Test setup + run: | + sudo nginx -t + + # Restarting nginx service + - name: Restart nginx service + run: | + sudo systemctl reload nginx diff --git a/.gitignore b/.gitignore index 57dd5eb..48059d3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ backend/secfit/.vscode/ backend/secfit/*/migrations/__pycache__/ backend/secfit/*/__pycache__/ -backend/secfit/db.sqlite3 +backend/secfit/db.sqlite3 \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..158a736 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,31 @@ +server { + listen 80; + + location / { + proxy_pass http://0.0.0.0:23/; + proxy_set_header Host ; + proxy_set_header X-Real-IP ; + proxy_set_header X-Forwarded-For ; + } +} + +server { + listen 217; + location / { + proxy_pass http://0.0.0.0:21/; + proxy_set_header Host ; + proxy_set_header X-Real-IP ; + proxy_set_header X-Forwarded-For ; + } +} + + +server { + listen 227; + location / { + proxy_pass http://0.0.0.0:22/; + proxy_set_header Host ; + proxy_set_header X-Real-IP ; + proxy_set_header X-Forwarded-For ; + } +} diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 6cf6f69..131034d 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -46,4 +46,4 @@ http { proxy_set_header X-Forwarded-Proto $scheme; } } -} +} \ No newline at end of file diff --git a/nginx_securefit.conf b/nginx_securefit.conf new file mode 100644 index 0000000..6ff2deb --- /dev/null +++ b/nginx_securefit.conf @@ -0,0 +1,31 @@ +server { + listen 80; + + location / { + proxy_pass http://0.0.0.0:${PROD_PORT_PREFIX}${GATEWAY_POSTFIX}/; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +} + +server { + listen ${DEV_PORT_PREFIX}7; + location / { + proxy_pass http://0.0.0.0:${DEV_PORT_PREFIX}${GATEWAY_POSTFIX}/; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +} + + +server { + listen ${STAG_PORT_PREFIX}7; + location / { + proxy_pass http://0.0.0.0:${STAG_PORT_PREFIX}${GATEWAY_POSTFIX}/; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +}