DevOps Intermediate

Git Automated Deployment

Automated deployment script from Git repository. Automates pull, build and deploy operations.

Published: May 05, 2024

Detailed Information

This script performs automated deployment from Git repository. Executes pull, build and deploy operations with single command. Can be used in CI/CD pipelines.

What Does This Script Do?

This script automates deployment process:

  • Clones Git repository
  • Pulls specified branch
  • Runs build command (optional)
  • Backs up current deployment
  • Deploys new version
  • Cleans temporary files

Why Should You Use It?

Automated deployment speeds up DevOps processes:

  • Speed: Automation instead of manual deployment
  • Reliability: Consistent deployment process
  • Backup: Security with automatic backup
  • CI/CD: Pipeline integration

How to Use

Step-by-Step Usage Guide

1. Create Script File

nano git_deploy.sh

2. Make Executable

chmod +x git_deploy.sh

3. Run Script

./git_deploy.sh https://github.com/user/repo.git main /var/www/app "npm run build"

4. CI/CD Integration

# Automated deployment with webhook
# Use with GitHub Actions, GitLab CI or Jenkins

Requirements

Requirements

  • Git: Git must be installed
  • Repository Access: Access to Git repository
  • Deploy Directory: Write permission to deployment directory

Use Cases

Use Cases

1. Production Deployment

Perform automated deployment to production environment.

2. CI/CD Pipeline

Use in CI/CD pipelines.

Examples

Usage Examples

Example 1: Node.js Application

./git_deploy.sh https://github.com/user/app.git main /var/www/app "npm install && npm run build"

Code

#!/bin/bash

# Git Automated Deployment Script

REPO_URL="$1"
BRANCH="${2:-main}"
DEPLOY_PATH="$3"
BUILD_COMMAND="$4"

if [ -z "$REPO_URL" ] || [ -z "$DEPLOY_PATH" ]; then
    echo "Usage: $0 <repo_url> [branch] <deploy_path> [build_command]"
    echo "Example: $0 https://github.com/user/repo.git main /var/www/app \"npm run build\""
    exit 1
fi

TEMP_DIR="/tmp/deploy_$(date +%Y%m%d_%H%M%S)"

echo "======================================"
echo "   GIT AUTOMATED DEPLOYMENT"
echo "======================================"
echo "Repository: $REPO_URL"
echo "Branch: $BRANCH"
echo "Deploy Path: $DEPLOY_PATH"
echo ""

# Clone repository
echo "1. Cloning repository..."
git clone -b "$BRANCH" "$REPO_URL" "$TEMP_DIR"
if [ $? -ne 0 ]; then
    echo "Error: Failed to clone repository"
    exit 1
fi
echo "✓ Repository cloned"
echo ""

cd "$TEMP_DIR" || exit 1

# Build if command provided
if [ -n "$BUILD_COMMAND" ]; then
    echo "2. Building application..."
    eval "$BUILD_COMMAND"
    if [ $? -ne 0 ]; then
        echo "Error: Build failed"
        exit 1
    fi
    echo "✓ Build completed"
    echo ""
fi

# Backup current deployment
if [ -d "$DEPLOY_PATH" ]; then
    echo "3. Backing up current deployment..."
    BACKUP_PATH="${DEPLOY_PATH}_backup_$(date +%Y%m%d_%H%M%S)"
    mv "$DEPLOY_PATH" "$BACKUP_PATH"
    echo "✓ Backup created: $BACKUP_PATH"
    echo ""
fi

# Deploy
echo "4. Deploying..."
mkdir -p "$(dirname "$DEPLOY_PATH")"
mv "$TEMP_DIR" "$DEPLOY_PATH"
echo "✓ Deployment completed"
echo ""

# Cleanup
echo "5. Cleaning up..."
rm -rf "$TEMP_DIR"
echo "✓ Cleanup completed"
echo ""

echo "======================================"
echo "   DEPLOYMENT SUCCESSFUL"
echo "======================================"
echo "Deployed to: $DEPLOY_PATH"
echo "Completed: $(date)"

Usage

chmod +x git_deploy.sh
./git_deploy.sh https://github.com/user/repo.git main /var/www/app "npm run build"

Troubleshooting

Troubleshooting

Problem: "git: command not found"

Solution: Install Git:

sudo apt-get install git

Tags

git deployment ci/cd devops automation