Blog>
Snippets

Leveraging TanStack Config for CI/CD Pipelines

Showcase a practical example of using TanStack Config to automate and streamline configurations within a CI/CD pipeline for efficient deployment.
const { createServer } = require('http');
const { execSync } = require('child_process');

createServer((req, res) => {
  if (req.url === '/deploy' && req.method === 'POST') {
    try {
      // Pull the latest code
      execSync('git pull origin main');
      // Install/update dependencies
      execSync('npm install');
      // Build the project using TanStack Config
      execSync('tanstack build');
      
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Deployment successful');
    } catch (error) {
      console.error('Deployment failed:', error);
      res.writeHead(500, { 'Content-Type': 'text/plain' });
      res.end('Deployment failed');
    }
  } else {
    res.writeHead(404);
    res.end();
  }
}).listen(8080);

console.log('Server listening on port 8080');
This JavaScript code creates a simple HTTP server that listens for POST requests on the /deploy URL. It uses the 'child_process' module to execute shell commands for pulling the latest code from the Git repository, installing or updating dependencies with npm install, and then building the project using TanStack Config's build command. If the commands execute successfully, it sends a 200 OK response indicating a successful deployment. In case of an error, it sends a 500 Internal Server Error response. This server could be used as part of a CI/CD pipeline to automate deployments.