book
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page
  • yaml
  • container

Was this helpful?

  1. jenkins
  2. jenkinsfile

node

PrevioustriggerNextscript

Last updated 2 months ago

Was this helpful?

[!TIP|label:reference:]

yaml

#!/usr/bin/env groovy

import groovy.transform.Field

@Field final String CLUSTER   = 'DevOps Kubernetes'
@Field final String NAMESPACE = 'devops'
String label                  = env.BUILD_TAG

ansiColor('xterm') { timestamps {
  podTemplate(
    label: label ,
    cloud: CLUSTER ,
    namespace: NAMESPACE ,
    showRawYaml: true,
    yaml: """
      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          jenkins: jnlp-inbound-agent
      spec:
        hostNetwork: true
        nodeSelector:
          <label_name>: "<value>"
        containers:
        - name: jnlp
          image: jenkins/inbound-agent:latest
          workingDir: /home/jenkins
          tty: true
    """
  ) { node (label) { container(name:'jnlp', shell:'/bin/bash') {
      stage('prepare') {
        sh """
          whoami
          pwd
          realpath ${WORKSPACE}
        """
      } // stage
  }}} // container | node | podTemplate
}} // timestamp | ansiColor

// vim: ft=Jenkinsfile ts=2 sts=2 sw=2 et

with resources

#!/usr/bin/env groovy

import groovy.transform.Field

@Field final String CLUSTER   = 'Jenkins Kubernetes'
@Field final String NAMESPACE = 'jenkins'
String label                  = env.BUILD_TAG

ansiColor('xterm') { timestamps {
  podTemplate(
    label: label ,
    cloud: CLUSTER ,
    namespace: NAMESPACE ,
    showRawYaml: true,
    yaml: """
      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          jenkins: jnlp-inbound-agent
      spec:
        hostNetwork: true
        nodeSelector:
          <label_name>: "<value>"
        containers:
        - name: jnlp
          image: jenkins/inbound-agent:latest
          workingDir: /home/jenkins
          tty: true
          resources:
            limits:
              memory: 1Gi
              cpu: 1
            requests:
              memory: 500Mi
              cpu: 0.5
    """
  ) { node (label) { container(name:'jnlp', shell:'/bin/bash') {
      stage('prepare') {
        sh """
          whoami
          pwd
        """
      } // stage
  }}} // container | node | podTemplate
}} // timestamp | ansiColor

// vim: ft=Jenkinsfile ts=2 sts=2 sw=2 et

with POD_LABEL

#!/usr/bin/env groovy

import groovy.transform.Field

@Field final String CLUSTER   = 'Jenkins Kubernetes'
@Field final String NAMESPACE = 'jenkins'

ansiColor('xterm') { timestamps {
  podTemplate(
    cloud: CLUSTER ,
    namespace: NAMESPACE ,
    showRawYaml: true,
    yaml: """
      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          jenkins: jnlp-inbound-agent
      spec:
        hostNetwork: true
        nodeSelector:
          <label_name>: "<value>"
        containers:
        - name: jnlp
          image: jenkins/inbound-agent:latest
          workingDir: /home/jenkins
          tty: true
          resources:
            limits:
              memory: 1Gi
              cpu: 1
            requests:
              memory: 500Mi
              cpu: 0.5
    """
  ) { node (POD_LABEL) { container(name:'jnlp', shell:'/bin/bash') {
      stage('prepare') {
        sh """
          whoami
          pwd
          ls -Altrh /home/
          ls -Altrh /home/jenkins/
          touch a.txt
          realpath ${WORKSPACE}
          realpath a.txt
        """
      } // stage
  }}} // container | node | podTemplate
}} // timestamp | ansiColor

// vim: ft=Jenkinsfile ts=2 sts=2 sw=2 et

default yaml

podTemplate(cloud: 'DevOps Kubernetes') {
  node(POD_LABEL) {
    stage('Run shell') {
      sh 'echo hello world'
    }
  }
}
  • windows (kubernetes)

    [!TIP|label:windows pod:]

    /**
     * Runs a build on a Windows pod.
     * Tested in EKS: https://docs.aws.amazon.com/eks/latest/userguide/windows-support.html
    **/
    podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: jnlp
        image: jenkins/inbound-agent:windowsservercore-1809
      - name: shell
        image: mcr.microsoft.com/powershell:preview-windowsservercore-1809
        command:
        - powershell
        args:
        - Start-Sleep
        - 999999
      nodeSelector:
        kubernetes.io/os: windows
    ''') {
        node(POD_LABEL) {
            container('shell') {
                powershell 'Get-ChildItem Env: | Sort Name'
            }
        }
    }
  • maven (kubernetes)

    // Build a Maven project using the standard image and Scripted syntax.
    // Rather than inline YAML, you could use: yaml: readTrusted('jenkins-pod.yaml')
    // Or, to avoid YAML: containers: [containerTemplate(name: 'maven', image: 'maven:3.6.3-jdk-8', command: 'sleep', args: 'infinity')]
    podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: maven
        image: maven:3.6.3-jdk-8
        command:
        - sleep
        args:
        - infinity
    ''') {
        node(POD_LABEL) {
            // or, for example: git 'https://github.com/jglick/simple-maven-project-with-tests'
            writeFile file: 'pom.xml', text: '''
              <project xmlns="http://maven.apache.org/POM/4.0.0">
                  <modelVersion>4.0.0</modelVersion>
                  <groupId>sample</groupId>
                  <artifactId>sample</artifactId>
                  <version>1.0-SNAPSHOT</version>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-surefire-plugin</artifactId>
                              <version>2.18.1</version>
                          </plugin>
                      </plugins>
                  </build>
                  <dependencies>
                      <dependency>
                          <groupId>junit</groupId>
                          <artifactId>junit</artifactId>
                          <version>4.12</version>
                          <scope>test</scope>
                      </dependency>
                  </dependencies>
                  <properties>
                      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                      <maven.compiler.source>1.8</maven.compiler.source>
                      <maven.compiler.target>1.8</maven.compiler.target>
                  </properties>
              </project>
            '''.stripIndent()
            writeFile file: 'src/test/java/sample/SomeTest.java', text: '''
              package sample;
              public class SomeTest {
                  @org.junit.Test
                  public void checks() {}
              }
            '''.stripIndent()
            container('maven') {
              sh 'mvn -B -ntp -Dmaven.test.failure.ignore verify'
            }
            junit '**/target/surefire-reports/TEST-*.xml'
            archiveArtifacts '**/target/*.jar'
        }
    }

container

podTemplate(cloud: 'DevOps Kubernetes', containers: [
  containerTemplate(
    name: 'jnlp',
    image: 'jenkins/inbound-agent:latest',
    ttyEnabled: true,
    privileged: false,
    alwaysPullImage: false,
    workingDir: '/home/jenkins'
  )
]) { node(POD_LABEL) {
    stage ('run') {
      echo 'yes!'
      sh """
        id
        whoami
        echo ${WORKSPACE}
        realpath ${WORKSPACE}
      """
    }
}}

multiple containerTemplate with resource limits

[!NOTE|label:add memory and cpu resources in containerTemplate:]

  • TIPS

    • the jnlp container template will be enabled by default, however it contains only requests:

      ---
      apiVersion: "v1"
      kind: "Pod"
      metadata:
        annotations:
          buildUrl: "https://jenkins.sample.com/job/devops/job/demo/job/container/7/"
          runUrl: "job/devops/job/demo/job/container/7/"
        labels:
          jenkins: "slave"
          jenkins/label-digest: "a170798a479a410811c24bd8c3bacac0dd709dff"
          jenkins/label: "devops_demo_container_7-7gfwb"
        name: "devops-demo-container-7-7gfwb-tjkzm-kfn2m"
        namespace: "marslo-test"
      spec:
        containers:
        - command:
          - "cat"
          image: "maven:3.3.9-jdk-8-alpine"
          imagePullPolicy: "IfNotPresent"
          name: "maven"
          resources:
            limits:
              memory: "1024Mi"
              cpu: "512m"
            requests:
              memory: "512Mi"
              cpu: "256m"
          tty: true
          volumeMounts:
          - mountPath: "/home/jenkins/agent"
            name: "workspace-volume"
            readOnly: false
        - command:
          - "cat"
          image: "golang:1.8.0"
          imagePullPolicy: "IfNotPresent"
          name: "golang"
          resources:
            limits:
              memory: "1024Mi"
              cpu: "512m"
            requests:
              memory: "512Mi"
              cpu: "256m"
          tty: true
          volumeMounts:
          - mountPath: "/home/jenkins/agent"
            name: "workspace-volume"
            readOnly: false
        - env:
          - name: "JENKINS_SECRET"
            value: "********"
          - name: "JENKINS_AGENT_NAME"
            value: "devops-demo-container-7-7gfwb-tjkzm-kfn2m"
          - name: "JENKINS_WEB_SOCKET"
            value: "true"
          - name: "JENKINS_NAME"
            value: "devops-demo-container-7-7gfwb-tjkzm-kfn2m"
          - name: "JENKINS_AGENT_WORKDIR"
            value: "/home/jenkins/agent"
          - name: "JENKINS_URL"
            value: "https://jenkins.sample.com/"
          image: "jenkins/inbound-agent:3206.vb_15dcf73f6a_9-2"
          name: "jnlp"
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
          volumeMounts:
          - mountPath: "/home/jenkins/agent"
            name: "workspace-volume"
            readOnly: false
        nodeSelector:
          jenkins.builder/loc: "dc5"
        restartPolicy: "Never"
        volumes:
        - emptyDir:
            medium: ""
          name: "workspace-volume"
podTemplate( cloud: 'DevOps Kubernetes',
             namespace: 'devops',
             nodeSelector: 'jenkins.builder/loc=dc5',
             containers: [
               containerTemplate(
                 name: 'maven',
                 image: 'maven:3.3.9-jdk-8-alpine',
                 ttyEnabled: true,
                 command: 'cat',
                 resourceRequestCpu: '256m',
                 resourceLimitCpu: '512m',
                 resourceRequestMemory: '512Mi',
                 resourceLimitMemory: '1024Mi'
               ),
               containerTemplate(
                 name: 'golang',
                 image: 'golang:1.8.0',
                 ttyEnabled: true,
                 command: 'cat',
                 resourceRequestCpu: '256m',
                 resourceLimitCpu: '512m',
                 resourceRequestMemory: '512Mi',
                 resourceLimitMemory: '1024Mi'
               ),
               containerTemplate(
                 name: 'jnlp',
                 image: 'jenkins/inbound-agent',
                 ttyEnabled: true,
                 resourceRequestCpu: '256m',
                 resourceLimitCpu: '512m',
                 resourceRequestMemory: '512Mi',
                 resourceLimitMemory: '1024Mi'
               )
             ]
) {
    node(POD_LABEL) {
      stage('maven') {
        container('maven') {
          sh """ which -a mvn """
        }
      }
      stage('golang') {
        container('golang') {
          sh """ which -a go """
        }
      }
    }
}
podTemplate( cloud: 'DevOps Kubernetes',
             namespace: 'devops',
             nodeSelector: 'devops.domain/loc=dc5',
             containers: [
              containerTemplate(
                 name: 'jnlp',
                 image: 'artifactory.domain.com/docker/devops/jnlp:4.1.0-py310-jammy',
                 args: '${computer.jnlpmac} ${computer.name}'
               ),
               containerTemplate(
                 name: 'maven',
                 image: 'artifactory.domain.com/dockerub-remote/maven:3.8.4-openjdk-17-slim',
                 ttyEnabled: true,
                 command: 'cat'
               ),
               containerTemplate(
                 name: 'golang',
                 image: 'artifactory.domain.com/dockerub-remote/golang:latest',
                 ttyEnabled: true,
                 command: 'cat'
               )
             ]
) {
    node(POD_LABEL) {
      stage('maven') {
        container('maven') {
          sh """ which -a mvn """
        }
      }
      stage('golang') {
        container('golang') {
          sh """ which -a go """
        }
      }
    }
}

windows.groovy
Changing container memory and cpu limits
zh jenkins-kubernetes-plugin
Kubernetes plugin
Container hangs in podTemplate in jenkinsfile
kubernetes
Kubernetes plugin
kubernetes sample code
pipeline
samples
windows.groovy
casc
yaml
with resources
with POD_LABEL
default yaml
container
multiple containerTemplate with resource limits