This example implements Jenkins as a code using Jenkins Configuration as Code Plugin.
- Docker
- Linux / Mac machine with bash
- Understanding of configuring Jenkins controllers and agents
Objective of this demo is to demonstrate capability to run Jenkins controller and agent as docker image (instead running them on VM).
This example uses official Jenkins docker images jenkins/jenkins and jenkins/inbound-agent as base images.
Implemented features
- Script to create Jenkins controller docker image
- configuration of Jenkins through
jenkins.yaml- Two users setup
adminandreadonly(password same as username) - Access control for users
- Two jobs setup for running through JOB DSL Plugin
- Permanent node setup
docker-agent2 - Two views for
UIandMicroservices - Global credential setup for
gitusingssh
- Two users setup
- Installation of required plugins (
plugins.txt) - Script to create Jenkins Agent docker image
- Shell scripts to build run controller and agent on linux/mac
Start the Jenkins controller
docker run -p 8080:8080 -p 50000:50000 -e CASC_JENKINS_CONFIG=/usr/share/jenkins/ -e MY_JENKINS_URL="http://host.docker.internal:8080/" mandarnilange/jenkins-controller
Go to Jenkins web controller at http://localhost:8080, and identify secret for docker-agent2.
Start Jenkins Agent
docker run --init -e JENKINS_URL=http://host.docker.internal:8080 -e JENKINS_AGENT_NAME=docker-agent2 -e JENKINS_AGENT_WORKDIR=/home/jenkins/agent -e JENKINS_SECRET=<Secret from master> mandarnilange/jenkins-inbound-agent
Go to Jenkins controller web and confirm agent is connected and jobs created to confirm job installation.
In case, you want to build yourself from source and run locally then follow next section.
Following command will copy jenkins.yaml, copy InitialConfig.groovy, install plugins and build controller image mandarnilange/jenkins-controller
$ cd jenkins-controller
$ ./buildControllerImage.sh
Run controller image after setting up environemnt variables in controller.env (no changes needed in case run on Mac/linux)
CASC_JENKINS_CONFIGfor Configuration as Code plugin config file locationMY_JENKINS_URLto indicate host on which controller is runningSSH_PRIVATE_KEYset private key for access git/bitbucket private repository
$ ./startController.sh
Following will build agent image mandarnilange/jenkins-inbound-agent
$ cd jenkins-agent
$ ./buildAgentImage.sh
Run agent image after setting up environment variables in agent.env (no changes needed in case run on Mac/linux)
JENKINS_URLURL of controllerJENKINS_SECRETSecret to be taken fromdocker-agent2node configuration on clusterJENKINS_AGENT_NAMEName of agent configured in controllerJENKINS_AGENT_WORKDIRwork directory for agent
$ cd jenkins-agent
$ ./runAgent.sh
You can extend this implementation - some key next steps that can be taken
- Add more users: update
jenkins.yamland recreate image - Setup more jobs: update
jenkins.yamland recreate image - Setup external volume for persistent storage: update
startController.sh - Access private git repository: setup
SSH_PRIVATE_KEYbefore starting controller - Install additional plugins: udpate
plugins.txtand recreate image - Add more agents: update
jenkins.yamland recreate image
Happy Learning!
mandarnilange/jenkins-controller
mandarnilange/jenkins-inbound-agent