I have already discussed the automated code deployment using Jenkins in my previous blogs but here I am going to explain to you the process of creating an Angular build and deploying it on the server using ssh/scp through a python script. Python is a language that can be used to solve many problems and this blog is just to explain how using a Python script, we can create an Angular build and deploy it to a remote server.
The script in this blog can be optimized but I have elaborated it in order to explain different aspects like creating zip, copy to a remote location and then move it from copied location to actual webroot location on the server from your machine.
For achieving this we need to import some python libraries like os for operating system commands, subprocess for executing the shell commands from Python, paramiko for creating ssh client and SCP client to perform remote file copy using SCP.
So let's create the script which will do following:
- Create an Angular build on a built path.
- Zip the build before sending it to the server.
- Create the SSH client and connect to the server.
- Create the SCP client and send the zip to the remote server.
- SSH to the server and move to the destination directory.
- Unzip the build directory zip.
- Copy it from destination directory to webroot directory.
- In case of any issue print the error message.
- Close the ssh connection.
So as we are clear what to do let us jump into the Python code. So as I have already discussed import the libraries by writing the following lines of code:
import subprocess as sp
from scp import SCPClient
Create the configuration for server, username, password, local build path, source file path, destination file path, and destination web root:
SERVER = '192.168.XX.XX'
user = 'user'
passwd = 'your_password'
buildFilePath = '/var/www/html/code_directory/'
sourceFilePath = buildFilePath+'dist.zip'
destinationFilePath = '/var/tmp/'
destinationWebRoot = '/var/www/html/test/'
In above code SERVER is the remote server where we are going to deploy the angular build, user and passwd is the server credential, buildFilePath is the directory where angular source code is available, sourceFilePath is the path we will use after creating the build inside buildFilePath, destinationFilePath is remote server location where we will copy the zipped angular build and destinationWebRoot is the remote server location where finally this angular code will be deployed.
Now create the build and zip it before copying to the remote location:
sp.call("ng build",shell=True )
sp.call("zip -r dist.zip dist",shell=True )
Now our build is ready to create the ssh client and connect to the remote server.
After creating the ssh client create the SCP client and remotely copy the angular built zip directory to the destination file path of remote server.
with SCPClient(ssh_client.get_transport()) as scp:
Now SSH to the remote server, move to the destination file path, unzip the angular built and copy it to the destination web root location.
ssh_client.exec_command('unzip '+destinationFilePath+'dist.zip -d '+destinationFilePath)
stdin, stdout, stderr = ssh_client.exec_command('cp -a '+destinationFilePath+'dist/* '+destinationWebRoot)
print("stderr: ", stderr.readlines())
print("pwd: ", stdout.readlines())
In the end, close the ssh connection.
In this way, we can automate the angular built creation and deployment process using Python script. We can customize this script as per our requirement.
You can get the code on my Github link: https://github.com/anubioinfo/build_and_deploy_angular_code/blob/master/angular_build_deploy.py
In case of any doubt please leave your comments. You can also follow me on Twitter:https://twitter.com/anubioinfo
Author of “Mastering Kibana 6.x”, “Kibana 7 Quick Start Guide”, “Learning Kibana 7”, &“Elasticsearch 7 Quick Start Guide” books & AWS Certified Solutions Architect.View Profile
Feb 9, 2019, 6:34:22 PM | Anurag Srivastava
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Sep 16, 2019, 5:55:06 AM | Anurag Srivastava
Apr 6, 2019, 8:41:41 PM | Anurag Srivastava
Jan 7, 2020, 7:15:34 PM | Anurag Srivastava
Jan 6, 2020, 7:30:13 PM | Anurag Srivastava
Jan 14, 2020, 10:22:34 AM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava
Jun 13, 2018, 3:44:01 PM | Anurag Srivastava
May 12, 2018, 4:47:07 PM | Anurag Srivastava
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Apr 24, 2018, 2:52:28 PM | Anurag Srivastava
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Jun 2, 2018, 10:49:54 AM | Anurag Srivastava