Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Tuesday, 13 December 2022

Configuring Jenkins ThinBackup and Restore


Create a backup directory 









Install Thinbackup Plugin










Navigate to ThinBackup and configure









Configure the settings


















Full backup happens every 5mins and Incremental backup every 2mins






Restore backup from the dropdown.








Backup any location

mkdir -p /opt/backup

chown -R jenkins:jenkins /opt/backup

ls -lsrt /opt/backup/

total 8

4 drwxr-xr-x 6 jenkins jenkins 4096 Dec 13 10:05 FULL-2022-12-13_10-05

4 drwxr-xr-x 3 jenkins jenkins 4096 Dec 13 10:06 DIFF-2022-12-13_10-06


Wednesday, 30 November 2022

Building a Spring Boot application in Jenkins using pipeline Groovy script and run with Docker Container

 

This Jenkins job will be building the docker image from the Git repo.

Create a pipeline job with Jenkins and add Pipeline Script.

pipeline {

    agent any


    tools {

        // Install the Maven version configured as "M3" and add it to the path.

        maven "M3"

    }


    stages {

        

        stage('Preparation') {

            steps {

                // Get some code from a GitHub repository

                git 'https://github.com/callicoder/spring-boot-websocket-chat-demo.git'


            }

        }

        

        stage('Build') {

            steps {


                // To run Maven on a Windows agent, use

                sh "mvn -Dmaven.test.failure.ignore=true clean package"

            }

            


            post {

                // If Maven was able to run the tests, even if some of the test

                // failed, record the test results and archive the jar file.

                success {

                    junit '**/target/surefire-reports/TEST-*.xml'

                    archiveArtifacts 'target/*.jar'

                }

            }

        }

 stage('Docker Build') {

   

          steps {

          sh 'docker build -t spring-boot-websocket-chat-demo .'

          }

        }

        

         

        stage('Deploy') {

   

          steps {

               sh 'docker stop app_container'

               sh 'docker rm app_container'

          sh 'docker run -d -p 5001:8080 --name app_container spring-boot-websocket-chat-demo'

          }

        }

    }

}















Tuesday, 29 November 2022

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

 

Error: - While running pipeline groovy script to build a docker image for spring boot chat demo.

+ docker build -t spring-boot-websocket-chat-demo .
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=spring-boot-websocket-chat-demo&target=&ulimits=null&version=1": dial unix /var/run/docker.sock: connect: permission denied
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
Stage "Deploy" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
solution:- 
sudo chmod 777 /var/run/docker.sock

Wednesday, 23 November 2022

Build a Java Application with Jenkins in Docker


cat Dockerfile

from jenkins/jenkins:lts

USER root

RUN apt-get update -qq && apt-get install wget

RUN wget http://apache.cs.utah.edu/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz

RUN tar xzf apache-maven-3.8.6-bin.tar.gz -C /opt/

RUN ln -s /opt/apache-maven-3.8.6 /opt/maven

RUN ln -s /opt/maven/bin/mvn /usr/local/bin

RUN rm -f apache-maven-3.8.6-bin.tar.gz

ENV MAVEN_HOME /opt/maven

RUN chown -R jenkins:jenkins /opt/maven

RUN apt clean

USER jenkins


sudo docker image build -t jenkins-docker .


 sudo docker images

REPOSITORY        TAG       IMAGE ID       CREATED          SIZE

jenkins-docker    latest    ca1cb1129c7e   38 seconds ago   505MB

jenkins/jenkins   lts       25fa92c47840   8 days ago       463MB


sudo docker run -it -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock --restart unless-stopped jenkins-docker


sudo docker ps

CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES

7333f25c2524   jenkins-docker   "/usr/bin/tini -- /u…"   4 minutes ago   Up 4 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   silly_sanderson


http://localhost:8080/


#get the admin password




Jenkins global configurations

sudo docker exec -it 7333f25c2524 /bin/bash

jenkins@7333f25c2524:/$ mvn --version

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

Maven home: /opt/maven

Java version: 11.0.16.1, vendor: Eclipse Adoptium, runtime: /opt/java/openjdk

Default locale: en, platform encoding: UTF-8

OS name: "linux", version: "5.15.0-52-generic", arch: "amd64", family: "unix"

jenkins@7333f25c2524:/$ java --version

openjdk 11.0.16.1 2022-08-12

OpenJDK Runtime Environment Temurin-11.0.16.1+1 (build 11.0.16.1+1)

OpenJDK 64-Bit Server VM Temurin-11.0.16.1+1 (build 11.0.16.1+1, mixed mode)































Execute shell commands in Jenkins by  Creating a Job



































Example using jenkins-docker-maven-github













    














Sunday, 29 May 2022

Reset Jenkins Admin Password


Make a backup copy of a Jenkins config file (If need to restore the previous settings)

cp -r /var/lib/jenkins/config.xml /var/lib/jenkins/config.xml.back


Disable the security

vim /var/lib/jenkins/config.xml 

<...>

<useSecurity>false</useSecurity>

<...>


Restart the Jenkins service

systemctl restart jenkins


Go to the Jenkins UI (No credentials this time) and reset the admin password


Navigate to "Manage Jenkins" 

In Security "Configure Global Security"

In Security Realm Select "Jenkins’ own user database" and  Save


Go to "People" From the Dashboard 

Select  username and "Configure"  Enter a new password in the "Password" and "Confirm password" fields and Save


Once the admin password is reset, restore the /var/lib/jenkins/config.xml file and restart Jenkins

 mv /var/lib/jenkins/config.xml.back /var/lib/jenkins/config.xml

 systemctl restart jenkins


Ansible Playbook | Trigger Jenkins Job using token and copy .war to Apache tomcat path

 cat trigger_jenkins.yml 
---
 - name: Deployment of .war file
   hosts: localhost
   become: yes

   tasks:

      - name: Trigger Jenkins Job
        shell: curl -v -X POST http://localhost:8080/job/Project_name/build --user jenkinsxyz:110d44acb43a8f57c8e54fc6360bf3e5ab

      - name: Wait until the file .war is present before continuing
        wait_for:
          path: /var/lib/jenkins/workspace/Project_name/target/Name.war

     - name: copy .war file to tomcat
        copy:
          src: /var/lib/jenkins/workspace/Project_name/target/Name.war
          dest: /opt/tomcat10/apache-tomcat-10.0.21/webapps/
          remote_src: yes
          directory_mode: yes

Wednesday, 6 April 2022

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope) error: failed to push some refs to 'https://github.com/path/filename.git'

  

Issue

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope)

error: failed to push some refs to 'https://github.com/path/filename.git'


Solution

Login GitHub URL 

Go to Settings ➡ Developer settings➡Personal access tokens


✔workflow Update GitHub Action workflows


Tuesday, 8 March 2022

Jenkins: Remote Triggering of a Parameterized Build


Configure a parameterized build in Jenkins


#Install Plugin - Build With parameters

Jenkins ➡ Manage Jenkins ➡ Manage Plugins ➡ Available ➡ Build With Parameters

Jenkins ➡ New Item ➡ Enter an item name ➡ [ParameterizedDemo] ➡ Freestyle project ➡ OK

General ➡ This project is parameterized ➡ String parameter

Name

my_param

Default Value

Hello


Build Triggers ➡ Trigger builds remotely ➡ 

Authentication Token

Token_987


Build ➡ dd build step ➡ Execute shell

Command

echo $my_param

Apply-Save


Triggering a parameterized build remotely


#Open the terminal

#curl -X GET <YourJenkinsJobUrl>/buildWithParameters?token=TOKEN_NAME

curl -X GET http://localhost:8080/job/ParameterizedDemo/buildWithParameters?token=Token_987


#Open the job in the Jenkins UI

Build with Parameters ➡ Build ➡ Build history ➡ Console Output


Console Output


Started by user admin

Running as SYSTEM

Building in workspace /var/lib/jenkins/workspace/ParameterizedDemo

[ParameterizedDemo] $ /bin/sh -xe /tmp/jenkins17901601731840577267.sh

+ echo Hello

Hello

Finished: SUCCESS


Install Ant plugin and integrate it with Jenkins


Tools: Git, GitHub, and Jenkins


1. Create a GitHub repository 

Create a new repository ➡ Repository Name ➡ Public ➡ Add a README file ➡ create repository

Code ➡ HTTPS ➡ https://<url> ➡ copy


2. Add build.xml file to the repository

#open terminal

git clone https://<url>

cd Demo-Ant/

vim build.xml

<?xml version="1.0"?>

<project name="Hello World Project" default="info">

<target name="info">

<echo> Hello World - Demo Ant</echo>

</target>

</project>


git init

git add build.xml

git commit -m "Add build file"

git remote -v

git push -u origin main


3. Set the Global Tool Configuration


Jenkins ➡ Manage Jenkins ➡ Global Tool Configuration


Ant-

Ant installations  

Add Ant

Ant

Name

[FirstAnt]

Install automatically

Install from Apache

Version

List of Ant installations on this system

Apply ➡ Save


4. Integrate Ant with Jenkins

Jenkins ➡ New Item ➡ Enter an item name ➡ Freestyle project ➡ OK

Source Code Management ➡ Git ➡ Repository URL[https://<url>] ➡ Branch Specifier[*/main]

Build ➡ Add build step ➡ Invoke Ant ➡ Ant Version[FirstAnt]

Apply ➡ Save


Build Now ➡ Build History ➡ Console Output


Friday, 4 March 2022

Reset Jenkins Admin Password


 sudo vi /var/lib/jenkins/config.xml


 <useSecurity>true</useSecurity>

#modify to

 <useSecurity>false</useSecurity>


 sudo systemctl restart jenkins


Navigate to the web console

Notice that you were not prompted for a username or password


do the following.


Click on People on the left-hand navigation menu.

Click on the Admin

Delete the user account


Navigate to Manage Jenkins

Click on Configure Global Security

Under Security Realm, select Jenkins’ own user database


In the Authorization section, select Logged-in users can do anything

Unselect Allow anonymous read access

Save changes


It will redirect to a page where a new Admin user can be created


#try by specifying the right path

http://localhost:8080/securityRealm/firstUser


#not working? repeat the above steps after the following

sudo mv /var/lib/jenkins/users/* /tmp/jenkins


Ref: - serverlab.ca


Friday, 11 February 2022

Add JDK Maven and Git Plugins with Jenkins

Add JDK Maven and Git Plugins with Jenkins in local

Login Jenkins⇒Manage Jenkins⇒Manage Plugins⇒available(search maven)⇒Maven Integration(Install without restart)

http://localhost:8080/updateCenter/    #to view


#in terminal

sudo apt install maven

mvn -version

Apache Maven 3.6.3

Maven home: /usr/share/maven

Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "5.13.0-28-generic", arch: "amd64", family: "unix"


Add JDK

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add JDK

#Name

local_java

#JAVA_HOME

/usr/lib/jvm/java-11-openjdk-amd64

#uncheck Install automatically

save


Add maven

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

local_maven

#MAVEN_HOME

/usr/share/maven

#uncheck Install automatically

save


Add Git

git --version

git version 2.25.1

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

localGit

#Path to Git executable

/bin/git

#uncheck Install automatically

save



Install Jenkins on Ubuntu 20.04

 To Install Jenkins on Ubuntu 20.04


#java

java --version

openjdk 11.0.13 2021-10-19


#add jenkins public key

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

OK


#add jenkins repository 

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


sudo apt update

sudo apt install jenkins


#service status

sudo service jenkins status

Ctrl+C #exit

sudo systemctl start jenkins

sudo service jenkins status


#In the browser

http://localhost:8080/


#to get Administrator password

sudo cat /var/lib/jenkins/secrets/initialAdminPassword


#Follow the pages to complete

Install suggested plugins 

Create Frist Admin User

Instance Configuration

Jenkins is Ready!


Jenkins⇒Manage Jenkins⇒Jenkins CLI⇒download "jenkins-cli.jar"

#or go to http://localhost:8080/cli/ 


sudo java -jar jenkins-cli.jar -s http://localhost:8080/ -version

Version: 2.319.3


Add JDK Maven and Git Plugins with Jenkins


Ref:- how-to-install-jenkins-on-ubuntu-20-04


Thursday, 20 February 2014

Jenkins On RedHat

Jenkins Prerequisites

httpd
subversion
java

Installation
Add the Jenkins repository to the yum repos, and install Jenkins from here.
Installation of a stable version
Installation of Java
Jenkins requires Java in order to run, yet certain Fedora-based distros don't include this by default. To install the Open Java Development Kit (OpenJDK) run the following:
sudo yum install java

Start/Stop
 
  • sudo service jenkins start/stop/restart
  • sudo chkconfig jenkins on
What does this package do?
  • Jenkins will be launched as a daemon up on start. See /etc/init.d/jenkins for more details.
  • The 'jenkins' user is created to run this service. If you change this to a different user via the config file, you must change the owner of /var/log/jenkins, /var/lib/jenkins, and /var/cache/jenkins.
  • Log file will be placed in /var/log/jenkins/jenkins.log. Check this file if you are troubleshooting Jenkins.
  • /etc/sysconfig/jenkins will capture configuration parameters for the launch.
  • By default, Jenkins listen on port 8080. Access this port with your browser to start configuration.  Note that the built-in firewall may have to be opened to access this port from other computers.  (See http://www.cyberciti.biz/faq/disable-linux-firewall-under-centos-rhel-fedora/ for instructions how to disable the firewall permanently)
  • A Jenkins RPM repository is added in /etc/yum.repos.d/jenkins.repo

IPtables and SELinux

sudo iptables –L
getenforce
Disabled

Port Listen Test

sudo netstat -tnlp | grep 8080
sudo netstat –tulpen
sudo lsof -i :8080

Run Jenkins

sudo locate jenkins.war
sudo java -jar /usr/lib/jenkins/jenkins.war

sudo ps -u jenkins
 PID TTY          TIME CMD
22623 ?        00:00:17 java

Error

WARNING: FAILED SelectChannelConnector@0.0.0.0:8080: java.net.BindException: Address already in use.
sudo netstat -tnlp | grep 8080
sudo ps -u jenkins
Check the firewall and network configuration, proxy or your antivirus etc.

Packages Installed

sudo rpm -qa | grep httpd
httpd-tools-2.2.15-29.el6_4.x86_64
httpd-2.2.15-29.el6_4.x86_64

sudo rpm -qa | grep subversion
subversion-1.6.11-9.el6_4.x86_64

sudo rpm -qa | grep java
tzdata-java-2013i-1.el6.noarch
java-1.7.0-openjdk-1.7.0.45-2.4.3.4.el6_5.x86_64

sudo rpm -qa | grep jenkins
jenkins-1.551-1.1.noarch

Ref: