Skip to content

How to Use the Deployment Script for Efficient Code Deployment Across Environments

When managing multiple environments whether staging, production manual deployment can be cumbersome. The Deployment Script simplifies this process by automating common tasks like uploading or downloading code, database synchronization, and accessing server environments via SSH.

In this article, we’ll walk through how to use the Deployment Script to deploy your code seamlessly between different environments, making it an essential tool for backend services, web applications, and monolithic apps.

What is the Deployment Script?

The Deployment Script automates the task of deploying code and databases across different environments. It is simple to use and helps ensure that your environments remain synchronized with minimal manual intervention. You can use it for:

  • Uploading code from your local environment to staging or production.
  • Downloading code from remote environments.
  • Accessing servers or databases via SSH or shell commands.
  • Syncing databases between your local environment and remote environments.

Getting Started: Setting Up the Deployment Script

To begin, you’ll need to clone the repository and set up the necessary credentials for your environments (staging, production, and local).

1. Clone the Repository

Start by cloning the script repository to your machine:

git clone https://github.com/xplodman/Deployment-Script.git
cd Deployment-Script

2. Configure Your Environments

The script uses environment configuration files to handle different servers. In your credentials.sh file, you’ll define the credentials for each environment (staging, production, and local). This includes your server and database credentials.

Here’s an example configuration structure:

# Start staging environment credentials
## Server credentials
staging_port='22' # Default 22
staging_user_ip='server_user@server_ip'
staging_ssh_password='your_ssh_password_if_exists' # Remove if no password
staging_private_key='your_private_key_path' # Remove if no key
staging_site_dir='server_full_path'

## Database credentials
staging_db_name='your_staging_db_name'
staging_db_host='127.0.0.1' # Default 127.0.0.1
staging_db_port='3306' # Default 3306
staging_db_username='your_staging_db_username'
staging_db_password='your_staging_db_password'


# Start production environment credentials
## Server credentials
production_port='22' # Default 22
production_user_ip='server_user@server_ip'
production_ssh_password='your_ssh_password_if_exists' # Remove if no password
production_private_key='your_private_key_path' # Remove if no key
production_site_dir='server_full_path'

## Database credentials
production_db_name='your_production_db_name'
production_db_host='127.0.0.1' # Default 127.0.0.1
production_db_port='3306' # Default 3306
production_db_username='your_production_db_username'
production_db_password='your_production_db_password'


# Local environment configuration
local_site_dir='site_directory'
local_db_dir='database_directory'
local_db_name='root'
local_db_username='root'
local_db_password='root'
special_commands_after_import_db_locally=''
special_commands_after_upload_to_environment=''
db_split_threshold=60

# Add more environments as needed
environments=('production' 'staging' 'local')

Usage of the Deployment Script

Once you have the script set up and the environment variables defined, you can use it to perform different deployment actions. Here’s a breakdown of the available options and their usage.

Running the Script

To see the available commands and usage, run:

./deploy_rsync.sh --help

This will output the following usage instructions:

Usage: ./deploy_rsync.sh <action> <environment>
Available actions: List of actions:
1. --upload env (Upload Local Site to env)
2. --download env (Download env Site to Local)
3. --ssh env (To enter the env server via ssh)
4. --db env (To enter the env database shell)
5. --download-db env (Download the env database to Local and remove it from the remote after the download is finished)
6. --import-db env (Import the env database to Local)
7. --upload-db env (Upload Local database to env)
Available environments:
production
staging

Each action has a specific purpose, and the <environment> can be either production or staging, depending on your configuration.

Common Deployment Actions

  1. Upload Local Site to Environment

To upload your local site to the staging or production environment, use:

./deploy_rsync.sh --upload staging

This command uploads the local site to the staging environment.

  1. Download Site from Environment to Local

If you need to download the remote site from staging or production back to your local environment, use:

./deploy_rsync.sh --download production
  1. SSH into the Environment Server

To SSH into your staging or production server for direct access, use:

./deploy_rsync.sh --ssh staging
  1. Database Operations

The script also supports database-related operations:

  • Download Database from Environment to Local:
./deploy_rsync.sh --download-db production
  • Import Database from Environment to Local:
./deploy_rsync.sh --import-db staging
  • Upload Local Database to Environment:
./deploy_rsync.sh --upload-db production

Customizing the Script for Your Project

You can customize the script for additional environments or to suit specific deployment needs. The environment credentials, SSH details, and database settings are stored in credentials.sh.

  • Add new environments by modifying the environments=('production' 'staging') array.
  • Update your environment-specific variables in credentials.sh to reflect the correct paths, credentials, and settings.

References

Conclusion

The Deployment Script streamlines the deployment process for developers, allowing for quick, consistent code pushes across different environments (staging, production, local). Whether you’re deploying code, syncing databases, or accessing server environments, this script automates essential tasks and reduces the risk of human error.

Check out the full instructions in the Deployment Script repository and start simplifying your deployment process today!

For additional help with automation or customizing your deployment workflow, feel free to reach out to XTND. We specialize in building automated solutions to make development more efficient.

Related Articles

Contact Us

Needs help with automation or customizing your deployment workflow

We specialize in building automated solutions to make development more efficient

Get In Touch