Jenkins: A Comprehensive Guide to Continuous Integration and Deployment

Understanding Continuous Integration

Continuous Entegration (CI) is a development practice where developers regularly merge their code changes into a central repository. After each merge, automated builds and tests are run. Jenkins serves as an excellent platform for implementing CI/CD processes due to its extensive plugin ecosystem.

The typical deployment pipeline involves these steps:

  1. Code retrieval from version control
  2. Code compilation (optional for interpreted languages)
  3. Configuration file placement
  4. Application packaging
  5. Transfer to target servers
  6. Temporary removal from load balancer
  7. Extraction and placement in web root directory
  8. Synchronization of differential files
  9. Service restart (optional)
  10. Testing
  11. Return to load balancer

Jenkins Installation

To set up Jenkins on a CentOS system, follow these steps:

  1. Install JDK: ``` yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  2. Download the Jenkins RPM package: ``` wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.32.3-1.1.noarch.rpm
  3. Install Jenkins: ``` rpm -ivh jenkins-2.32.3-1.1.noarch.rpm
  4. Start Jenkins service: ``` service jenkins start
  5. Verify the service is running on port 8080: ``` netstat -tulnp|grep 8080
    
    

Access Jenkins at http://your-server-ip:8080. The initial password is located at /var/lib/jenkins/secrets/initialAdminPassword.

Jenkins Plugins and Configuration

Jenkins' functionality is primarily extended through plugins. To install plugins:

  1. Navigate to Manage Jenkins → Manage Plugins
  2. Install necesary plugins (e.g., GitLab plugin for GitLab integration)
  3. Advanced users can upload custom plugins via Advanced tab

Important Jenkins directories:

  • /etc/sysconfig/jenkins - Configuration file
  • /usr/lib/jenkins/jenkins.war - Application WAR file
  • /var/lib/jenkins - Main directory
  • /var/log/jenkins - Log files

Jenkins Backup and Recovery

To back up Jenkins:

mkdir /backup/jenkins -p
rsync -raz --delete --progress /var/lib/jenkins /backup/jenkins/

To restore from backup:

rsync -raz --delete /backup/jenkins/jenkins /var/lib/
service jenkins restart

Jenkins Upgrade Process

  1. Locate the current WAR package: ``` rpm -ql jenkins | grep jenkins.war
  2. Download the latest stable WAR package
  3. Stop Jenkins: ``` service jenkins stop
  4. Backup the existing WAR file and replace with the new version: ``` cd /usr/lib/jenkins mv jenkins.war jenkins-old.war mv /path/to/new/jenkins.war .
  5. Restart Jenkins: ``` service jenkins start
    
    

Setting Up Git Integration

To integrate Jenkins with Git repositories:

  1. Install Git: ``` yum install -y git
  2. Generate SSH key for Jenkins server: ``` ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub
  3. Add the public key to your Git repository
  4. In Jenkins, create a new project and configure Git source management

Creating a Jenkins Job for PHP Projects

  1. Create a new "Freestyle project" in Jenkins
  2. Configure "General" settings to discard old builds
  3. Under "Source Code Management", select Git and provide repository URL
  4. Add SSH credentials for private repositories
  5. In "Build" section, add "Execute shell" step for deployment: ``` #!/bin/bash rsync -raz --delete --progress . user@target-server:/path/to/deploy/ ssh user@target-server "cd /path/to/deploy && restart_command"
    
    

Integrating Maven with Jenkins

Installing Maven

wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
tar -zxvf apache-maven-3.5.2-bin.tar.gz
mv apache-maven-3.5.2 /opt/
export PATH=$PATH:/opt/apache-maven-3.5.2/bin
echo 'export PATH=$PATH:/opt/apache-maven-3.5.2/bin' >> /etc/profile
source /etc/profile
mvn -v

Configuring Maven in Jenkins

  1. Navigate to Manage Jenkins → Configure System → Global Tool Configuration
  2. Add Maven installation with path to your Maven installation
  3. Create a new Maven project in Jenkins
  4. Configure Git source management
  5. In "Build" section, set goals to "clean install"
  6. Add "Execute shell" step for deployment: ``` #!/bin/bash rsync -raz --delete --progress target/*.jar user@target-server:/opt/ ssh user@target-server "java -jar /opt/your-app.jar"
    
    

Version-Based Deployments with Maven

  1. Install "Git Parameter Plugin" in Jenkins
  2. In your job configuration, check "This project is parameterized"
  3. Add "Git Parameter" with name "release"
  4. Select "Tag" as the release type
  5. Update your source management to use the parameter: ``` ${release}
  6. Create tags in your Git repository: ``` git tag v1.0 git push origin v1.0
    
    

Remote Management with Jenkins

Using SSH for Remote Operations

In your Jenkins job's "Execute shell" step:

#!/bin/bash
ssh user@remote-server "/path/to/deployment/script.sh"

Using SSH Plugin

  1. Install "SSH Plugin" in Jenkins
  2. Navigate to Manage Jenkins → Configure System → SSH remote hosts
  3. Add your remote server details
  4. In your job configuration, add "Send files or execute commands over SSH" build step

Jenkins Clustering

  1. In Jenkins UI, navigate to Manage Jenkins → Configure Global Security
  2. Set "TCP port for JNLP agent" (e.g., 30000)
  3. Navigate to Manage Jenkins → Manage Nodes → New Node
  4. Create a new node with appropriate name and labels
  5. In your job configuration, under "General" → "Restrict where this project can be run", specify the node label

Role-Based Authorization in Jenkins

  1. Install "Role-based Authorization Strategy" plugin
  2. Navigate to Manage Jenkins → Configure Global Security
  3. Select "Role-Based Strategy" under Authorization
  4. Create users via Manage Jankins → Manage Users
  5. Assign roles via Manage and Assign Roles:
    • Project roles: Define permissions for specific jobs
    • Global roles: Define system-wide permissions

Implementing Jenkins Pipelines

  1. Install "Pipeline" plugin
  2. Create a new "Pipeline" project
  3. Use Pipeline script to define your workflow: ``` node { stage 'Build PHP Application' build 'php-project' stage 'Build Java Application' build 'java-project' }
    
    

GitLab Integration with Jenkins

  1. Install "GitLab Plugin" in Jenkins
  2. Configure GitLab credentials in Jenkins
  3. In Jenkins system settings, configure GitLab connection
  4. In your job configuration:
    • Check "Build when a change is pushed to GitLab"
    • Set GitLab CI Service URL
    • Configure trigger events and branch filters
  5. In GitLab project settings, add webhook with the Jenkins URL and secret token

Nexus Repository Management

Installing Nexus

wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.gz
tar -zxvf nexus-2.14.5-02-bundle.tar.gz
mv nexus-2.14.5-02 sonatype-work /opt/
cd /opt/nexus-2.14.5-02
./bin/nexus start
export RUN_AS_USER=root
./bin/nexus start

Access Nexus at http://your-server-ip:8081/nexus (default credentials: admin/admin123).

Configuring Maven to Use Nexus

Edit Maven's settings.xml:

<repositories>
    <repository>
        <id>nexus</id>
        <url>http://your-nexus-server:8081/nexus/content/groups/public/</url>
    </repository>
</repositories>

Deploying Java Applications with Jenkins

  1. Set up Tomcat server with proper permissions
  2. Push Java application code to Git repository
  3. Create a new Maven project in Jenkins
  4. Configure Git source management
  5. Set build goals to "clean install -Dmaven.test.skip=true"
  6. Add "Deploy war/ear to a container" post-build action:

Automated Deployment Script

Create a deployment script with the following structure:

#!/bin/bash

# Configuration
NODE="target-server-ip"
PRO_NAME="project-name"
CODE_DIR="/deploy/code/$PRO_NAME"
CONFIG_DIR="/deploy/config"
TMP_DIR="/deploy/tmp"
TAR_DIR="/deploy/tar"

# Functions
writelog() {
    echo "$(date "+%Y-%m-%d %H:%M:%S"): $1" >> /var/log/deploy.log
}

code_get() {
    writelog "Retrieving code from repository"
    cd $CODE_DIR && git pull
    cp -r $CODE_DIR $TMP_DIR
    API_VER=$(git log -1 --pretty=format:%h)
    return $API_VER
}

code_config() {
    writelog "Applying configuration files"
    /bin/cp -r $CONFIG_DIR/* $TMP_DIR/$PRO_NAME
    PKG_NAME="${PRO_NAME}_${API_VER}_$(date +%Y-%m-%d-%H-%M-%S)"
    cd $TMP_DIR && mv $PRO_NAME $PKG_NAME
}

code_tar() {
    writelog "Creating deployment package"
    cd $TMP_DIR && tar -czf ${PKG_NAME}.tar.gz ${PKG_NAME}
}

code_scp() {
    writelog "Transferring package to target server"
    scp $TMP_DIR/${PKG_NAME}.tar.gz $NODE:/webroot/$PRO_NAME
}

code_deploy() {
    writelog "Deploying application on target server"
    ssh $NODE "cd /webroot/$PRO_NAME && tar -zxf ${PKG_NAME}.tar.gz"
    ssh $NODE "rm -rf /data/web/$PRO_NAME && ln -s /webroot/$PRO_NAME/${PKG_NAME} /data/web/$PRO_NAME"
}

code_test() {
    writelog "Testing deployment"
    curl -s --head http://$NODE/ | grep '200 OK'
    if [ $? -ne 0 ]; then
        writelog "Deployment test failed"
        exit 1
    fi
}

# Main execution
case "$1" in
    deploy)
        code_get
        code_config
        code_tar
        code_scp
        code_deploy
        code_test
        ;;
    *)
        echo "Usage: $0 {deploy}"
        exit 1
        ;;
esac

Conclusion

Jenkins provides a powerful platform for implementing continuous integration and deployment workflows. By leveraging its extensive plugin ecosystem and flexible configuration options, teams can automate their build, test, and deployment processes, accelerating delivery while maintaining quality standards.

Tags: Jenkins continuous-integration devops automation Git

Posted on Mon, 19 Oct 2026 16:29:10 +0000 by gullit