Next.js app serves an old build after GitHub Actions deployment to a Google Cloud VM
by Scallar It Solution
I am deploying a Next.js application using the App Router to an Ubuntu virtual machine on Google Cloud. The deployment is triggered through GitHub Actions whenever code is pushed to the main branch. The workflow successfully pulls the latest code, installs dependencies, creates a production build, and restarts the application with PM2. However, the website sometimes continues showing the previous version even though the GitHub Actions workflow finishes successfully. My deployment commands are similar to: cd /var/www/scallar git fetch origin git reset --hard origin/main npm ci npm run build pm2 restart scallar-app The PM2 application is started using: npm start I have also tried: pm2 delete scallar-app pm2 start npm --name "scallar-app" -- start The repository contains the updated files, and the .next directory has a recent timestamp. Restarting Nginx or manually deleting the .next directory and rebuilding usually fixes the problem. Expected behaviour: Every successful deployment should immediately serve the latest Next.js build. Actual behaviour: The previous build is sometimes served until I manually remove the build directory and restart PM2 or Nginx. What is the correct deployment process for a Next.js application running behind Nginx and PM2? Should the .next directory always be removed before building, and could PM2, Nginx caching, or multiple running Node.js processes be causing the old version to remain active?