Jenkins & Infrastructure as Code
HungWei Chiu
Why
Maintain and manage three Jenkins servers


Almost 1000 Jenkins Jobs


What I learned
Bio
HungWei Chiu(Hwchiu)

MTS @ Open Networking Foundation (ONF)


Kubernetes/Container/Linux/Network...etc


Blog: https://hwchiu.com


Facebook: 矽⾕⽜的耕⽥筆記
Agenda
Introduction to Jenkins


Infrastructure as Code and Jenkins Job Builder (JJB)


Demo


Global JJB
Jenkins
Self-containerd, open source automation server


Can be used to automate all sorts of taks


Java-based application.


Plugin ecosystem to enhance its functionality.
Supports
VSC system


Git/GitHub/GitLab/Gerrit


Bitbucket/SVC...etc


Authentication system


Internal DB, LDAP, OAuth, Azure AD, Crowd, OpenID..etc


Authorization


Matrix-based security (by user and groups)


Project-based


Almost all functions are powered by plugin.
Use Cases
Source Code
(GitHub/Git)
Developer Jenkins
Unit Test
Integration Test
Tasks
Build container
Push container
Deploy application
Run your scripts
Push Trigger
Feedback
Execute
Use Cases
Others
Developer Jenkins
Unit Test
Integration Test
Tasks
Build container
Push container
Deploy application
Run your scripts
Web hook
Feedback
Execute
Scheduled Job
Source Code
(GitHub/Git)
Developer Jenkins
Unit Test
Integration Test
Tasks
Build container
Push container
Deploy application
Run your scripts
Feedback
Pull
Execute
Architecture
Jenkins Server


Self-hosted


Jenkins Job


Freestyle


Pipeline


GitHub Organization


Multi-branch Pipeline


Executor
Architecture
Jenkins


Job A
Con
fi
guration
Tasks
Job B
Con
fi
guration
Tasks
Job C
Con
fi
guration
Tasks
Job D
Con
fi
guration
Tasks
GitHub Trigger Scheduled
Trigger by JobA WebHook Trigger
Test
CD
CI
Test
Architecture
Jenkins-


Job A
Con
fi
guration
Tasks
Job B
Con
fi
guration
Tasks
Virtual Machine


Jenkins Executors
Bare Metal Server


Container


Dispatch Jobs
Installation
System packages


Ansible playbook


Container


Docker command


Kubernetes


Helm


Jenkins X


CD foudnation
Installation
Con
fi
guration


General


Source Code Control


Build Trigger


...


Tasks


Pipeline scripts


Shell scripts


....
Demo
https://jenkins.hwchiu.com


curl -X POST "https://hwchiu:xxxx@jenkins.hwchiu.com/job/
manually-pipeline-2/build?token=haha"
Common Issues
Server Con
fi
gurations


Job Con
fi
gurations


Manual


Tasks


Manual


VCS (Pipeline scripts)
Common Issues
How to audit ?


who made what change


How to manage the Jenkins ?


Similar jobs
JJB
Jenkins Job Builder


Takes simple descriptions of Jenkins jobs in YAML format.


Use them to con
fi
gure the Jenkins jobs


Of
fi
cial site


https://jenkins-job-builder.readthedocs.io/en/latest/
index.html
JJB
All jobs are de
fi
ned by YAML
fi
les.


How it works


Prepare an authentication
fi
le with Jenkins credentials.


Install JJB tool


De
fi
ne your jobs in YAML


Execute the JJB to update your jobs in Jenkins server
Architecture
Jenkins


Job A
Con
fi
guration
Tasks
Job B
Con
fi
guration
Tasks
YAML
fi
les


JJB tool
Update
Read and to XML
Authentication FIle
Jenkins.ini








Tools
pip install --user jenkins-job-builder




List Jobs
jenkins-jobs --conf jenkins.ini list







De
fi
ne Job
jenkins-jobs --conf jenkins.ini update job.yaml









Job-Template
Need several jobs de
fi
ned that are nearly identical


Use the job-template to de
fi
ne the skeleton of a job


Use the project to realize the job with appropriate variable
substitution.
Specify the job-template by id
Pipeline job
De
fi
ne the pipeline script in the repo.


Include the pipeline in Job's de
fi
nition.


Use one VCS for both JJB and pipeline scripts
Real Case
A large scale open source project


Involved industries


Software, Hardware, System Integrator


A single Jenkins system for all purposes

How to con
fi
gure the Jenkins?
Software
Would like to developing the software


Unit test


Integration test


Compatibility


Jenkins executors


Cloud


On-premises
Hardware
Would like to provide the hardware to support the software


Integration test


Compatibility


Jenkins executors


On-premises
System Integrator
Develop their own software to connect the open source and hardware


Integration test


Compatibility


Jenkins executors


Cloud


On-premises
Software
Jenkins Server


Jenkins Executor


Software
Company


Jenkins Executor


USA
Cloud(VM/Container)
Job A
Con
fi
guration
Tasks
De
fi
ne
Require variety software
Hardware
Jenkins Server


Jenkins Executor


Hardware
Company


Europe
Job A
Con
fi
guration
Tasks
De
fi
ne
SI
Jenkins Server


Jenkins Executor


SI


Jenkins Executor


India
Cloud(VM/Container)
Job A
Con
fi
guration
Tasks
De
fi
ne
Require variety software
Architecture
Jenkins Server


Jenkins Executor


Jenkins Executor


Jenkins Executor


Jenkins Executor


USA India Europe
Cloud
Jenkins Executor


Cloud
Issues
All people want to de
fi
ne their own jobs.


Including con
fi
guration, tasks and executor environment.


For con
fi
guration and task


A single repo with JJB
fi
les


For executor environment


Packer
JJB
We need a Jenkins Job to execute the JJB to update all Jenkins
Job


Chicken or the egg


Manually create a Jenkins Job for updating JJB


Then, ask that Jenkins Job to update Jenkins by JJB YAML.
Possible Scenario
Jenkins Server


Operator


Git Repo
Job A
Con
fi
guration
Tasks
JJB Yaml


JJB Yaml


JJB Yaml


JJB Yaml


Read
Create
fi
rst Job
Possible Scenario
Jenkins Server

 Git Repo
Job A
Con
fi
guration
Tasks
JJB Yaml


JJB Yaml


JJB Yaml


JJB Yaml


Jenkins Executor


Cloud(VM/Container)
Dispatch
JJB Update
Pull Code
Trigger Jenkins
2
1
3
4
Packer
HashiCorp Packer automates the creation of any type of machine image


It embraces modern con
fi
guration management


Automated scripts to install and con
fi
gure the software within your Packer-made images.


AWS/Azure/Docker...etc


AMI (build, push)


Docker (build + tag + push)


Prepare a Jenkins Job to perform the Packer to create the new image for Jenkins executor.
Possible Scenario
Jenkins Server

 Git Repo
Job A
Con
fi
guration
Tasks
JJB Yaml


JJB Yaml


JJB Yaml


JJB Yaml


Jenkins Executor


Cloud(VM/Container)
Dispatch
Pull Code
Trigger Jenkins
2
1
3
Cloud Provider


4
Build and Push
Global JJB
Provided by Linux Foundation


Lots of pre-de
fi
ned JJB
fi
les

Jenkins jobs to perform the JJB to update Jenkins


Jenkins jobs to perform Packer to create image


ci-management

Jenkins & IaC