jenkins - ghdrako/doc_snipets GitHub Wiki

Installing

Instaling with Ansible

# role for stable installation of the Jenkins
$ ansible-galaxy install opstree_devops.jenkins

Create a “hosts” file in the directory where you have cloned this ansible role. Define the server connection details where Jenkins should be installed and configured.

[jenkins]
10.1.1.100 ansible_ssh_user=ubuntu
ansible_ssh_private_key_file=<file_path>
Create a file named site.yml:
---
- hosts: jenkins
become: yes
roles:
-opstree_devops.jenkins

Once these two files are created, we can run the ansible by using this command:

$ ansible-playbook -i hosts site.yml

Some features supported by this role are as follows:

  • Plugin installation while installing Jenkins
  • Credentials management
  • Global tool configuration
  • Default package installation

Installing Jenkins with Docker

$ docker run -p <host_port>:8080 -v <host_volume>:/var/jenkins_home jenkins/jenkins

We need to specify the following parameters

  • host_port - The port on which Jenkins is visible outside of the container
  • host_volume - This specifies the directory where the Jenkins home is mapped. It needs to be specified as volume because it contains the configuration, pipeline builds, and log
  1. Prepare the volume directory $ mkdir $HOME/jenkins_home
  2. Run the Jenkins container:
$ docker run -d -p 9090:8080 \
 -v $HOME/jenkins_home:/var/jenkins_home \ 
 --name jenkins jenkins/jenkins

# powershell
docker run -d -p 9090:8080 `
 -v C:\Projects\docker\jenkins\jenkins_home:/var/jenkins_home `
 --name jenkins jenkins/jenkins


  1. Check whether Jenkins is running $ docker logs jenkins

After performing these steps, you can access your Jenkins instance at http://localhost:8080/

Job

A job is set of tasks/subtasks implemented as a sequential process to perform different phases of the build release lifecycle.

It will typically include the following steps:

  1. Pulling a change from the repository using a plugin, which will integrate with the given source code management system (e.g., a Git plugin).
  2. Compiling a change by using a build tool like Maven and the Jenkins plugin (e.g., a Maven plugin).
  3. Running unit/integration testing on the compiled code using the build tool again.
  4. Running static analysis to check the code against coding standards and for dead code. This is done using a tool like SonarQube, which again is triggered using the Jenkins plugin.
  5. Bundling the compiled and tested files in the form of library files, like .JAR or .WAR files. This also can be done by using a build tool that’s triggered using the Jenkins plugin.
  6. Deploying this built library file on the production/test environment.
  7. Running end-end tests on this deployed application using end- end test automation tools (e.g., UI automation tools like Selenium, Protractor, etc.).
  8. Sending an email notification to the concerned team members regarding the status of this newly created application build. Includes the report of the end-end tests.

Using job

  1. Create a job
  2. Fill in the Enter an item name textbox by providing the name of the project in ex. hit_counter.
  3. Click Freestyle project and then click OK
  4. In the General tab, select the Discard old builds option so that the old builds do not eat up your disk space. Jenkins will do the housekeeping.
  5. In the Source Code Management tab, select Git. In Repository URL, enter https://github.com//hit_counter ( need Git Plugin)
  6. In the Build Triggers tab, select Poll SCM. This is where you specify how often you want Jenkins to perform the tests.
    • H/5 * * * * this means that you want Jenkins to perform the test every minute
    • H * * * *, this means the polling will be done every hour. If you do it as
    • H/15 * * * * the polling will be done every 15 minutes.
  7. Click your mouse outside the textbox. If you entered the code correctly, Jenkins will show the message stating when it will execute the next job. Otherwise, it will display an error in red.
  8. Click the Build tab. Click Add build step. Select Execute shell and write:
docker login -u <login> -p <password>
docker build -t hit_counter .
docker tag hit_counter akdevops/hit_counter
docker push akdevops/hit_counter
  1. Click on Build Now from the menu on the left.
⚠️ **GitHub.com Fallback** ⚠️