Jenkins pipeline trigger pipeline on Agent

59 views Asked by At

I have A Jenkins Server and 3 Agents. Builder, Programmer, and Tester. The three agents are pretty self explanatory. The Builder builds a Yocto image and saves the output files. The Programmer takes the saved image files and places them onto a device under test. The Tester then runs a suite of automated tests against the new image files.

If my Builder's Yocto stage is complete I would like it to kick off a pipeline on the Programmer agent. I am having a hard time finding an example of this. I assume it would be something similar to this:

pipeline {
    agent {
        label 'YoctoBuildNode'
    }
    
    stage('Yocto') {
            steps {
                script {
                    if (isStagingOrMaster()) {
                        echo "Performing a full Yocto build for Staging!"
                        sh "chown -R $USER:$USER ${env.WORKSPACE}"
                        sh "chmod -R a+rwx ${env.WORKSPACE}"
                        sh "${env.WORKSPACE}/jenkins_quick_start.sh"
                    } else {
                            echo "Building the main Application for branch ${env.BRANCH_NAME}"
                            buildProject()
                        }
                }
            }
        }
    stage('Doxygen') {
    
    }
    state('Trigger Programmer') {
        agent {
            label 'ProgramNode'
        }
        Not sure what to do here
    }
2

There are 2 answers

0
markalex On

There is an example here, specifically second example in this section.

In short, you declare top-level agent none, and then declare appropriate agent on every step of your build.

Something like this:

pipeline {
    agent none 
    stages {
        stage('Yocto') {
            agent { label 'YoctoBuildNode' } 
            steps {
                echo 'Yo, I'm cool Yocto'
                println agentName
            }
        }
        stage('Example Test') {
            agent { label 'ProgramNode' } 
            steps {
                echo 'Ehh, I'm boring JDK'
                println agentName
            }
        }
    }
}
0
Ruslan On

you can use different agent for each stage. each stage will be executed on own agent, and to pass artifacts from one agent to another need to use stash unstash
because agents don't share filesystem

pipeline {
  agent none 
  stages {
    stage('Yocto') {
      agent { label 'YoctoBuildNode' } 
      steps {
        script {
          if (isStagingOrMaster()) {
            echo "Performing a full Yocto build for Staging!"
            sh "chown -R $USER:$USER ${env.WORKSPACE}"
            sh "chmod -R a+rwx ${env.WORKSPACE}"
            sh "${env.WORKSPACE}/jenkins_quick_start.sh"
          } else {
            echo "Building the main Application for branch ${env.BRANCH_NAME}"
            buildProject()
            stash includes: 'dist/**/*', name: 'builtSources'
          }
        }
      }
    }
    stage('Example Test') {
        agent { label 'ProgramNode' } 
        steps {
          unstash 'builtSources'
          println agentName
        }
    }
  }
}