Deployment of Applications on Azure VM with CI/CD using github workflows


Article content
Process Flow Diagram

In this article, we’ll walk through the process of deploying applications to the Azure cloud using a Virtual Machine (VM) and a GitHub workflow for Continuous Integration and Continuous Deployment (CI/CD). This setup is a popular method for automating deployments, ensuring code quality, and reducing manual intervention during the release process. The diagram above illustrates the overall flow, from development to deployment

Here we will focus on deploying the application to an Azure VM. The VM acts as a server where the application is hosted. Once the deployment is completed through the GitHub workflow, the VM pulls the latest code, builds the application, and runs it. This can be done using tools like Docker to containerize the application


Steps to be Followed:

  • Build Application on your local system

Article content
Sample project directory files


Article content
Application running in Local

  • Create Github repo and initialize the repo in your local system to enable CI


Article content
Setup Github Repo

  • Using Azure VM services create VM-UBUNTU server and login using putty

Note: While this guide assumes that you’ve already created an Ubuntu VM on Azure and set up SSH access using PuTTY, we won't cover those steps here, as there are many tutorials available on platforms like YouTube that explain the process in detail

  • Now that your VM is up and running, we’ll set up a GitHub Actions workflow that triggers automatically whenever there is a commit to the <branch name> branch. The workflow will perform the following tasks:

  1. Connect to the Azure VM via SSH: The workflow will establish a connection to the VM to begin the deployment process
  2. Directory Check and Navigation: The script will check if the relevant GitHub directory exists on the VM:
  3. Pull Latest Changes from GitHub: Once inside the correct directory, the workflow will pull the latest changes from the GitHub repository, ensuring that the directory is up to date with the latest code
  4. Port Check: The workflow will then check if port <port number> (used by Streamlit) is occupied by another process using pgrep. If it is, the script will stop the process currently using that port to free it up for the new deployment
  5. Docker Container Check and Cleanup: The script will verify if a Docker container named <app-name> (as specified in the workflow file) is already running:
  6. Build and Run the New Docker Image: After completing the checks, the script will:

With this automated process in place, any time new code is pushed to the specified branch, the workflow will automatically deploy the updated version of your Streamlit application to the Azure VM.

Note: for this use case I am going to deploy a streamlit application which will be accessible on port 8501


Article content
Sample Workflow yaml file

  • after creating the workflow file, make sure you add the Azure VM password to the environment secrets of your repository

Article content
Add Secret Key for Azure VM password

  • Now commit and push the changes (workflow file added) to the branch mentioned in the workflow file, This will automatically trigger the workflow

Article content
Workflow successfully Triggered and Completed with No errors

  • Now the application should run on the given port number. Access this application from the public IP address and port number: <public_ip_address:port_number>

Article content
Application Successfully Running

  • To Test the CI/CD, we will make a change to our application in the local and commit the changes to Github repo. the changes should Automatically reflect in the Application running in VM as well

Article content
Modified application file
Article content
Pushed the changes to Github Repo
Article content
Triggered workflow automatically and the workflow is Successful
Article content
Finally Changes reflected in the Application

Conclusion

Deploying applications to the Azure cloud using a GitHub workflow and Azure VMs is a powerful combination for automating and managing your deployment pipeline. This is also a cost effective option when compared with the Azure web app services

This setup ensures that your code is consistently tested, deployed, and updated, reducing the effort involved in manual deployments and improving the overall reliability of your system.

With Continuous Integration and Continuous Deployment (CI/CD) in place, developers can focus more on building features while the pipeline takes care of the testing and deployment, making it easier to ship high-quality applications faster.

To view or add a comment, sign in

Others also viewed

Explore topics