Ex 4 Devops
Ex 4 Devops
Theory:
“Continuous Integration is a software development practice where members of a team
integrate their work frequently, usually each person integrates at least daily - leading to
multiple integrations per day. Each integration is verified by an automated build (including
test) to detect integration errors as quickly as possible.” In simple way, Continuous
integration (CI) is the practice of frequently building and testing each change done to your
code automatically. Jenkins is a self-contained, open source automation server which can be
used to automate all sorts of tasks related to building, testing, and delivering or deploying
software. Our first job will execute the shell commands. The freestyle project provides
enough options and features to build the complex jobs that you will need in your projects.
OUTPUT:
1. Installation in a Docker container
docker pull jenkins/jenkins:lts
docker ps will not show a running container, as we did not yet start it. Start an instance of Jenkins
with the following command:
If we need other tools installed, we can create wer own Docker file with the necessary tooling
installed. For example the following creates a new image based on Jenkins with Maven installed.
All the configuration and jobs will be stores in wer user defined directory.
Download the jenkins.war file.. From this file we can start Jenkins directly via the command line
with java -jar jenkins*.war.
To run it in wer Tomcat server, put the .war file into the webapps directory. If we start Tomcat,
wer Jenkins installation will be available under
http://localhost:8080/jenkins
If the jenkins.war is deployed in wer webapps directory, but can not be started and the tomcat
manager says FAIL - Application at context path /jenkins could not be started, we may need to
grant the permissions for JENKINS_HOME.
sudo
3. Configure Jenkins
After installation, open a browser and connect to it. The default port of Jenkins is :8080,
therefore on wer local machine we find it under the following URL:
http://localhost:8080/
We will need to copy the initial password from the file system of the server.
Afterwards we can select to install Plugins. Select the Install suggested Plugins to get a typical
configuration.
Select Manage Jenkins and then Configure Global Security. Select the Enable security flag. The
easiest way is to use Jenkins own user database. Create at least the user "Anonymous" with read
access. Also create entries for the users we want to add in the next step.
On the login page, select Create an account to create the users we just gave access.
If we want to create a role based authorization Strategy we first need to install the Role-based
Authorization Strategy Plugin. Go to Manage Jenkins Manage Plugins Available enter Role-
based Authorization Strategy in the filter box and select and install the Plugin. To see a list of
commonly used Plugins go to Plugin management.
Now go to Manage Jenkins Manage and Assign Roles Assign Roles to grant users additional
access rights.
Navigate to Manage Roles to define access restrictions in detail. Pattern is a regex value of the job
name. The following grants unregistered users read-only access to wer build jobs that start with
the L-, C-, I- or M- and only those.
If we want to access a private Git repo, for example at GitHub, we need to generate an ssh key-
pair. Create a SSH key with the following command.
The public key must be uploaded to the service we are using, e.g., GitHub.
Enter a description for the job (project name) and configure how many builds should be retained and for
how long.
Configure how the source code can be retrieved. If we are for example using Git, enter the URL to the Git
repository. If the repository is not public, we may also need to configure the credentials.
Specify when and how wer build should be triggered. The following example polls the Git
repository every 15 min. It triggers a build, if something has changed in the repo.
To trigger a build after a commit on the Git repository, select GitHub hook trigger for GITScm
polling instead of Poll SCM.
Press Save to finish the job definition. Press Build Now on the job page to validate that the job
works as expected.
After a while the job should go to green or blue (depending on wer configuration), if successful.
Click on the job and afterwards on Console Output to see the log file. Here we can analyze the
build errors.
5. Build Pipelines
Jenkins pipelines help we align the build process of a project. This is done by specifying tasks
and the order in which they are executed. There are all kinds of possible tasks that a Jenkins
pipeline can do for we. For example, build assets, send an email on error, send the build artifacts
via SSH to wer application server, etc.
Jenkins allows to specify pipelines using a Jenkinsfile. This is just a textfile that contains the
necessary data for Jenkins to execute the pipeline.
Jenkins supports two different syntaxes.
1. Declarative (since Pipeline version 2.5)
2. Scripted
For this tutorial we will focus on the declarative approach.
The following example shows a pipeline with 2 stages:
pipeline {
agent any
stages {
stage('Build Assets') {
agent any
steps {
echo 'Building Assets...'
}
}
stage('Test') {
agent any
steps {
echo 'Testing stuff...'
}
}
}
}
The agent directive tells Jenkins to allocate a workspace and an executor for the pipeline. Without it, the
pipeline is not valid and therefore required.
Setup using the Blue Ocean Plugin
The above process can also be done using the Blue Ocean Jenkins Plugin.
To install the Plugin go to Manage Jenkins Manage Plugins Available and select the Blue
Ocean Plugin.
After the installation is finished we have an additional menu entry called Open Blue Ocean in
wer main Jenkins navigation.
The Blue Ocean application will provide a link to the GitHub page we need to visit. The necessary
permissions that Blue Ocean needs to operate are already selected. Add a description and click
on Generate Token at the bottom of the page.
Copy the generated token and paste it in the Blue Ocean mask.
Select the account the repository belongs to and select the repository.
Adding steps to wer Pipeline
In the next screen we will see a visual representation of wer Pipeline. Here we can add or remove steps.
To create a new step click on + in the canvas. A menu will open on the right that lets we specify
a name and what steps we want to perform.
After we have finished editing the Pipeline Blue Ocean will offer we to commit the newly
created pipeline to wer repository.
Under the hood Blue Ocean only created a valid Jenkinsfile to be used by Jenkins.
After committing Jenkins will build the project using the newly modified Pipelines.
6. Restart wer Jenkins
After installing Plugins we will need to restart Jenkins. We can do so by adding restart as URL
parameter,
CONCLUSION:
Here, we got to learn about the Jenkins Programming and how to install and configure it.