简体   繁体   中英

How to integrate Jobs in Jenkins using Pipeline

I have Four Jobs- A,B,C,D

A- Build

B- Test

C- Sonar Analysis

D- Deploy

My scenario-

1- I need to create a Pipeline A->B->C

2- I Need to create a other Pipeline A->B->D

My issue is--

1- If I select "Trigger Parameterized builds on other projects " and add Job B under Job A, I can't use Job A for my second scenario.

How should I use Job A for both the pipelines without effecting.

If I understand the question correctly, you are looking to create two pipelines. In the pipelines, you can define which jobs to build using stages.

For your requirement, you need to create two pipelines and define stages according to your needs. Trigger Parameterized builds on other projects is not a suitable option for you.

stage('Build A') {
build job: 'A' , parameters: <Give_your_own_parameters>
}

stage('Build B') {
build job: 'B' , parameters: <Give_your_own_parameters>
}
stage('Build C') {
build job: 'C' , parameters: <Give_your_own_parameters>
}

You can also get the syntax from Pipeline Syntax in the Pipeline Section of the pipeline you are building.

您可以通过创建A和B的重复项目来简化此过程。在New Item>从Project A复制细节期间可以轻松地复制Project

This is not easy feasible for a simple reason : you are not using Jenkins jobs the way they were meant to be used.

The concept of a job in Jenkins is that a job is a sequence of actions . A job does not have a single responsibility such as just building, just testing or just deploying. In your case, you should have 3 "actions" in a job, and 3 "actions" in another job.

The freestyle job approach

The common approach would be something like :

Build job

  1. Action 1 : build
  2. Action 2 : test
  3. Action 3 : run Sonar analysis

Deploy job

  1. Action 1 : build
  2. Action 2 : test
  3. Action 3 : deploy

Unless I'm missing something here, you don't want to separate these 3/4 actions in 4 separate jobs, as this would be highly inneficient . For example, test phase and Sonar analysis should probably be run just after a code build has been made, so you want to share the same workspace to be able to test your built code.

The pipeline approach

Another - preferred - approach would be to use actual Jenkins pipelines , ie Groovy scripts that will allow you to define your steps as functions and then reuse them in both your "Build job" and "Deploy job".

As an example, you could have a functions.groovy containing your build/test functions :

functions.groovy

def build() {
  // Build code here...
}

def test() {
  // Test code here...
}

build-job.groovy

node {
   def functions = load 'functions.groovy'

   stage('Build') {
       functions.build()
   }
   stage('Test') {
       functions.test()
   }
   stage('Sonar Analysis') {
       // Sonar analysis code...
   }
}

deploy-job.groovy

node {
   def functions = load 'functions.groovy'

   stage('Build') {
       functions.build()
   }
   stage('Test') {
       functions.test()
   }
   stage('Deploy') {
       // Deploy code...
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM