Jenkins Agent

Intro

The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

Built-in podTemplate

The podTemplate is a template of a pod that will be used to create agents, users can define a podTemplate to use in the kubernetes plugin.

When creating a pipeline, each Pod contains at least the jnlp container for Jenkins Master to communicate with the Jenkins Agent. In addition, users can add containers in the podTemplate to meet their own needs. Users can choose to use the form of their own Pod yaml to flexibly control the runtime, and the container can be switched by the container command.

  1. pipeline {
  2. agent {
  3. kubernetes {
  4. //cloud 'kubernetes'
  5. label 'mypod'
  6. yaml """
  7. apiVersion: v1
  8. kind: Pod
  9. spec:
  10. containers:
  11. - name: maven
  12. image: maven:3.3.9-jdk-8-alpine
  13. command: ['cat']
  14. tty: true
  15. """
  16. }
  17. }
  18. stages {
  19. stage('Run maven') {
  20. steps {
  21. container('maven') {
  22. sh 'mvn -version'
  23. }
  24. }
  25. }
  26. }
  27. }

At the same time, in order to reduce the user’s learning cost, we have built in some podTemplate, so that users can avoid writing yaml files.

In the current version we have built in 4 types of podTemplates, i.e. base, nodejs, maven, go, and provide an isolated Docker environment in the Pod.

You can use the built-in podTempalte by specifying the Agent’s label. For example, to use the nodejs podTemplate, you can specify label as nodejs when creating the Pipeline, as shown in the example below.

DevOps Project Management - 图1

  1. pipeline {
  2. agent {
  3. node {
  4. label 'nodejs'
  5. }
  6. }
  7. stages {
  8. stage('nodejs hello') {
  9. steps {
  10. container('nodejs') {
  11. sh 'yarn -v'
  12. sh 'node -v'
  13. sh 'docker version'
  14. sh 'docker images'
  15. }
  16. }
  17. }
  18. }
  19. }

podTemplate base

NameType / Version
Jenkins Agent Labelbase
Container Namebase
OScentos-7
Docker18.06.0
Helm2.11.0
KubectlStable release
Built-in Softwaresunzip、which、make、wget、zip、bzip2、git

podTemplate nodejs

NameType / Version
Jenkins Agent Labelnodejs
Container Namenodejs
OScentos-7
Node9.11.2
Yarn1.3.2
Docker18.06.0
Helm2.11.0
Kubectlstable release
Built-in Softwaresunzip、which、make、wget、zip、bzip2、git

podTemplate maven

NameType / Version
Jenkins Agent Labelmaven
Container Namemaven
OScentos-7
Jdkopenjdk-1.8.0
Maven3.5.3
Docker18.06.0
Helm2.11.0
Kubectlstable release
Built-in Softwaresunzip、which、make、wget、zip、bzip2、git

podTemplate go

NameType / Version
Jenkins Agent Labelgo
Container Namego
OScentos-7
Go1.11
GOPATH/home/jenkins/go
GOROOT/usr/local/go
Docker18.06.0
Helm2.11.0
Kubectlstable release
Built-in Softwaresunzip、which、make、wget、zip、bzip2、git