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 > Environmanet 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 templete, insert this into groovy-html.template

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

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

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

Loading