Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real Time Scenario for Multi-Stage Build - 3 Tier Architecture #329

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions multi-stage-multi-agent/Real_Used_Case_scenario/JenkinsFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
pipeline {
agent none // Define specific agents in each stage

stages {
stage('Build Web Tier') {
agent {
docker {
image 'openjdk:17-jdk' // Java Docker image
}
}
steps {
echo 'Building Web Tier...'
// Assuming a Maven-based Java project
sh 'mvn clean compile' // Compile the Java project
sh 'mvn package' // Package the Java project (produces a .jar or .war file)
}
}

stage('Test Web Tier') {
agent {
docker {
image 'openjdk:17-jdk' // Java Docker image
}
}
steps {
echo 'Testing Web Tier...'
// Run tests using Maven
sh 'mvn test' // Execute unit tests
sh 'mvn verify' // Verify the results, including integration tests if any
}
}

stage('Build Application Tier') {
agent {
docker {
image 'node:18' // Node.js Docker image
}
}
steps {
echo 'Building Application Tier...'
// Assuming a Node.js project with npm
sh 'npm install' // Install dependencies
sh 'npm run build' // Build the application (if using a build tool)
}
}

stage('Test Application Tier') {
agent {
docker {
image 'node:18' // Node.js Docker image
}
}
steps {
echo 'Testing Application Tier...'
// Run tests using npm
sh 'npm test' // Run the test suite defined in package.json
}
}

stage('Build Database Tier') {
agent {
docker {
image 'mariadb:latest' // SQL Docker image (MariaDB)
}
}
steps {
echo 'Building Database Tier...'
// For databases, this typically involves preparing schema or seed data
// Example script to initialize the database (customize as needed)
sh 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" < init-database.sql'
}
}

stage('Test Database Tier') {
agent {
docker {
image 'mariadb:latest' // SQL Docker image (MariaDB)
}
}
steps {
echo 'Testing Database Tier...'
// Example script to run tests or validate database schema
// This could involve connecting to the database and running queries
sh 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" < test-database.sql'
}
}

stage('Deploy Web Tier') {
agent {
docker {
image 'openjdk:17-jdk' // Java Docker image
}
}
steps {
echo 'Deploying Web Tier...'
// Deployment commands for the Web Tier
sh 'scp target/myapp.jar user@server:/path/to/deploy' // Example SCP command
sh 'ssh user@server "systemctl restart myapp"' // Example SSH command to restart the application
}
}

stage('Deploy Application Tier') {
agent {
docker {
image 'node:18' // Node.js Docker image
}
}
steps {
echo 'Deploying Application Tier...'
// Deployment commands for the Application Tier
sh 'scp -r dist/* user@server:/path/to/deploy' // Example SCP command
sh 'ssh user@server "pm2 restart myapp"' // Example SSH command to restart the Node.js application
}
}

stage('Deploy Database Tier') {
agent {
docker {
image 'mariadb:latest' // SQL Docker image (MariaDB)
}
}
steps {
echo 'Deploying Database Tier...'
// Database deployment commands (e.g., applying schema changes)
sh 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" < deploy-database.sql'
}
}
}

post {
always {
echo 'Cleaning up...'
// Cleanup steps, if necessary
// For example, remove temporary files or notify stakeholders
}
}
}
44 changes: 44 additions & 0 deletions multi-stage-multi-agent/Real_Used_Case_scenario/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Real Time Used Case Detailed Steps Explained:

**Web Tier (Java)**

**Build**:
mvn clean compile: Cleans and compiles the Java project.
mvn package: Packages the compiled code into a .jar or .war file.
**Test**:
mvn test: Runs unit tests.
mvn verify: Runs integration tests and verifies the results.


**Application Tier (Node.js)**

**Build**:
npm install: Installs dependencies defined in package.json.
npm run build: Builds the application if a build step is defined (e.g., for a React app).
**Test**:
npm test: Runs the tests defined in package.json.

**Database Tier (SQL)**

**Build**:
mysql -u root -p"$MYSQL_ROOT_PASSWORD" < init-database.sql: Initializes the database with schema and seed data.
**Test**:
mysql -u root -p"$MYSQL_ROOT_PASSWORD" < test-database.sql: Runs tests or validations on the database.
Deploy Stages

**Web Tier**:
Uses scp to copy the JAR file to the deployment server and ssh to restart the application service.

**Application Tie**r:
Uses scp to copy built files to the deployment server and ssh to restart the Node.js application using pm2.

**Database Tier**:
Uses mysql to apply any database changes or schema updates.

**Customization**:

**Scripts and Commands**: Ensure the scripts (e.g., build-web.sh, test-web.sh) and deployment commands are adapted to fit your actual build and deployment processes.

**Credentials**: Securely manage credentials (e.g., database passwords) using Jenkins credentials management rather than hard-coding them in scripts.

This pipeline setup should give you a comprehensive framework to manage builds, tests, and deployments across different tiers of your application using Docker containers for isolated and consistent environments.