Tuesday, February 1, 2022

Maven, Jenkins - Class Notes

 Maven - Class Notes
----
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

No comments:

Post a Comment

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. ...