Jenkins is an automation server. Installing and configuring Jenkins itself shall be a fully automated process. That is the goal of auto-jenkins.
Currently, auto-jenkins does the following automatically:
- Set up a Jenkins master in a Docker container, with a configurable list of plugins installed
- Configure an SSH slave, which can be a physical host or any host accepting SSH connections
With the pipeline plugin (plugin id: workflow-aggregator), all the builds can be executed in a Docker container.
Docker is required on both the master host (host where the Jenkins master container is running) and the SSH slave. One physical host can be used both as the master host and as the SSH slave. For example, I ran the entire setup on a MacBook Pro, which is both the master host and the SSH slave.
All commands are run on the master host.
mkdir -p /private/var/tmp/jenkins_home_master/.ssh
chmod 755 /private/var/tmp/jenkins_home_master/.ssh
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f /private/var/tmp/jenkins_home_master/.ssh/id_rsa
ssh-keyscan slave.example.com >> /private/var/tmp/jenkins_home_master/.ssh/known_hosts
Allow SSH onto the slave1 using the generated key pair and prepare the work direcory for the Jenkins slave2
ssh-copy-id -f -i /private/var/tmp/jenkins_home_master/.ssh/id_rsa [email protected]
ssh -i /private/var/tmp/jenkins_home_master/.ssh/id_rsa [email protected] 'mkdir -p /private/var/tmp/jenkins_home_slave'
cat ./ansible/extra-vars.json
{
"jenkins_slave": {
"name": "ssh_slave",
"host": "slave.example.com",
"port": 22,
"path": "/private/var/tmp/jenkins_home_slave",
"label": "ssh_slave_label"
},
"pk_cred": {
"id": "pk_jenkins",
"user": "your_user",
"desc": "authorized key for ssh onto the slave host"
}
}
Build the Jenkins master Docker image3
packer build jenkins.json
cat ~/.auto-jenkins-env
PASSWORD=admin
USERNAME=admin
docker run -d -p 8080:8080 --env-file ~/.auto-jenkins-env -v /private/var/tmp/jenkins_home_master:/var/jenkins_home --rm --name auto-jenkins ouyi/auto-jenkins:0.0.1
Create a test pipeline job4
node('ssh_slave_label') {
echo 'Hello World'
withDockerContainer('alpine') {
git url: 'https://github.com/ouyi/test'
sh 'pwd && ls -lR && cat README.md'
}
}
- Deploy in AWS
- Multiple slaves
- Reverse proxy with nginx
Footnotes
-
Mac users might need to allow remote login ↩
-
Make sure the paths
/private/var/tmp/jenkins_home_master
and/private/var/tmp/jenkins_home_slave
are added to "File sharing" under Docker preferences. ↩ -
Packer is required on the master host ↩
-
The jenkins public key ([email protected]) shall be added as deploy key to GitHub repo ↩