Git Product home page Git Product logo

jenkins-k8sagent-lib's Introduction

Jenkins Shared Library to get dynamic agent from Kubernetes cloud

This is a Jenkins Shared Library to get a dynamic agent from Kubernetes cloud.

Define the required agent in format "a+b+c", for example, "small+pg+privileged" to get an agent of "small size, additional postgres container and jnlp container running in privileged mode".
It's flexible to add templates in folder resources/podtemplates. The yaml files will be merged together and passed to Kubernetes Plugin to launched the desired pod.

Below lines will get an agent with 2 containers in the pod, a JNLP container of "mini" size plus a "postgres" container.

pipeline {
  agent {
    kubernetes(k8sagent(name: 'mini+pg'))
  }
  stages {
    stage('demo') {
      steps {
        echo "this is a demo"
        script {
          container('pg') {
            sh 'su - postgres -c \'psql --version\''
          }
        }
      }
    }
  }
}

A more sophisticated example is here, where you could decide the agent type with more complex logic.

Preconditionl

  • Running Kubernetes cluster
  • Jenkins server with connection to the cluster
  • Kubernetes Plugin installed on the Jenkins

The Jenkins server could be stand-alone or be running inside the Kubernetes cluster.

Usage

Recommended Method 1 - Import into Jenkins

Import the library following the instruction import

Use it in pipeline

@Library("k8sagent") _

Method 2 - Load dynamically

@Library("github.com/liejuntao001/jenkins-k8sagent-lib") _

However due to Jenkins security control, multiple methods need get approved.

Build and test

The library is a gradle project with tests.

.
├── build.gradle
├── src
│   └── com
├── test
│   ├── com
│   └── groovy
├── testjobs
│   ├── k8sagent_Jenkinsfile.groovy
│   └── simple_Jenkinsfile.groovy
└── vars
    └── k8sagent.groovy

Local build and test

./gradlew clean test

> Task :test
K8sAgentTest: testSdk25: SUCCESS
K8sAgentTest: testSmall: SUCCESS
K8sAgentTest: testBase: SUCCESS
K8sAgentTest: testFast: SUCCESS
K8sAgentTest: testPg: SUCCESS
K8sAgentTest: testPrivileged: SUCCESS
Tests: 6, Failures: 0, Errors: 0, Skipped: 0


BUILD SUCCESSFUL in 2s
6 actionable tasks: 5 executed, 1 up-to-date

Run the demo in a real Jenkins Job .

demojob

jenkins-k8sagent-lib's People

Contributors

liejuntao001 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.