Table of Contents
- Jenkins CLI
- REST API
- SSH Plugin
- Jmeter plugin
- Ansible plugin
- Jenkins Agents
- Backup Jenkins
- Automating ansible installation
Jenkins CLI
https://www.jenkins.io/doc/book/managing/cli/
Download jenkins-cli.jar from jenkins >> manage >> cli
Verify java installation
$ java --version
openjdk 21.0.5 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)
Install java if necessary
sudo dnf install javaTry jenkins cli
java -jar jenkins-cli.jar -s http://18.157.165.32:8080/ helpGet a list of jobs
$ java -jar jenkins-cli.jar -s http://18.157.165.32:8080/ \
-auth admin:<password> list-jobs
Restart jenkins
$ java -jar jenkins-cli.jar -s http://18.157.165.32:8080/ \
-auth admin:<password> restart
For security create a Security | API Token
Example
java -jar jenkins-cli.jar -s http://localhost:8080/ \
-auth myuser:11a2b3c4d5e6f7g8h9i0jklmno \
build MyPipelineJob -p ENV=production -p VERSION=1.2.3
java -jar jenkins-cli.jar -s http://localhost:8080/ \
-auth myuser:11a2b3c4d5e6f7g8h9i0jklmno \
console MyPipelineJob #123
REST API
Example:
http://18.157.165.32:8080/api/
Get information
curl JENKINS_URL/job/JOB_NAME/buildWithParameters \
--user USER:TOKEN \
--data id=123 --data verbosity=high
Example:
curl -s http://18.157.165.32:8080/api/json \
--user admin:<password> | jq
curl -s http://18.157.165.32:8080/api/json \
--user admin:<password> | jq .url
SSH Plugin
- Add SSH Username with private key
- Create a project that add SSH Agent
- SSH remote server
Jmeter plugin
https://www.jenkins.io/doc/book/using/using-jmeter-with-jenkins/
Use jmeter plans on performance/
Ansible plugin
- Install python3
- Install python3-pip
- Install ansible-core
Modify Dockerfile to add python and rebuild jenkins image
FROM jenkins/jenkins:latest-jdk21
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y \
docker-ce-cli \
python3 \
python3-pip \
python3-dev
USER jenkins
# Set the default Python version
RUN ln -s /usr/bin/python3 /usr/bin/python
# Verify Python and pip installation
RUN python --version && pip3 --version
Jenkins Agents
docker run -d --name jenkins-agent \
-e JENKINS_URL=http://<JENKINS_MASTER_URL>:8080 \
-e JENKINS_AGENT_NAME=agent-name \
-e JENKINS_SECRET=<SECRET> \
-e JENKINS_AGENT_WORKDIR=/home/jenkins/agent \
jenkins/inbound-agentTokens can be obtain from Manage Jenkins >> Nodes >> New Node
Example:
docker run -d --name jenkins-agent007 \
-e JENKINS_URL=http://172.31.19.130:8080 \
-e JENKINS_AGENT_NAME=agent007 \
-e JENKINS_SECRET=7464a64c7d1b21bf70cc69f7ba1b80f4ebaf5c925af2348bb9f6b9982c9c5c64 \
-e JENKINS_AGENT_WORKDIR=/home/jenkins/agent \
jenkins/inbound-agentHow to use agents
pipeline {
agent { label 'linux' } // Force the whole pipeline to 'my-agent' node
stages {
stage('Build') {
steps {
echo 'Building on the specified agent'
}
}
stage('Test') {
steps {
echo 'Testing on the specified agent'
}
}
}
}
With no default agent
pipeline {
agent none // No global agent, each will define its own agent
stages {
stage('Build') {
agent { label 'build-agent' } // Force 'Build' to run 'build-agent'
steps {
echo 'Building on the build-agent'
}
}
stage('Test') {
agent { label 'test-agent' } // Force 'Test' to run 'test-agent'
steps {
echo 'Testing on the test-agent'
}
}
stage('Deploy') {
agent { label 'deploy-agent' } // Force 'Deploy' to run 'deploy-agent'
steps {
echo 'Deploying on the deploy-agent'
}
}
}
}
Backup Jenkins
Various schemes can be used to create backups. These are discussed in this section:
- Filesystem snapshots
- Plugins for backup
- Write a shell script that backs up the Jenkins controller
On Controller export nfs server
/etc/exports
/mnt/ 172.31.21.101/24(rw,sync,no_root_squash)Start nfs server
sudo systemctl restart nfs-server.service
On jenkins master mount backup dir
sudo mount -t nfs 172.31.16.118:/mnt /mnt
Create a backup
sudo tar czvf /mnt/backup-jenkins-$(date +'%F').tgz \
/var/lib/docker/volumes/jenkins-data/_dataNOTE: If necessary, add automatic backup to crontab
Automating ansible installation
$ pip install ansible-navigator
Create ansible.cfg
[defaults]
inventory = hosts
collections_path = ./collections
interpreter_python = auto_silent
host_key_checking = False
remote_user = redhat
private_key_file = ~/.ssh/id_rsa
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = falseCreate hosts
[nodes]
jenkins-cfreire-ctrl ansible_host=172.31.16.118
jenkins-cfreire-jk ansible_host=172.31.21.101
jenkins-vneves-ctrl ansible_host=172.31.21.55
jenkins-vneves-jk ansible_host=172.31.17.222
jenkins-asousa-ctrl ansible_host=172.31.17.74
jenkins-asousa-jk ansible_host=172.31.19.154
[cfreire]
jenkins-cfreire-ctrl
jenkins-cfreire-jk
[jenkins]
jenkins-cfreire-jk
Requirements
ansible-galaxy collection install community.general
ansible-galaxy collection install community.docker
Deploy jenkins
jenkins_deploy.yml
---
- name: Deploy Jenkins in docker
hosts: jenkins
gather_facts: false
vars:
timezone: Europe/Lisbon
jenkins_image: my_jenkins
jenkins_container: jenkins-blueocean
user: redhat
tasks:
- name: Set timezone
community.general.timezone:
name: "{{ timezone }}"
- name: Create jenkins directory
ansible.builtin.file:
path: /home/{{ user }}/{{ jenkins_image }}/
state: directory
mode: '0755'
- name: Copy Dockerfile
ansible.builtin.copy:
src: files/Dockerfile
dest: /home/{{ user }}/{{ jenkins_image }}/Dockerfile
mode: '0644'
- name: Build {{ jenkins_image }} image
community.docker.docker_image_build:
name: "{{ jenkins_image }}"
path: /home/{{ user }}/{{ jenkins_image }}/
dockerfile: Dockerfile
- name: Create jenkins network
community.docker.docker_network:
name: jenkins
- name: Start jenkins-docker container
community.docker.docker_container:
name: jenkins-docker
image: docker:dind
state: started
detach: yes
privileged: yes
networks:
- name: jenkins
aliases:
- docker
env:
DOCKER_TLS_CERTDIR: /certs
volumes:
- jenkins-docker-certs:/certs/client
- jenkins-data:/var/jenkins_home
published_ports:
- "2376:2376"
command: --storage-driver overlay2
restart_policy: always
- name: Start jenkins-blueocean container
community.docker.docker_container:
name: "{{ jenkins_container }}"
image: "{{ jenkins_image }}"
state: started
detach: yes
restart_policy: on-failure
networks:
- name: jenkins
env:
DOCKER_HOST: tcp://docker:2376
DOCKER_CERT_PATH: /certs/client
DOCKER_TLS_VERIFY: '1'
published_ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins-data:/var/jenkins_home
- jenkins-docker-certs:/certs/client:ro
- name: Wait for 30 seconds
ansible.builtin.pause:
seconds: 30
- name: Extract jenkins password from docker container
community.docker.docker_container_exec:
container: "{{ jenkins_container }}"
command: cat /var/jenkins_home/secrets/initialAdminPassword
register: result
tags: password
- name: Print jenkins password
ansible.builtin.debug:
msg: " {{ inventory_hostname }} | password: {{ result.stdout }} "
register: debug_output
tags: password
- name: Write passwords to a file
local_action: copy content="{{ debug_output.msg }}" dest="password.txt"
tags: password
files/Dockerfile
FROM jenkins/jenkins:2.479.2-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
ansible-playbook jenkins_deploy.yml