From 09f677057a21b42996c8a8543584faec7c96888e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauritz=20Skog=C3=B8y?= Date: Thu, 27 Mar 2025 16:08:10 +0100 Subject: [PATCH] Feat: correct update of workflow files (hopefully) --- .github/workflows/deploy_dev.yml | 34 ++++++++-- .github/workflows/deploy_test.yml | 44 ------------- .github/workflows/update_primary_nginx.yml | 77 +++++++++++++++++----- 3 files changed, 88 insertions(+), 67 deletions(-) delete mode 100644 .github/workflows/deploy_test.yml diff --git a/.github/workflows/deploy_dev.yml b/.github/workflows/deploy_dev.yml index 75923fb..5e7f60a 100644 --- a/.github/workflows/deploy_dev.yml +++ b/.github/workflows/deploy_dev.yml @@ -5,37 +5,59 @@ on: push: branches: - main + pull_request: + branches: + - main jobs: deploy-dev: - runs-on: self-hosted # Ensure you're using your self-hosted runner + runs-on: self-hosted steps: - name: Checkout repository - uses: actions/checkout@v3 # Checkout the latest code + uses: actions/checkout@v3.6.0 + + - name: Check Docker installation + run: | + if ! command -v docker &> /dev/null; then + echo "Docker is not installed" + exit 1 + fi + if ! docker info &> /dev/null; then + echo "Docker daemon is not running" + exit 1 + fi - # 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 + - name: Check environment variables (should not really be here as you might expose secrets ) run: env # Remove existing docker containers - name: Stop and remove existing containers run: | docker compose down || true + docker system prune -f || true # Pull the latest Docker images - name: Pull latest Docker images - run: docker compose pull || true + run: | + docker compose pull || { + echo "Failed to pull Docker images" + exit 1 + } # Build Docker images if there are any changes - name: Build Docker images - run: docker compose build --no-cache + run: | + docker compose build --no-cache || { + echo "Failed to build Docker images" + exit 1 + } # Start Docker containers (detached mode) - name: Start Docker containers diff --git a/.github/workflows/deploy_test.yml b/.github/workflows/deploy_test.yml deleted file mode 100644 index ea79766..0000000 --- a/.github/workflows/deploy_test.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Deploy to TDT4242 server development - -on: - workflow_dispatch: - inputs: - config: - description: "Which compose config file to deploy" - required: true - default: "docker-compose.dev.yml" - type: choice - options: - - "docker-compose.dev.yml" - - "docker-compose.stag.yml" - - "docker-compose.yml" - -jobs: - deploy: - runs-on: self-hosted - 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: - export-variables: true - keys-case: bypass - - - name: Check status of config file - run: | - env - docker compose version - cd $GITHUB_WORKSPACE - docker compose -f $COMPOSE_FILE --verbose config && printf "OK\n" || exit 1 - - - 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 cd23a4e..71dddd5 100644 --- a/.github/workflows/update_primary_nginx.yml +++ b/.github/workflows/update_primary_nginx.yml @@ -12,19 +12,28 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v3.6.0 - # This is bad practise as environment variables should be stored in a secure location, but hey it works now. - # Can be fixed through utilizing predefined custom workflow, or github runners-secrets - - name: Export environment variables + - name: Check nginx installation 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" + 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}" @@ -34,19 +43,53 @@ jobs: echo "PROD_PORT_PREFIX=$PROD_PORT_PREFIX" >> $GITHUB_ENV echo "GATEWAY_POSTFIX=$GATEWAY_POSTFIX" >> $GITHUB_ENV + - name: Backup existing configuration + 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 configuration" + fi + - name: Replace environment variables in Nginx config run: | + if [ ! -f nginx_template.txt ]; then + echo "Nginx template file not found" + exit 1 + fi envsubst '${DEV_PORT_PREFIX} ${PROD_PORT_PREFIX} ${STAG_PORT_PREFIX} ${GATEWAY_POSTFIX}' < nginx_template.txt > ./nginx_securefit.conf - - name: Copy variables to conf file + - name: Validate new configuration + run: | + if ! sudo nginx -t -c ./nginx_securefit.conf; then + echo "New nginx configuration is invalid" + exit 1 + fi + + - name: Copy configuration to nginx + run: | + sudo cp ./nginx_securefit.conf /etc/nginx/conf.d/nginx.conf || { + echo "Failed to copy new configuration" + exit 1 + } + + - name: Test nginx configuration run: | - sudo cp ./nginx_securefit.conf /etc/nginx/conf.d/nginx.conf + sudo nginx -t || { + echo "Nginx configuration test failed" + exit 1 + } - - name: Test setup + - name: Reload nginx service run: | - sudo nginx -t + sudo systemctl reload nginx || { + echo "Failed to reload nginx" + exit 1 + } - # Restarting nginx service - - name: Restart nginx service + - name: Verify nginx is running run: | - sudo systemctl reload nginx + if ! systemctl is-active --quiet nginx; then + echo "Nginx service is not running after reload" + exit 1 + fi + echo "Nginx service is running successfully"