diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 25e73286..f7a94bac 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -7,7 +7,7 @@ on: jobs: deploy: - runs-on: bp-runner + runs-on: bp_runner defaults: run: working-directory: /var/www/aihomework/boilerplate/dev diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index df8f28ae..cc25ee19 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -1,74 +1,78 @@ -name: Deploy to Prod +name: Deploy to production on: - workflow_run: - workflows: [CI] - types: - - completed - branches: [prod] + push: + branches: + - prod jobs: - on-success: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} + deploy: + runs-on: bp_runner + defaults: + run: + working-directory: /var/www/aihomework/boilerplate/prod + steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Install sshpass - run: sudo apt-get install sshpass + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 - - name: Fetch .env file from server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/prod-deployment/hng_boilerplate_expressjs/.env .env - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '20' - - name: Build Docker images + - name: Remove old actions remote URL + continue-on-error: true run: | - docker compose --env-file .env -f docker-compose.production.yml build + git remote rm action - - name: List Docker images - run: docker images + - name: Stash or remove local changes + run: | + if git diff --quiet; then + echo "No local changes to stash." + else + echo "Stashing local changes..." + git stash --include-untracked || echo "Failed to stash changes. Attempting to reset..." + git reset --hard || exit 1 + fi - - name: Save Docker images to tarball + - name: Pull from GitHub + id: pull run: | - docker save hng_boilerplate_expressjs-backend_prod:latest | gzip > prod-images.tar.gz + remote_repo="https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git" + git remote add action $remote_repo + git pull $remote_repo prod - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - name: prod-images - path: prod-images.tar.gz + - name: Install dependencies + run: yarn install --frozen-lockfile - - name: Copy Docker images to server - uses: appleboy/scp-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - source: "prod-images.tar.gz" - target: "~/images-tar" + - name: Run tests + run: yarn test - - name: Deploy to server - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - cd ~/prod-deployment/hng_boilerplate_expressjs - git stash - git checkout prod - git pull - docker load -i ~/images-tar/prod-images.tar.gz - docker compose -f docker-compose.production.yml down - docker compose -f docker-compose.production.yml up -d + - name: Build the application + run: yarn build && sudo rm -rf build - on-failure: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'failure' }} - steps: - - run: echo "CI Workflow failed. Prod deployment was not triggered." \ No newline at end of file + # - name: Generate migrations + # run: yarn migration:generate + + # - name: Run migrations + # run: yarn migration:run + + - name: Setup and restart service + run: | + sudo cp server-script/aihomeworkprod.service /etc/systemd/system + sudo systemctl daemon-reload + sudo systemctl restart aihomeworkprod.service + + - name: Verify deployment + run: | + echo "Waiting for service to start..." + sleep 10 + if sudo systemctl is-active --quiet aihomeworkprod.service; then + echo "Deployment successful!" + else + echo "Deployment failed!" + exit 1 + fi diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 1333d15a..92c611b2 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,58 +1,78 @@ name: Deploy to staging on: - workflow_run: - workflows: [CI] - types: - - completed - branches: [staging] + push: + branches: + - staging jobs: deploy: - runs-on: ubuntu-latest + runs-on: bp_runner + defaults: + run: + working-directory: /var/www/aihomework/boilerplate/staging + steps: - - name: Checkout repository - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '20' - - name: Fetch .env file from server + - name: Remove old actions remote URL + continue-on-error: true run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/staging-deployment/hng_boilerplate_expressjs/.env .env - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + git remote rm action - - name: Build Docker images + - name: Stash or remove local changes run: | - docker compose --env-file .env -f docker-compose.staging.yml build + if git diff --quiet; then + echo "No local changes to stash." + else + echo "Stashing local changes..." + git stash --include-untracked || echo "Failed to stash changes. Attempting to reset..." + git reset --hard || exit 1 + fi + + - name: Pull from GitHub + id: pull + run: | + remote_repo="https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git" + git remote add action $remote_repo + git pull $remote_repo staging + + - name: Install dependencies + run: yarn install --frozen-lockfile - - name: List Docker images - run: docker images + - name: Run tests + run: yarn test - - name: Save Docker images to tarball + - name: Build the application + run: yarn build && sudo rm -rf build + + # - name: Generate migrations + # run: yarn migration:generate + + # - name: Run migrations + # run: yarn migration:run + + - name: Setup and restart service run: | - docker save hng_boilerplate_expressjs-backend_staging:latest | gzip > staging-images.tar.gz + sudo cp server-script/aihomeworkstaging.service /etc/systemd/system + sudo systemctl daemon-reload + sudo systemctl restart aihomeworkstaging.service - - name: Copy Docker images to server - uses: appleboy/scp-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - source: "staging-images.tar.gz" - target: "~/images-tar" - - - name: Deploy to server - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - cd ~/staging-deployment/hng_boilerplate_expressjs - git stash - git checkout staging - git pull - docker load -i ~/images-tar/staging-images.tar.gz - docker compose -f docker-compose.staging.yml down - docker compose -f docker-compose.staging.yml up -d + - name: Verify deployment + run: | + echo "Waiting for service to start..." + sleep 10 + if sudo systemctl is-active --quiet aihomeworkstaging.service; then + echo "Deployment successful!" + else + echo "Deployment failed!" + exit 1 + fi diff --git a/server-script/aihomeworkprod.service b/server-script/aihomeworkprod.service index fe26ab18..b4fad082 100644 --- a/server-script/aihomeworkprod.service +++ b/server-script/aihomeworkprod.service @@ -3,11 +3,11 @@ Description=AIHomework-Prod After=network.target [Service] -WorkingDirectory=/var/www/aihomework/prod -ExecStart=/bin/bash /var/www/aihomework/prod/server-script/startappprod.sh +WorkingDirectory=/var/www/aihomework/boilerplate/prod +ExecStart=/bin/bash /var/www/aihomework/boilerplate/prod/server-script/startappprod.sh #Restart=on-failure #RestartSec=20s StartLimitInterval=0 [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/server-script/aihomeworkstaging.service b/server-script/aihomeworkstaging.service index 831038c2..a4d624df 100644 --- a/server-script/aihomeworkstaging.service +++ b/server-script/aihomeworkstaging.service @@ -3,8 +3,8 @@ Description=AIHomework-Dev After=network.target [Service] -WorkingDirectory=/var/www/aihomework/staging -ExecStart=/bin/bash /var/www/aihomework/dev/server-script/startappstaging.sh +WorkingDirectory=/var/www/aihomework/boilerplate/staging +ExecStart=/bin/bash /var/www/aihomework/dev/boilerplate/server-script/startappstaging.sh #Restart=on-failure #RestartSec=20s StartLimitInterval=0 diff --git a/server-script/startappprod.sh b/server-script/startappprod.sh index e7f711a5..44cccc70 100755 --- a/server-script/startappprod.sh +++ b/server-script/startappprod.sh @@ -1,5 +1,5 @@ #!/bin/bash -cd /var/www/aihomework/prod/ +cd /var/www/aihomework/boilerplate/prod/ mkdir -p logs /usr/bin/yarn start >> logs/prodoutput.log 2>&1 diff --git a/server-script/startappstaging.sh b/server-script/startappstaging.sh index 9e056b30..36707514 100644 --- a/server-script/startappstaging.sh +++ b/server-script/startappstaging.sh @@ -1,5 +1,5 @@ #!/bin/bash -cd /var/www/aihomework/staging/ +cd /var/www/aihomework/boilerplate/staging mkdir -p logs /usr/bin/yarn start >> logs/stagingoutput.log 2>&1