Trigger Multi-stage child pipeline builds using Jenkins

Trigger Multi-stage child pipeline builds using Jenkins

Jenkins is an open-source automation server that is widely used in the field of DevOps (Development and Operations) for continuous integration and continuous delivery (CI/CD) processes. Here are some of the key uses and benefits of Jenkins in a DevOps environment:

No alt text provided for this image


1. Continuous Integration

2. Continuous Delivery/Deployment

3. Automated Testing

4. Monitoring and Reporting

5. Extensibility

6. Community and Support

In summary, Jenkins is a powerful tool for automating CI/CD processes, enabling continuous integration, delivery, and deployment. Its flexibility, extensibility, and strong community support make it a popular choice for organizations adopting DevOps practices.

Running Multiple Child Pipelines with a Parent Pipeline in Jenkins

Understanding the Parent-Child Pipeline Model:

1. The parent-child pipeline model allows for the organization and orchestration of multiple pipelines.

2. In this model, the parent pipeline acts as the controlling mechanism, responsible for triggering the execution of the child pipelines.

3. Each child pipeline focuses on a specific set of tasks or processes, while the parent pipeline manages the overall workflow.

The benefits here are:

1. Faster execution time when child job has a prepared workspace.

2. If the workspace preparation requires external API calls, this is done just once in the parent job.

3. A quick investigation of results associated with specific parent job run.

No alt text provided for this image


The Parent Pipeline:

1. The parent pipeline is orchestrating the execution of the child pipelines in the desired order.

2. Depending on the user requirements, it can trigger the Child Pipeline’s.

Before trigger the pipeline we need to install requirements

curl -sL https://rpm.nodesource.com/setup_lts.x | sudo bash -

sudo dnf install -y nodejs        

Run script

npm start        

Show the below parent pipeline code

pipeline 

agent any

parameters {

choice(

choices: ['child-job-1', 'child-job-2'],

description: 'Select the pipeline to execute',

name: 'SelectedPipeline'

)

}

stages {

stage('Main Pipeline') {

steps {

echo 'Running main pipeline...'

script {

def selectedPipeline = "${params.SelectedPipeline}"

// Call the selected child pipeline

if (selectedPipeline == 'child-job-1') {

build job: 'child-job-1', wait: true

} else if (selectedPipeline == 'child-job-2') {

build job: 'child-job-2', wait: true

} else {

error "Invalid pipeline selection:

${selectedPipeline}"

}

}

}

}

}

}        


Build the child pipeline using parameters

Here we can see the dashboard's of Parent pipeline select the child pipeline

No alt text provided for this image

After build parrent pipeline dashboard

Here we can see the dashboard's of Parent pipeline.

No alt text provided for this image
No alt text provided for this image

Child pipeline

Child pipeline-1 code

Show the below child pipeline code

pipeline 

agent any

stages {

stage('Running react js Code') {

steps {

script {

withCredentials([usernamePassword(credentialsId:

'Jenkins_CS7_with_PD', usernameVariable: 'REMOTE_USER', passwordVariable:

'REMOTE_PASSWORD')]) {

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'mkdir -p /opt/hello-worldreact1'"

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'cd /opt/hello-worldreact1'"

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'curl -sL

https://rpm.nodesource.com/setup_lts.x | sudo bash -'"

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'sudo dnf install -y

nodejs'"

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'cd /opt/hello-world-react1

&& npx create-react-app .'"

sh """

sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'echo \"import React from

\\"react\\";

function App() {

return (

<div className=\\"App\\">

<h1>Hello, World!</h1>

</div>

);

}

export default App;\" > /opt/hello-worldreact1/

src/App.js'

"""

sh "sshpass -p \"${env.REMOTE_PASSWORD}\" ssh -

o 'StrictHostKeyChecking=no' [email protected] 'cd /opt/hello-world-react1

&& npm start'"

}

}

}

}

}

}
        

Dashbord status of the child pipeline-1

Here we can see the child pipeline after the suceess build.

No alt text provided for this image

Once the job is down shows the results

No alt text provided for this image

Child pipeline-2 code

Show the below child pipeline code


pipeline 

agent any

stages {

stage('Running backend setup') {

steps {

withCredentials([usernamePassword(credentialsId:

'Jenkins_CS7_with_PD', usernameVariable: 'REMOTE_USER', passwordVariable:

'REMOTE_PASSWORD')]) {

script {

sh """

sshpass -p "${env.REMOTE_PASSWORD}" ssh -o

'StrictHostKeyChecking=no' ${env.REMOTE_USER}@172.168.9.181 '

mkdir -p /opt/my-backend-app &&

cd /opt/my-backend-app &&

npm init -y &&

npm install express &&

cat <<EOF > server.js

const express = require("express");

const app = express();

const PORT = 5000;

// Define a sample route

app.get("/api/hello", (req, res) => {

res.json({ message: "Hello from the

backend!" });

});

// Start the server

app.listen(PORT, () => {

console.log("Server is running on port" + PORT);

});

EOF

'

sshpass -p "${env.REMOTE_PASSWORD}" ssh -o

'StrictHostKeyChecking=no' ${env.REMOTE_USER}@172.168.9.181 '

firewall-cmd --zone=public --addport=

5000/tcp --permanent &&

firewall-cmd --reload &&

cd /opt/my-backend-app &&

node server.js

'

"""

}

}

}

}

}

}
        

Dashbord status of the child pipeline-1

Here we can see the child pipeline after the suceess build.

No alt text provided for this image

Once the job is down shows the results

No alt text provided for this image

I hope this article has provided you with valuable insights into the topic at hand. Whether you're a beginner or an experienced professional, there's always something new to learn and discover in the world. By sharing my knowledge and experience with you, I hope to have sparked your curiosity and inspired you to explore further. Thank you for taking the time to read this article, and I look forward to hearing your thoughts and feedback.

Written by,

SIVA PRASAD T

要查看或添加评论,请登录

社区洞察

其他会员也浏览了