diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6ac5e77..b731adf 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,21 +1,21 @@ -name: Deploy - +name: Build & Deploy on: push: - branches: [ main ] # or 'master' if that's your main branch + branches: [main] jobs: deploy: runs-on: ubuntu-latest steps: - - name: Deploy to Server - uses: appleboy/ssh-action@v1.0.0 + - name: Deploy NodeJS app + uses: appleboy/ssh-action@v0.1.2 with: - host: ${{ secrets.HOST }} - username: root - key: ${{ secrets.SSH_PRIVATE_KEY }} + host: ${{secrets.SSH_HOST}} # IP address of the server you wish to ssh into + key: ${{secrets.SSH_KEY}} # Private or public key of the server + username: ${{ secrets.SSH_USERNAME }} # User of the server you want to ssh into + script: | - cd /var/www/valkyrie - git pull - npm install - pm2 reload all || pm2 start server/server.js --name "valkyrie" \ No newline at end of file + mkdir test + cd test + git clone git@github.com:Mulubwa17/ci-cd-to-digitalocean-with-github-actions.git + echo 'Deployment successful to digital ocean' \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..038261f --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: node server/server.js \ No newline at end of file diff --git a/package.json b/package.json index ea3d7f3..52e52b1 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "start:maintenance": "cross-env MAINTENANCE_MODE=true node server/server.js", "dev": "nodemon server/server.js", "dev:maintenance": "cross-env MAINTENANCE_MODE=true nodemon server/server.js", - "build": "echo 'No build script needed'", + "build": "npm install", + "heroku-postbuild": "npm install", "create-admin": "node server/scripts/createAdmin.js", "reset-user-ids": "node server/scripts/resetUserIds.js", "generate-test-users": "node server/scripts/generateTestUsers.js", diff --git a/server/server.js b/server/server.js index d9949de..d01d86f 100644 --- a/server/server.js +++ b/server/server.js @@ -121,7 +121,7 @@ async function connectToDatabase() { console.log('Connected to MongoDB'); } catch (error) { console.error('Error connecting to database:', error); - throw error; + setTimeout(connectToDatabase, 5000); } } } @@ -189,14 +189,7 @@ app.use('/api', userRoutes); app.use('/video', express.static(path.join(__dirname, '../video'))); // Serve static files from the client directory -app.use( - '/', - (req, res, next) => { - res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); - next(); - }, - express.static(path.join(__dirname, '../client')) -); +app.use(express.static(path.join(__dirname, '../client'))); // Serve static files from the images directory app.use( @@ -312,8 +305,14 @@ app.use((req, res, next) => { // Call this function after the server starts app.listen(port, '0.0.0.0', async () => { console.log(`Server running at http://0.0.0.0:${port}`); - if (process.env.NODE_ENV !== 'production') { - await resetUserIdsIfNeeded(); + console.log('Environment:', process.env.NODE_ENV); + try { + await connectToDatabase(); + if (process.env.NODE_ENV !== 'production') { + await resetUserIdsIfNeeded(); + } + } catch (error) { + console.error('Startup error:', error); } });