Tuesday, January 25, 2022

Maven - Jenkins - Intro - class notes

 Maven by Ravi

----


apt install tree

Build configuration tool - Maven


Project directour structure

Build process (pom.xml)

Life cycle phases

  - validate

  - compile

  - Test

  - Package

  - Install

  - Deploy


command

$ mvn -f pom.xml clean install/deploy


install vs deploy


nexus/jfrog



draio - mac drawing tool


diagrams.net



maven -> jenkins -> nexus repo


webhook (jenkins)



--------------------

Building lab environment

- login to aws console

hostname: maven-host

- connect 

$ ssh -i "user.pem" ubuntu@ec2-192.168.10.20.us-webt-1.compute.amazonaws.com


security -> 

inbound rule

ssh -> use your ip


ssh anywhere 0.0.0.0

or ssh source anywhere -> specify your ip



$ tree ../core-app


$ mvn clean

going to delete target directory


when you run mvn compile/package

the output, packaged file will be on target directory


$ cd core-app; ls


$ mvn ..

it generates target file 


$ mvn clean install

deletes all old target directory contents and re-creates


it compiles, packages and stores on repo as well




multi module projct

$ cd maven-multimules

$ tree simple-parent

 -src

   - main

   - test


$ git remove -v


$ cat pom.xml


distribution manage task, we are going to manage


push to remove site using vscode


$ git status


$ touch .gitignore

$ vi .gitignore

target/


$ git status

$ rm -rf .gitignore

$ mvn clean


$ git pull


$ mvn clean install


$ git status


Notice: git will ignore the target file.



We have to login to

- ssh: ec2

- git we use, clone/pull operation

- we use Maven


developer modify the file everyday.


so you have to manually upload/download


so, we use process continue integration tool (jenkins) to automate the task.


We are going to install jenkins tool.


continous integration tool

- jenkins

- gitlab runner

- circleci

- azure devops pipeline

- bitbucket pipeline

- gitub actions

- code pipeline

- travis

- teamcity


=========================

4 steps

Step 1. maven commands

Step 2. jenkins

Step 3. 

Step 4. 



You can install maven and jenkins on the same system but its better to use different instance.


1. Launch a new instance - use t2.small for jenkins

2. t2.micro for maven 


login to the hosts:


Install jenkins

use online instruction

set up repo or download the package and install - either way..

$ sudo apt-key add - 

$ wget -q -o - https://pkg .. > /etc/apt/sources.list.d/jenkins.list'



$ apt-update


$ apt install jenkins


$ apt repolist (apt ubunto repolist)


failed to start LSB: start jenkins at boot time..


read the error message..

no jave executable found in current path



Install JAVA

look at the bottom of the page, you will see guide how to install.

$ install openjdk-11-

$ systemctl start jenkins


$ systemctl status jenkins



get jenkins public ip and paste on browser


http://<ip>:8080


you may not get any page, so you have to add 


click on the instance

- security  and add port 8080 anywhere and save the rule


refresh on the browser, you will see it.


follow the instruction to set up user


jenkins home directory

$ cd /var/lib/jenkins


you finally completed jenkins server.



click on the create a job from the middle of the page


item name: test job

select Freestyle projecct -> basic type 


click ok


you are on general


select discard old builds


source code management

git


build

add exxecute shell

echo "Hello world"


click ok


we created a simple job.


click o build now, 

click on build history

- click on console output - you will see the output

- go to command prompt and run $ echo "hello, World" and compare the output.


as of now, we created new item, free stye project. selected couple option with echo command.





$ git remote -v


get the url


now go to source management and paste the git url


brnach /master

save it and build now.


it will perform the job.


job is executed. 




to to home page

manage jenkins


- manage plug-ins



# cd /var/lib/jenkins

# cd workspace/testjob

# ls

src

pom.xml


where do you define the linux credentials?

- we install jenkins on the linux server so it use the system account to launch the job.



click on manage plugins

search "maven

select maven integration


go to home dir

- click on new item

Now, you can see maven project

name: core-app-maven-build

select maven project

clcik ok


copy your repo 

specify git


go down

build option , it automatically understands


Root pom

pom.xml


Under goal and option

clean install


save the job


you save new job

to go home, amd click on manage jenkins

- configure tools, global tools configuration


click on add maven

name: maven-3.8.4   # specify the path


select Install automatically


jenkins automatically downloads maven pacakge and installs it.


you can select the version you want..


once saved, click on build now.


click on the job and console output


where can we put maven server details if we jenkins and maven are on different server..




As of now, we install jenkins and created job. and successfully executed the job


check git link for 1:05:00 - 1:07:50


$ mvn archetype...


$ tree core-app


github.com/devopsgsvc



add multiple people into the team and distribut the job.

that way, you can add number of executors

- click on manage jenkins

- Manage nodes


to add plugins

- add, remove disable enable plugins..

- you can add n numbers of hosts.


- click on new node

or

configure clouds


node name: node01

select permanent agent


description: testing


number of executor: 3


remote root directory: 

paste from the command below


go to command line and create dir

$ mkdir jenkins-agent

$ pwd

copy the path



Labels: maven-label


Usage: use this node as much as possible


launch method: launch agents via ssh


Host: IP address - if cloud, use provate IP

credentials: 



Host key verification strategy: known hosts file verification strategy

to login from node1 to node2, we can use password or key.

Node 1 ---ssh---> Node2


$ ssh node2


you can use id_rsa.pub per user basic or

if you are using cloud, you can use authorized key from user's home directory to .ssh.


This public key can also be used to login to remote machine for passwordless login.


add credential

domain: global credential

kind: ssh usernane with private key

id: ubuntu

private key:

key:

paste the output of your authorized_keys value.



Host key verification strategy:


save


click on node01 and click on log


you will get the status if it is succesfully connected.



why labels

if you want to add multiple node, each node may be doing different tasks, say 2 node maven, 2 docker, or other job, so label will help you to pick up..


jobs -> configuration


restrict where this project can be run 

  label expression: maven-label

specify your label here.


once done, click on build now.

click on job# and click on console output.



click on workspace - delete


or go to command line and browse to the jenkins location.



go to home

- manage jenkins

- configure clouds


error - no cloud implementation for dynamically allocated agents installed .. go to plugin manager...



Do some research on,

how to configure cloud concepts on jenkins... 


configure -> fill required info, 

Will cover this instance tomorrow..

- dynamic node

- Webhook

- Authentication

- Pipeline

- Nexus

- Docker


If you are using cloud service, make sure to stop/delete.

draw.io



Thursday, January 13, 2022

Day 3 - Jenkins Pipeline

day 3 - jenkins

Pre-requisites
1. Install two linux instance
2. 



install tomcat

# vi 


Install Apache Tomcat Server on Ubuntu

1. Update your system
$ sudo apt update

2. Search the tomcat on repo
$ sudo apt-cache search tomcat

3. download and install tomcat server
$ sudo apt install tomcat9 tomcat9-admin

verify
$ ss -ltn  # see if port 8080 is listening

4. Allow port 8080 
$ sudo ufw allow from any to any port 8080 proto tcp

5. Test if tomcat is installed successfully
http://<IP-Addr:8080

You will see "It works!" if installed successfully.

6. Create User

$ sudo vi /etc/tomcat9/tomcat-users.xml

<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
#<user username="tomcat" password="pass"roles="admin-gui,manager-gui"/>
<user username="admin" password="password" roles=roles="admin-gui,manager-gui, manage-scripts" />

7. Start your tomcat
$ sudo systemctl restart tomcat9
$ sudo systemctl enable tomcat9 # to make sure tomcat runs fine upon reboot.

8. Access your tomcat application manager
http://<ip-addr>:8080/manager

it will prompt you for username/pw. Enter the username/password you speficy at the tomcat-users.xml config file.


You are all set..


once you install tomcat, go to your jenkins and go to plugins
search for "deploy to"

add "Deploy to cintainer"

and click on "download and install after restart"




/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/jre/bin/java

Day2 - Jenkins - pipeline class notes

 Maven
- Maven plugin.
mvn clean install
sh script.sh
python script.py

apt install maven
yum install maven
maven --help
mvn --help
------------------------------
node{
  stage('Checkout code'){
     checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/anujdevopslearn/MavenBuild.git']]])
}

}

put this code in between checkout code section
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/anujdevopslearn/MavenBuild.git']]])
----------------------------------------------
node(){
stage('Checkout Code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/anujdevopslearn/MavenBuild.git']]])
}
stage('Build Process'){
sh '''
ls -ltra
mvn clean install
'''
}
stage('Deployment'){
}
}


-------------------
node(){
def gitURL = "https://github.com/anujdevopslearn/MavenBuild.git"
def branchName = "*/master"
def mvnGoal = "install"
stage('Checkout Code'){
checkout([$class: 'GitSCM', branches: [[name: branchName]], extensions: [], userRemoteConfigs: [[url: gitURL]]])
}
stage('Build Process'){
sh '''
ls -ltra
mvn clean ${mvnGoal}
'''
}
stage('Deployment'){
}
}

=================================
node(){
def gitURL = "https://github.com/anujdevopslearn/SonarQubeNodeJS"
def branchName = "*/master"
def mvnGoal = "install"
stage('Checkout Code'){
checkout([$class: 'GitSCM', branches: [[name: branchName]], extensions: [], userRemoteConfigs: [[url: gitURL]]])
}
stage('Build Process'){
sh '''
ls -ltra
#mvn clean ${mvnGoal}
npm install # node package manager
npn run build --if-present
npn run test
npn run test:e2e # e2e - end to end
'''
}
stage('Deployment'){
}
}

Day1- jenkins

1. Git install
$ apt-get install git
$ yum install git -y
Install latest version of git by compiling from source code,
jenkins.io/download
as of today, ssh certificate of jenkins is expired so steps are not working....

https://progressivecoder.com/creating-an-aws-account-a-step-by-step-process-guide/

installation
https://www.section.io/engineering-education/setup-ssh-ubuntu-vm-aws/
or jenkins.io/debian-stable
cloudbees.com/jenkins
hudson** - paytool


aws -> ubuntu 20.04 -> Instance type: t2.micro -> next ->  security group
add ports
-> add - tcp port  8080 anywhere/0
or use the existing security group which fulfil the requirement for jenkins
-> view n launch -> launch
-> save or use the exisint keypair
connect to your instance -> 
login to the instance
paste the command from the installation guide ..
# curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyrung.asc > /dev/null
2. Install java
$ sudo apt install OpenJdk-8-jdk
apt update
apt install default-jdk
apt install jenkins

3. Run jenkins - installl
we just follow official steps

curl -fsSL https:/
https://www.toolsqa.com/jenkins/install-jenkins/
https://www.toolsqa.com/jenkins/install-jenkins/
https://www.toolsqa.com/jenkins/install-jenkins/
https://www.toolsqa.com/jenkins/install-jenkins/
sudo t

https://www.toolsqa.com/jenkins/install-jenkins/
Tomorrow: Creating jenkins jobs..


github.com/anujdeveloplearn/interviewQuestions/tree/master/interviewQuestions


codeshare.io

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee     /usr/share/keyrings/jenkins-keyring.asc > /dev/null
apt update
apt install default-jdk
apt install jenkins

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...