From 772728d48d16be3cc970751dc8cb45e085bc9e03 Mon Sep 17 00:00:00 2001 From: Destiny Saturday Date: Mon, 12 Aug 2024 18:16:26 +0100 Subject: [PATCH] Update and rename main.yml to prod-deployment.yml --- .github/workflows/main.yml | 117 -------------------------- .github/workflows/prod-deployment.yml | 47 +++++++++++ 2 files changed, 47 insertions(+), 117 deletions(-) delete mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/prod-deployment.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index fde213c0..00000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,117 +0,0 @@ -name: CI/CD--Main - -on: - pull_request: - branches: - - main - push: - branches: - - main - -env: - DB_USERNAME: ${{ secrets.DB_USERNAME }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - DB_DATABASE: ${{ secrets.DB_DATABASE }} - DB_HOST: ${{ secrets.DB_HOST }} - DB_PORT: ${{ secrets.DB_PORT }} - DB_ENTITIES: ${{ secrets.DB_ENTITIES }} - DB_MIGRATIONS: ${{ secrets.DB_MIGRATIONS }} - GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} - GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} - GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} - REDIS_HOST: ${{ secrets.REDIS_HOST }} - DB_TYPE: 'postgres' - PROFILE: 'staging' - NODE_ENV: 'development' - PORT: 3000 - REDIS_PORT: 6379 - -jobs: - test-and-build-main: - runs-on: ubuntu-latest - if: github.event_name == 'pull_request' - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Node.js - uses: actions/setup-node@v3 - with: - node-version: '18' - - - name: Install dependencies - run: npm install --include=dev - - - name: Build project - run: npm run build - - - name: Run tests - run: npm run test - - - name: Generate migrations - run: npm run migration:generate - - - name: Run migrations - run: npm run migration:run - - - name: Start application - run: | - npm run start:prod > app.log 2>&1 & - APP_PID=$! - echo $APP_PID - echo "Application started with PID $APP_PID" - sleep 30 # Wait for the application to start - tail -f app.log & - # Check application status with curl - if curl --retry 5 --retry-delay 5 --max-time 10 http://localhost:3000/health; then - echo "Application is up and running." - else - echo "Application failed to start. Logs:" - cat app.log - echo "Exiting workflow due to application failure." - kill $APP_PID - exit 1 - fi - - kill $APP_PID - echo "Application terminated Successfully." - - - name: Revert Migrations - run: npm run migration:revert - if: always() - - deploy-main: - runs-on: ubuntu-latest - if: github.event_name == 'push' - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Node.js - uses: actions/setup-node@v3 - with: - node-version: '18' - - - name: Install dependencies - run: npm install --include=dev - - - name: Build project - run: npm run build - - - name: Run tests - run: npm run test - - - name: Deploying to virtual machine - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.SERVER_HOST }} - username: ${{ secrets.SERVER_USER }} - # key: ${{ secrets.SERVER_PRIVATE_KEY }} - password: ${{ secrets.SERVER_PASSWORD }} - port: ${{ secrets.SERVER_PORT }} - script: | - echo "hello" - export PATH=$PATH:/home/teamalpha/.nvm/versions/node/v20.15.1/bin - bash ~/main-deployment.sh diff --git a/.github/workflows/prod-deployment.yml b/.github/workflows/prod-deployment.yml new file mode 100644 index 00000000..8278616e --- /dev/null +++ b/.github/workflows/prod-deployment.yml @@ -0,0 +1,47 @@ +name: Prod Deployment + +on: + workflow_dispatch: + push: + branches: + - main + +jobs: + build-and-push: + if: github.event.repository.fork == false + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build Docker image + run: docker build -t nestjs_prod:green . + + - name: Save and compress Docker image + run: | + docker save nestjs_prod:green | gzip > nestjs_prod.tar.gz + + - name: Copy image to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "nestjs_prod.tar.gz" + target: "/tmp" + + deploy: + needs: build-and-push + runs-on: ubuntu-latest + environment: + name: "dev" + url: ${{ vars.URL }} + steps: + - name: Deploy on server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | + ./deploy.sh prod