jenkins (CJE) Exercise Guide

#######################################################################
Exercise: Install a Jenkins Master and Prerequisites

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Basic Instructions

Download the appropriate java jdk version from the Oracle website. For this course we use jdk-8u121.
Copy the package from your local environment to the target server.
Install and configure Java (jdk-8u121)
Install Jenkins (version 2.19.4)
Complete the Install Wizard.

Prerequisite Install

Go to: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Download the appropriate java jdk version from the Oracle website. For this course we use jdk-8u121.
Copy the package from your local environment to the target server.
Below is an example using scp:
scp jdk-8u121-linux-x64.rpm user@your-server:/home/user/
Install the jdk package.
rpm -Uvh jdk-8u121-linux-x64.rpm
Setup Alternatives for Java:
alternatives –install /usr/bin/java java /usr/java/latest/bin/java 200000
alternatives –install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
alternatives –install /usr/bin/jar jar /usr/java/latest/bin/jar 200000
Note: Check out Terry’s nugget on this for more detail:
“Setting local and global java environment variables”
Set JAVA_HOME environmental variable in rc.local.
vi /etc/rc.local

export JAVA_HOME=”/usr/java/latest”
Jenkins Install

Add the Jenkins repo to your yum sources on the CentOS node.
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
Import the Jenkins rpm signing key.
rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
Install the Jenkins package.
We will be using version 2.19.4 which is the target version for the certification test.
yum install -y jenkins-2.19.4-1.1
Check for services running on 8080 before starting the Jenkins service.
netstat -tulpn | grep 8080
If nothing is running on 8080, go ahead and start the service via systemctl.
systemctl start jenkins
Also, enable the Jenkins service so it starts on system startup.
systemctl enable jenkins
Check again for services running on port 8080.
There will be a slight delay, so we’ll use watch to wait for the signal.
watch n=1 “netstat -tulpn | grep 8080”
ctrl-c to break the watch when the service is shown to be running on 8080
Visit the web gui at:
http://your-server-fqdn:8080/
You’ll be prompted for the initialAdminPassword which is located in /var/lib/jenkins/secrets/initialAdminPassword on the system being configured. You’ll want to cat that and copy and paste it into the browser.
cat /var/lib/jenkins/secrets/initialAdminPassword
Paste the password into Install Wizard.
Choose “Install Suggested Plugins.”
Set admin settings, user, password, etc…
Press “Enter.”
Click “Start Using Jenkins.”
Now you have your Jenkins Master up and running!

#######################################################################
Exercise: Configuring Matrix-based Security

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Add a ‘developer’ user
Setup Matrix-based security in “Configure Global Security”
Give all permissions to the admin user.
Give read permissions to Anonymous.
Give all permissions except “delete” and “administer” to the “developer” user.
Prerequisite

Must have a configured Jenkins Master
Procedure

Go to the Jenkins Dashboard.
http://your-server:8080
Click “Manage Jenkins.”
Click “Manage Users”
Click “Create User”
Create the ‘developer’ user.
Click back to “Manage Jenkins.”
Click “Configure Global Security.”
Check “Enable Security.”
Select “Jenkins’ own user database.”
Place a check mark next to “Allow users to sign up.”
Select “Matrix-based Security.”
Select the admin user and check all the permissions.
Set “Read” permissions for “Anonymous.”
Set the ‘developer’ user and select all permissions except Administer and “Delete” permissions.
Click Save.
###################################################
Exercise: Add a Jenkins Slave

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisites:

Spin up another lab instance for a slave
Have an RSA key pair generated for the Jenkins User on the Master
Basic Instructions

Set up Java (jdk-8u121) like you did on the master previously.
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Add a Jenkins User and home to the slave node.
Coply the Jenkins Master pub key to the Slave nodes authorized keys.
Add a Node in the Jenkins Dashboard.
Set the appropriate configuration items, including the node’s fqdn.
Use “Private key” “From Jenkins Master” for credentials.
Ensure the agent is available for use from the Node view. (no red ‘x’)

 

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisites

Spin up the node (cloud instance, or hardware).
Install and configure Java as described in the Install Prerequisite section.
Generate an RSA key pair for the Jenkins user on the Jenkins Master.
JAVA SETUP ON SLAVE

Go to: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Download the appropriate java jdk version from the Oracle website. For this course we use jdk-8u121.
Copy the package from your local environment to the target server.
Below is an example using scp:
scp jdk-8u121-linux-x64.rpm user@your-server:/home/user/
Install the jdk package.
rpm -Uvh jdk-8u121-linux-x64.rpm
Setup Alternatives for Java:
alternatives –install /usr/bin/java java /usr/java/latest/bin/java 200000
alternatives –install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
alternatives –install /usr/bin/jar jar /usr/java/latest/bin/jar 200000
Note: Check out Terry’s nugget on this for more detail:
“Setting local and global java environment variables”
Set JAVA_HOME environmental variable in rc.local.
vi /etc/rc.local
export JAVA_HOME=”/usr/java/latest”
Jenkins Slave Setup

FROM THE TARGET SLAVE NODE’S CONSOLE

Switch to the “root” user.
sudo su
Add a jenkins user with the home “/var/lib/jenkins”.
useradd -d /var/lib/jenkins jenkins
FROM THE JENKINS MASTER

Copy the id_rsa.pub key from the Jenkins user on the master.
cat /var/lib/jenkins/.ssh/id_rsa.pub
FROM THE TARGET SLAVE NODE’S CONSOLE

Create an authorized_keys file for the Jenkins user.
mkdir /var/lib/jenkins/.ssh
vi /var/lib/jenkins/.ssh/authorized_keys
Paste the key from the Jenkins master into the file vim. Save with “:wq”.
FROM THE JENKINS DASHBOARD

Click “Manage Jenkins” from the left panel.
Click “Manage Nodes.”
Click “Add Node.”
Set a name for your node (e.g. “Slave 1”).
Select “Permanent Node.”
Set “Remote root directory” to ‘/var/lib/jenkins.’
Set “Usage” to “Use this node as much as possible.”
Set “Launch Method” to “Launch slave agents via SSH.”
Set “Host” to your nodes fqdn (e.g. brandon4232.mylabserver.com).
Select “Add” under “Credentials.”
Set “Kind” to “SSH Username with private key.”
Set “Username” to “jenkins.”
Set “Private key” to “From the Jenkins Master.”
Click “Add.”
Choose the new credential from the “Credentials” dropdown.
Click “Save.”
The agent should now be available for use.

##################################################################
Exercise: Working with the Plugin Manager

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisites:

You must have a configured Jenkins Master.
Basic Instructions

Install the thinBackup plugin from an hpi file.
http://updates.jenkins-ci.org/download/plugins/thinBackup/1.8/thinBackup.hpi
Use the Plugin Manager to Upgrade thinBackup to the latest version.
Use the Plugin Manager to Downgrade thinBackup back to version 1.8.
Use the Plugin Manager to remove thinBackup.

Installing thinBackup 1.8 from a .hpi File

Download the file from this link:
http://updates.jenkins-ci.org/download/plugins/thinBackup/1.8/thinBackup.hpi
From the Dashboard, click “Manage Jenkins.”
Click “Manage Plugins.”
Click the “Advanced” tab.
In the “Upload Plugin” section, use the “Choose File” button to navigate to your hpi file.
Install it.
Upgrade thinBackup to the Latest Version

From the Dashboard, click “Manage Jenkins.”
Click “Manage Plugins.”
Click the “Updates” tab.
Check the box next to thinBackup.
Click “Download now and install after restart.”
Downgrade thinBackup Back to 1.8

From the Dashboard, click “Manage Jenkins.”
Click “Manage Plugins.”
Click the “Installed” tab.
Click the “Downgrade to 1.8” button for thinBackup.
Uninstall thinBackup

From the Dashboard, click “Manage Jenkins.”
Click “Manage Plugins.”
Click the “Installed” tab.
Click the “Uninstall” button for thinBackup.
#########################################################################
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Add a new Freestyle Project, called “Parametererized Project Madlibs.”
Add three string parameters:
NAME
ADJECTIVE
PLURAL_NOUN
Add a Build step that echoes the environment variable “JOB_NAME”, and then echoes the sentence “${NAME} has ${ADJECTIVE} ${PLURAL_NOUN}!” (e.g “Brandon has bad jokes!”)
Trigger the build with parameters.
############
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Procedure

From the Jenkins Dashboard, click “New Item” in the left panel.
Add the name “Parametererized Project Madlibs”.
Select “Freestyle Project”
Click “OK”
Check the “This project is parameterized” check box in the “General” config secion
Add three string parameters by clicking “Add Parameter” and selecting “String Parameter” from the list three times.
7.Set the name of the three parameters as follows:
NAME
ADJECTIVE
PLURAL_NOUN
Add a Build step that looks like this:
echo $JOB_NAME
echo “${NAME} has ${ADJECTIVE} ${PLURAL_NOUN}”
Click “Save”
Click “Build with Parameters”
Make up your own words to pass. (e.g. “Brandon”, “bad”, “jokes”)
Click “Build”
############################################################
Exercise: Build a Simple Pipeline without SCM

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Add a new Pipeline project called “Pipeline without an SCM”
Use the Jenkinsfile syntax in the Pipeline definition field to accomplish the following:
Use the ‘master’ agent
Add three stages; “PRINT”, “WRITE”, “READ”
In “PRINT”, add a step to echo the job name
In “WRITE”, write the build number to a file called “build_number” in the workspace.
In “READ”, read the build number from the file.
Make a post section and archive and fingerprint the file created in the “WRITE” stage in it.
Trigger a Project Build and review the console output and generated Artifact.

####
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Procedure

From the Jenkins Dashboard, select “New Item”.
Enter “Pipeline without an SCM” for the name.
Select “Pipeline”
Click “OK”
Scroll down to the Pipeline section.
Your Jenkinsfile should look like the following:
pipeline {
agent {
label ‘master’
}
stages {
stage(‘PRINT’) {
steps {
sh ‘echo $JOB_NAME’
}
}
stage(‘WRITE’) {
steps {
sh ‘echo $BUILD_NUMBER >> build_number’
}
}
stage(‘READ’) {
steps {
sh ‘cat build_number’
}
}
}
post {
success {
archiveArtifacts artifacts: ‘build_number’, fingerprint: true
}
}
}
Click “Save”
Click “Build Now”
Click the build number when it shows in the Executor view on the left.
Follow the console ouptput to view the output of your pipeline.
Click back on the “Build View” and ensure your artifact has been created.
##########################################################
Exercise: Configure Notifications in a Pipeline
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Configure your SMTP server for “aspmx.l.google.com”.
Add a new pipeline project called Notification Pipeline.
Use the pipeline definition field to add a one stage/one step pipeline that prints “Notification Time!”.
Add a post directive to the Jenkinsfile that sends a notification to yourself whenever the stages complete successfully.
Make the Notification look like this:
Subject: “Notification Pipeline [Build Number] Ran!”
body: “Notification Pipeline [Build Number] Ran!”
“Check console output at Notification Pipeline [Build Number]”
Run the build and see if you get an email.
###########
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Procedure

From the Jenkins Dashboard, select “Manage Jenkins”.
Select “Configure System”
Scroll to “Extended E-mail Notification”
Set “SMTP Server” to “aspmx.l.google.com”
Click “Advanced”
Set “SMTP Port” to “25”.
Click “Save.”
From the Jenkins Dashboard, select “New Item”.
Enter “Notification Pipeline” for the name.
Select “Pipeline”
Click “OK”
Scroll down to the Pipeline section.
Your Jenkinsfile should look like the following:
pipeline {
agent {
label ‘master’
}
stages {
stage(‘Greeting’) {
steps {
sh ‘echo “Notification Time!”‘
}
}
}
post {
success {
emailext(
subject: “${env.JOB_NAME} [${env.BUILD_NUMBER}] Ran!”,
body: “””<p>’${env.JOB_NAME} [${env.BUILD_NUMBER}]’ Ran!”:</p>
<p>Check console output at <a href=’${env.BUILD_URL}’>${env.JOB_NAME} [${env.BUILD_NUMBER}]/a></p>”””,
to: “your@email.com”
)
}
}
}
Click “Save”
Click “Build Now”
Click the build number when it shows in the Executor view on the left.
Follow the console ouptput to view the output of your pipeline.
Check for an email (it may have been filtered to spam).
########################################################
Exercise: Using the Jenkins CLI
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Setup an RSA key pair if necessary
Download the jenkins-cli.jar file from the Jenkins master
Set an alias and environment variable for the Jenkins CLI.
Run the following commands:
who-ami
help
version
Install the thinBackup plugin via the Jenkins CLI.
View the console output for any job via the Jenkins CLI.
###########
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Setup a Pub Key for Your User

Generate a rsa key pair for your user.
Go to:
http://yourserver.com/me/configure
Copy the contents of your id_rsa.pub file into the “SSH Public Keys” field.
Download the CLI Jar file

wget -P /var/lib/jenkins/ http://:8080/jnlpJars/jenkins-cli.jar
Setting Up your Environment

Set the JENKINS_URL environment variable.
echo “JENKINS_URL=’http://localhost:8080′&#8221; >> /etc/environment
Set an alias for your bash shell.
echo “alias jenkins-cli=’java -jar /var/lib/jenkins/jenkins-cli.jar'” >> ~/.bashrc
Logout and log back in.
The Jenkins CLI can now be executed via jenkins-cli
Run a Few Commands

Run ‘who-am-i’
jenkins-cli who-am-i
Run ‘help’
jenkins-cli help
Run ‘version’
jenkins-cli version
Install ‘thinBackup’
jenkins-cli install-plugin thinBackup
View the console output from a build.
//varies, here’s an example
jenkins-cli console “Freestyles/My Freestyle Project” 51

###################################

Exercise: Using the Jenkins REST API

NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Basic Instructions

Retrive the Administrative user’s API token
User curl and Jenkins’ REST API for the following actions:
Trigger a job build
Retrieve the config.xml file for a Project
Disable a Project
Enable a Project
##########
NOTE:

Be sure you are using the same server version and distribution from the course for these topics.
Prerequisite

Must have a configured Jenkins Master
Procedure

From the Jenkins Dashboard, click “Manage Jenkins.
Click “Manage Users”.
Click your the configure button (gear-shaped) next to your administrative user.
Click the “API Token” button.
With your username, and the api token, the basic curl syntax will look as follows:
curl http://<your-server&gt;:8080/<some REST endpoint> –user <username>:<api token>
Trigger a build:
//example
curl -X POST http://brandon4231.mylabserver.com:8080/job/Freestyles/job/My%20Freestyle%20Project/build –user brandon:acaab63811fab6a5990eb7c6904bb6bf
Retrieve a project config.xml file
//example
curl http://brandon4231.mylabserver.com:8080/job/Freestyles/job/My%20Freestyle%20Project/config.xml –user brandon:acaab63811fab6a5990eb7c6904bb6bf
Disable a project
//example
curl -X POST http://brandon4231.mylabserver.com:8080/job/Freestyles/job/My%20Freestyle%20Project/disable –user brandon:acaab63811fab6a5990eb7c6904bb6bf
Enable a project
//example
curl -X POST http://brandon4231.mylabserver.com:8080/job/Freestyles/job/My%20Freestyle%20Project/enable –user brandon:acaab63811fab6a5990eb7c6904bb6bf

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s