Introduction
In today's fast-paced software development environment, the effectiveness of a Continuous Integration and Continuous Deployment (CI/CD) pipeline is critical for delivering high-quality applications rapidly. A 3-tier application architecture consists of the presentation layer, business logic layer, and data access layer. In this article, I will walk you through the process of automating a CI/CD pipeline for a 3-tier application using Docker containers, Azure cloud services, and GitHub Actions for seamless deployment.
Understanding the 3-Tier Architecture
Before diving into the CI/CD implementation, it’s important to understand how a 3-tier application functions:
Presentation Layer: This is the user interface of the application that handles user interactions, typically built with front-end technologies.
Business Logic Layer: This tier manages the core logic of the application, processing commands, making logical decisions, and performing calculations.
Data Access Layer: This layer interacts with the database, executing queries and managing data persistence.
Each layer can be developed and deployed independently, making them ideal candidates for a containerized deployment strategy.
Setting Up Docker
Docker containers are a crucial aspect of our CI/CD pipeline since they ensure consistent environments across development, testing, and production.
Create a Dockerfile: Start by defining a Dockerfile for your application’s backend service. Here’s an example for a Node.js application:
# Use Node.js as the base image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy application files
COPY . .
# Expose the application port
EXPOSE 3000
# Command to run the application
CMD ["npm", "start"]Build the Image: Run the following command to build the Docker image:
docker build -t my-node-app .Setting Up Azure
To deploy your application, we’ll use Azure's cloud infrastructure. Here’s how you can set it up:
Create an Azure Account: If you don’t have one yet, sign up for an Azure account to get access to their cloud services.
Deploy Azure Container Instances (ACI): In the Azure portal, navigate to Container Instances and create a new instance. During setup, link it to your Docker image stored on a container registry.
SQL Database: For the data access layer, create an Azure SQL Database. Ensure that you configure the necessary firewall settings to allow access to your application.
Configuring GitHub Actions
With Docker and Azure set up, it’s time to leverage GitHub Actions for CI/CD. Create a .github/workflows/ci-cd.yml file in your repository:
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: my-node-app:latest
- name: Deploy to Azure
uses: Azure/setup-azure-cli@v1
with:
azure-clients-id: ${{ secrets.AZURE_CLIENT_ID }}
azure-client-secret: ${{ secrets.AZURE_CLIENT_SECRET }}
azure-tenant-id: ${{ secrets.AZURE_TENANT_ID }}
- name: Deploy the container
run: |
az container create --resource-group myResourceGroup --name myContainer --image my-node-app:latest --ports 3000Explanation of the Workflow
Trigger: The workflow is triggered on every push to the main branch.
Build Job: This job checks out the code and builds the Docker image. It then pushes the image to Docker Hub.
Deployment: Finally, it uses the Azure CLI to create a container instance using the newly built image.
Conclusion
Implementing a CI/CD pipeline for a 3-tier application using Docker, Azure, and GitHub Actions
