Feat: deployment only happen when manually triggered. Only to test ng… #54
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: Deploy nginx configuration to TDT4242 server | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| deploy-nginx: | |
| runs-on: self-hosted | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v3.6.0 | |
| - name: Check nginx installation | |
| run: | | |
| if ! command -v nginx &> /dev/null; then | |
| echo "Nginx is not installed" | |
| exit 1 | |
| fi | |
| if ! systemctl is-active --quiet nginx; then | |
| echo "Nginx service is not running" | |
| exit 1 | |
| fi | |
| - name: Set up environment variables | |
| env: | |
| GROUP_ID: ${{ secrets.GROUP_ID }} | |
| PORT_PREFIX: ${{ secrets.PORT_PREFIX }} | |
| DEV_POSTFIX: ${{ secrets.DEV_POSTFIX }} | |
| STAG_POSTFIX: ${{ secrets.STAG_POSTFIX }} | |
| PROD_POSTFIX: ${{ secrets.PROD_POSTFIX }} | |
| GATEWAY_POSTFIX: ${{ secrets.GATEWAY_POSTFIX }} | |
| run: | | |
| 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: Backup existing configurations | |
| run: | | |
| if [ -f /etc/nginx/conf.d/nginx.conf ]; then | |
| sudo cp /etc/nginx/conf.d/nginx.conf /etc/nginx/conf.d/nginx.conf.backup | |
| echo "Created backup of existing nginx.conf" | |
| fi | |
| if [ -f /etc/nginx/nginx.conf ]; then | |
| sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup | |
| echo "Created backup of existing main nginx.conf" | |
| fi | |
| - name: Generate server nginx configuration | |
| run: | | |
| if [ ! -f nginx.conf ]; then | |
| echo "Server nginx configuration template not found" | |
| exit 1 | |
| fi | |
| envsubst '${DEV_PORT_PREFIX} ${PROD_PORT_PREFIX} ${STAG_PORT_PREFIX} ${GATEWAY_POSTFIX}' < nginx.conf > ./nginx_securefit.conf | |
| - name: Generate Docker nginx configuration | |
| run: | | |
| if [ ! -f nginx/nginx.conf ]; then | |
| echo "Docker nginx configuration template not found" | |
| exit 1 | |
| fi | |
| cp nginx/nginx.conf ./nginx_docker.conf | |
| - name: Validate configurations | |
| run: | | |
| if ! sudo nginx -t -c ./nginx_securefit.conf; then | |
| echo "Server nginx configuration is invalid" | |
| exit 1 | |
| fi | |
| if ! sudo nginx -t -c ./nginx_docker.conf; then | |
| echo "Docker nginx configuration is invalid" | |
| exit 1 | |
| fi | |
| - name: Copy configurations to nginx | |
| run: | | |
| sudo cp ./nginx_securefit.conf /etc/nginx/conf.d/nginx.conf || { | |
| echo "Failed to copy server configuration" | |
| exit 1 | |
| } | |
| sudo cp ./nginx_docker.conf /etc/nginx/conf.d/nginx_docker.conf || { | |
| echo "Failed to copy Docker configuration" | |
| exit 1 | |
| } | |
| - name: Test nginx configuration | |
| run: | | |
| sudo nginx -t || { | |
| echo "Nginx configuration test failed" | |
| exit 1 | |
| } | |
| - name: Reload nginx service | |
| run: | | |
| sudo systemctl reload nginx || { | |
| echo "Failed to reload nginx" | |
| exit 1 | |
| } | |
| - name: Verify nginx is running | |
| run: | | |
| if ! systemctl is-active --quiet nginx; then | |
| echo "Nginx service is not running after reload" | |
| exit 1 | |
| fi | |
| echo "Nginx service is running successfully" |