Blog Detail

preview image DevOps
by Anurag Srivastava, Jun 26, 2018, 4:50:18 PM | 3 minutes

Build and deploy Angular code using Python

I have already discussed the automated code deployment using Jenkins in my previous blogs but here I am going to explain you the process of creating an angular build and deploying on server using ssh/scp through python script.

The script in this blog can be optimized but I have elaborated it in order to explain different aspects like creating zip, copy to remote location and then move it from copied location to actual web root 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 scpclient from scp to perform remote file copy.

So lets create the script which will do following:

  • Create the 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 remote server.
  • SSH to the server and move to destination directory.
  • Unzip the build directory zip.
  • Copy it from destination directory to web root directory.
  • In case of any issue print the error message.
  • Close the ssh connection.


So as we are clear what to do lets jump into the Python code. So as I have already discussed import the libraries by writing following lines of code:

import os
import subprocess as sp
import paramiko
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:

os.chdir(buildFilePath)
sp.call("ng build",shell=True )
sp.call("zip -r dist.zip dist",shell=True )


Now our build is ready so create the ssh client and connect to the remote server.

ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=SERVER,username=user,password=passwd)

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:
scp.put(sourceFilePath, destinationFilePath)

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('cd '+destinationFilePath)
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())

At the end close the ssh connection.

ssh_client.close()


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.

My Github link: https://github.com/anubioinfo/build_and_deploy_angular_code/blob/master/angular_build_deploy.py

Please comment here if you have any query. 




About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Data Scientist | Innovator |

View Profile

Comments (1)

  • user image
    Navin Kholikov
    Oct 2, 2018, 2:37:16 PM

    Hi mr. Anurag Srivastava. How can I build and deploy angular project using atlassian bamboo? Help me please !

Leave a comment

Related Blogs

Load csv Data into Elasticsearch

Oct 15, 2018, 6:25:43 PM | Anurag Srivastava

htop: An Interactive Process Viewer

Oct 13, 2018, 8:49:59 PM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Top Blogs

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava