Automating Deployment with a Basic DevOps Script

Jude Tech
2 min readJan 9, 2024

--

Objective: Create a simple DevOps script to automate the deployment of a web application. The script will involve pulling the latest code from a version control system, installing dependencies, and restarting the application.

Sample Script: Let’s assume you have a Node.js web application hosted on a server, and you want to automate the deployment process

#!/bin/bash

# Deploy Script for Node.js Web Application

# Variables
APP_DIR="/path/to/your/app"
REPO_URL="https://github.com/yourusername/yourwebapp.git"
BRANCH="main"

echo "Starting deployment..."

# Navigate to the application directory
cd $APP_DIR || exit
echo "Changing to the application directory: $APP_DIR"

# Pull the latest code from the repository
git pull origin $BRANCH
echo "Pulled the latest code from $BRANCH branch"

# Install dependencies
npm install
echo "Installed npm dependencies"

# Restart the Node.js application (Assuming it's managed by PM2)
pm2 restart yourapp
echo "Restarted the Node.js application"

echo "Deployment completed successfully!"

Explanation:

  1. Variables: Set the necessary variables like the application directory, repository URL, and branch.
  2. Navigate to the Application Directory: Change into the application directory using cd.
  3. Pull Latest Code: Use git pull to fetch and merge the latest code from the specified branch.
  4. Install Dependencies: If your application uses npm, run npm install to install the required dependencies.
  5. Restart Application: Assuming your Node.js application is managed by PM2, restart it to apply the changes.
  6. Completion Message: Display a message indicating the successful completion of the deployment.

Application: This script can be utilized whenever you want to deploy the latest version of your Node.js web application. You can trigger it manually or integrate it into a continuous integration/continuous deployment (CI/CD) pipeline to automate the deployment process whenever changes are pushed to the repository.

Note: Ensure that you have the necessary permissions, and tailor the script according to your specific application structure and requirements. Adjustments may be needed based on the tools and technologies you use in your stack.

Thanks for reading

--

--

Jude Tech

IT professional || AWS Technology Architecting || Oracle Cloud Infrastructure Certified Associate||Cybersecurity for Businesses EC-Council || Technical Writer.