Jenkins 2 tips

Set up Jenkins with Java WAR file

http://mirrors.jenkins-ci.org/war-stable/

mkdir -p /opt/jenkins_home
export JENKINS_HOME=/opt/jenkins_home

Computer > Properties > Advanced System Setting > Advanced > Environment Variables > System Variables
Add the variable JENKINS_HOME with value C:\jenkins_home

Set up Jenkins on RedHat, CentOS with RPM

export JAVA_HOME=/opt/jdk/jdk1.8.0_112
export JENKINS_HOME=/var/lib/jenkins
export PATH=$JAVA_HOME/bin:$PATH

wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins

chkconfig jenkins on
service jenkins start
# systemctl start jenkins.service

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload
firewall-cmd --list-all

cat /var/lib/jenkins/secrets/initialAdminPassword
# get the random password and change it

http://localhost:8080

Manage Jenkins > Configure Global Security

Check: Enable security
Check: Project-based Matrix Authorization Strategy
Create a user "admin" in: User/group to add:
Check All rules for the user
Save

Sign up a user with username "admin" above.
Log in with "admin".

Add nodes
Jenkins nodes must have Java 8 or 9 installed.

Linux nodes with JNLP

- http://localhost:8080/configureSecurity/
- Jenkins->Global Security->TCP port for JNLP agents : 41357
- Then run

firewall-cmd --zone=public --add-port=41357/tcp --permanent
firewall-cmd --reload

Windows nodes with JNLP

- Open a command window with "Run as Administrator" (press Windows + R, type "cmd", right click the cmd.exe, choose Run As...).
- Change directory to the place where you saved the slave-agent.jnlp file.
- At the prompt run "slave-agent.jnlp"
- Now with the running slave click the menu item to "Install as Service"
- By the defaut, Jenkins slave will run with Local System account, you might need to change to your account in the Log On option

- For network drives mapped
Example case:
+ Windows: master Jenkins
+ Linux: Jenkins slave and Samba share directory

# Example samba config in /etc/samba/smb.conf
# Change TUAN to your account ID
# Make sure that you you the same password for account TUAN on both Windows and Linux
[TUAN_samba]
path = /home/tuan/samba
writeable = yes
valid users = TUAN

+ Windows master has to mount the samba drive with Admin right
- Open a command window with "Run as Administrator" (press Windows + R, type "cmd", right click the cmd.exe, choose Run As...).
- Mount the Samba drive like this

# Change TUAN to your account ID
net use Z: "\\LINUX-HOST\TUAN_samba" /persistent:yes /USER:TUAN

- You might also have to include above mounting command in your Jenkins shell script

Windows master without git
Assume you want run Jenkins on Windows as master and Linux as slaves.

You don't want to install git on Windows, but Linux slaves.

Go to "Home > Manage Jenkins > Global Tool Configuration" or just http://JENKINS_URL:8080/configureTools/
Change "git.exe" to "git", just ignore the red texts ("There's no such executable git...")

Pipeline call job with parameters

stage('test stage') {

    // Parallel example
    parallel(
        Suse: { node('suse'){
        sh '''
            uname -a
        '''
        } },
        Centos: {
               // https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
               build (job : 'my_test_job',
                   propagate: false,
                   parameters: [
                                 [$class: 'StringParameterValue',
                                 name: 'CMD',
                                 value: 'uname -a'],
                                 [$class: 'StringParameterValue',
                                 name: 'CMD2',
                                 value: 'ls -la']
                               ]
            )
        },
        Centos2: {
               // https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
               build (job : 'my_test_job',
                   propagate: false,
                   parameters: [[$class: 'StringParameterValue',
                                 name: 'CMD',
                                 value: '#!/bin/bash -l \\ && uname -a']]
            )
        },
        Windows: { node('windows'){

        bat '''
            net use
        '''

        } }
        )
    // Parallel example end

    // Customizing build display texts
    // https://stackoverflow.com/questions/44817027/how-to-graphically-visualize-tag-build-branch-in-jenkins

       script {
                currentBuild.displayName = "#${currentBuild.getNumber()} - master"
                currentBuild.description = 'Final Release'
               }

   // Show all Jenkins variables which you can use in this job or pipeline
    node ('suse') {
        sh '''
            printenv
        '''
    }
    node ('windows') {
        bat '''
            printenv
        '''
    }    

}

Extended E-mail Notification and its templates

Installing Extended E-mail Notification and its template:
- https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
- https://wiki.jenkins.io/display/JENKINS/Email-ext+Template+Plugin

To use the templates for email-ext, download the example template at
- https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template

Create a directory name "email-templates" in your JENKINS_HOME directory, put the "groovy-html.template" there.

On Jenkins home page => Manage Jenkins => Editable Email Template Management. Or just click http://localhost:8080/emailexttemplates

Add New Template => Default Content

${SCRIPT, template="groovy-html.template"}

To use a environment variable in the template, insert this into groovy-html.template

BUILD_URL is your environment varialbe
buildURL is your Groovy variable and is used in the template

<p>Some HTML...</p>

<% def buildURL = build.getEnvironment(listener).get('BUILD_URL') %>
<p>This is my BUILD_URL env varibale: <b>${buildURL}</b></p>

<p>Some HTML...</p>

You can also escape HTML characters also (i.e convert < to '& l t ;')

<p>Some HTML with special characters...</p>

<% def MY_HTML = build.getEnvironment(listener).get('MY_HTML_ENV')
MY_HTML = org.apache.commons.lang.StringEscapeUtils.escapeHtml(MY_HTML)
%>
<p>This is my MY_HTML_ENV env varibale: <b>${MY_HTML}</b></p>

<p>Some HTML...</p>

To include images into your mails

Put the images into this Jenkins directory: $JENKINS_HOME/war/images/ and then restart Jenkins service.

And insert this into groovy-html.template. Note that ${rooturl}static/e59dfe28/images is constant.

<p>Some HTML...</p>

<img src="${rooturl}static/e59dfe28/images/YOUR_IMAGE.jpg"/>

<p>Some HTML...</p>

Fix Content Security Policy in Jenkins

Go to “Manage Jenkins” -> “Script console” and run below command:

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

Loading