Difference between revisions of "Jenkins/Install and Plugins"

From Ever changing code
Jump to navigation Jump to search
Line 61: Line 61:
*ansible
*ansible
*terraform
*terraform
== [https://jenkins.io/doc/book/managing/cli/ Jenkins CLI] ==
Download from http://jenkins-master:8080/cli, onto any server you wish to remotely control Jenkins
wget http://jenkins-master:8080/jnlpJars/jenkins-cli.jar


== Plugin: SSH Slaves plugin - adds slaves_nodes over ssh ==
== Plugin: SSH Slaves plugin - adds slaves_nodes over ssh ==

Revision as of 21:02, 16 September 2017

Install Jenkins

Pre-requisites: Java

Install Jenkins on Ubuntu from repository

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

Add repo to sources.list

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update && sudo apt-get install jenkins

Verify

cat /etc/passwd | grep jenkins #check if user has been created without shell
service jenkins status     #check if service is running

First run

  1. Password preview sudo vi /var/lib/jenkins/secrets/initialAdminPassword
  2. Url http://localhost:8080
  3. Select plugins you wish to install using web based wizard
  4. Once all downloaded, create your Admin user
  5. Once completed you will see: "Jenkins is ready! Your Jenkins setup is complete.", press Start Jenkins

Proxy if needed

If you run on restricted port system you cannot control the box firewall you may want to proxy your connection to Jenkins port :8080. You can install nGinx to do this for you

sudo apt-get install nginx
sudo vi /etc/nginx/sites-enabled  #then enable proxy_pass
   location / {
      proxy_pass http://127.0.0.1:8080;
      }

Configure ssh keys and permissions

Jenkins master node

wip.. change default jenkins user HOME directory ..verify

$ sudo hostnamectl set-hostname jenkins-master
$ sudo vi /etc/passwd                                             #make sure jenkins user hava access to shell
$ sudo mkdir -p  /tank/jenkins; usermod -d /tank/jenkins jenkins  #update default wierd jenkins HOME directory
$ sudo cp -r /etc/skel/. /tank/jenkins                            #copy .bashrc, .profile and other skeleton files
$ sudo passwd jenkins                   #set a password
$ sudo su jenkins                       #become jenkins user 
$ cd ~/.ssh && ssh-keygen               #generate ssh key pair
$ ssh-copy-id jenkins@localhost         #add public key to Jenkins (localhost) server, these might be minions workers
$ sudo visudo                           #allow jenkins sudo without password and disable requireTTY; makes easier to run builds
  jenkins ALL=(ALL)     NOPASSWD: ALL
  Defaults  !requiretty

The !requiretty will allow Jenkins to run bash scripts in a background otherwise you receive an error:

sudo: no tty present and no askpass program specified
Build step 'Execute shell' marked build as failure

Jenkins slave (minion) worker

There is no need to install anything apart allowing the master node to ssh to it

$ sudo hostnamectl set-hostname jenkins-minion
$ sudo mkdir -p  /tank/jenkins; usermod -d /tank/jenkins jenkins
$ sudo passwd jenkins            #set a password, may mach the master node one but not necessary
$ sudo visudo                    #allow jenkins run sudo without password prompt; makes easier to run builds
  jenkins ALL=(ALL)     NOPASSWD: ALL
jenkins@master $ ssh-copy-id jenkins@slave  #allow password-less ssh to slave nodes, so master can send jobs to it
jenkins@slave  $ ssh-copy-id ubuntu@boxes   #allow password-less ssh to all servers that Jenkins will be building/configuring

If you have any plugins on Jenkins-master node that require external programs to run you should install these tools also on slaves. As all jobs are send to slaves these external programs should be there, most common are:

  • git
  • java - installed automatically but you need to have Oracle Java account credentials set up in the wide configuration settings. You can also install Java yourself.
  • ansible
  • terraform

Jenkins CLI

Download from http://jenkins-master:8080/cli, onto any server you wish to remotely control Jenkins

wget http://jenkins-master:8080/jnlpJars/jenkins-cli.jar


Plugin: SSH Slaves plugin - adds slaves_nodes over ssh

This plugin allows you to manage slaves running on *nix machines over SSH. It adds a new type of slave launch method.

On the worker, create a workspace folder for jobs

$ mkdir ~/jenkins-minion_workspace

Go to Jenkins-master console and add this New Node

  1. Go to http://jenkins-master:8080
  2. Manage Jenkins > Manage Nodes > New Node with permanent agent and fill in with details
Jenkins-master-add-new-slave

Add credentials, then select them on the main new node screen and save configuration.

Jenkins-minion-connection-credentials

If you have Java installed on the remote worker node, all you need to do is to Launch Agent. You should see that the worker is in service and it's time in sync

Jenkins-slaves-node-list

Plugin: Self-Organizing Swarm Plug-in Modules

This plugin enables slaves to auto-discover nearby Jenkins master and join it automatically, thereby forming an ad-hoc cluster. This plugin consists of two pieces:

  • self-contained CLI client that discovers a nearby Jenkins (via a UDP broadcast) and joins it
  • A plugin that needs to be installed on Jenkins master to accept swarm clients

Download a client

wget https://repo.jenkins-ci.org/releases/org/jenkins-ci/plugins/swarm-client/3.3/swarm-client-3.3.jar

Run as anonymous user, in this case you need to enable Agent privileges on Jenkins-master node http://jenkins-master:8080/configureSecurity/ > Configure Global Security > Matrix-based security > all Agent permissions.

java -jar swarm-client-3.3.jar -name minion1 -master http://jenkins-master:8080/

Available options:

-name VAL :name of the slave
-username VAL :the Jenkins username for authentication
-password VAL :the Jenkins user password
-passwordEnvVariable VAL :environment variable that the password is stored in                                                                       

When connected you should see on slave's

jenkins@jenkins-minion-2:~$ java -jar swarm-client-3.4a.jar -master http://jenkins-master:8080/ -name minion-2
Sep 16, 2017 6:25:03 PM hudson.plugins.swarm.Client main
INFO: Client.main invoked with: [-master http://jenkins-master:8080/ -name minion-2]
Sep 16, 2017 6:25:03 PM hudson.plugins.swarm.Client run
INFO: Discovering Jenkins master
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 16, 2017 6:25:03 PM hudson.plugins.swarm.Client run
INFO: Attempting to connect to http://jenkins-master:8080/ 8d17dd57-bef9-4be9-9633-4f00dfe45ff2 with ID 16252dd0
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main createEngine
INFO: Setting up slave: minion-2-16252dd0
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener <init>
INFO: Jenkins agent is running in headless mode.
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://jenkins-master:8080/]
Sep 16, 2017 6:25:04 PM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting server accepts the following protocols: [JNLP4-connect, CLI2-connect, JNLP-connect, Ping, CLI-connect, JNLP2-connect]
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Agent discovery successful
  Agent address: jenkins-master
  Agent port:    40843
  Identity:      6c:9c:65:9d:4a:a3:0a:ee:1d:c4:f9:ad:a2:1b:f8:51
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Handshaking
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connecting to jenkins-master:40843
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Trying protocol: JNLP4-connect
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Remote identity confirmed: 6c:9c:65:9d:4a:a3:0a:ee:1d:c4:f9:ad:a2:1b:f8:51
Sep 16, 2017 6:25:04 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected

Plugin: Ssh plugin

This plugin allows to run shell commands on a remote machine via ssh. It provides Execute shell script on remote host using ssh option in job build section.

Install the plugin

  1. Install ssh plugin from Manage Jenkins > Manage Plugins > click on Available tab and search for ssh
  2. Configure remote hosts that Jenkins can connect goto Manage Jenkins > Configure System and fill in SSH remote hosts section with similar
Jenkins-ssh-remote-hosts

Add remote ssh commands project

Once ssh remote hosts are added you can create a new project and in Build section you can choose Execute shell script on remote host using ssh. You can choose remote host from drop down list that contains hosts that you configured in the previous step.

Post build actions

There is whole bunch options you can do after your build has been successful. These can be picked up in your project > Configure > Post-build Actions section from drop down menu

  • Build other projects
  • Email notification

Plugin: Schedule build

Use Schedule Build Plugin for this.