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:
- Code retrieval from version control
- Code compilation (optional for interpreted languages)
- Configuration file placement
- Application packaging
- Transfer to target servers
- Temporary removal from load balancer
- Extraction and placement in web root directory
- Synchronization of differential files
- Service restart (optional)
- Testing
- Return to load balancer
Jenkins Installation
To set up Jenkins on a CentOS system, follow these steps:
- Install JDK: ```
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
- Download the Jenkins RPM package: ```
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.32.3-1.1.noarch.rpm
- Install Jenkins: ```
rpm -ivh jenkins-2.32.3-1.1.noarch.rpm
- Start Jenkins service: ```
service jenkins start
- 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:
- Navigate to Manage Jenkins → Manage Plugins
- Install necesary plugins (e.g., GitLab plugin for GitLab integration)
- 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
- Locate the current WAR package: ```
rpm -ql jenkins | grep jenkins.war
- Download the latest stable WAR package
- Stop Jenkins: ```
service jenkins stop
- 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 .
- Restart Jenkins: ```
service jenkins start
Setting Up Git Integration
To integrate Jenkins with Git repositories:
- Install Git: ```
yum install -y git
- Generate SSH key for Jenkins server: ```
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
- Add the public key to your Git repository
- In Jenkins, create a new project and configure Git source management
Creating a Jenkins Job for PHP Projects
- Create a new "Freestyle project" in Jenkins
- Configure "General" settings to discard old builds
- Under "Source Code Management", select Git and provide repository URL
- Add SSH credentials for private repositories
- 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
- Navigate to Manage Jenkins → Configure System → Global Tool Configuration
- Add Maven installation with path to your Maven installation
- Create a new Maven project in Jenkins
- Configure Git source management
- In "Build" section, set goals to "clean install"
- 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
- Install "Git Parameter Plugin" in Jenkins
- In your job configuration, check "This project is parameterized"
- Add "Git Parameter" with name "release"
- Select "Tag" as the release type
- Update your source management to use the parameter: ```
${release}
- 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
- Install "SSH Plugin" in Jenkins
- Navigate to Manage Jenkins → Configure System → SSH remote hosts
- Add your remote server details
- In your job configuration, add "Send files or execute commands over SSH" build step
Jenkins Clustering
- In Jenkins UI, navigate to Manage Jenkins → Configure Global Security
- Set "TCP port for JNLP agent" (e.g., 30000)
- Navigate to Manage Jenkins → Manage Nodes → New Node
- Create a new node with appropriate name and labels
- In your job configuration, under "General" → "Restrict where this project can be run", specify the node label
Role-Based Authorization in Jenkins
- Install "Role-based Authorization Strategy" plugin
- Navigate to Manage Jenkins → Configure Global Security
- Select "Role-Based Strategy" under Authorization
- Create users via Manage Jankins → Manage Users
- Assign roles via Manage and Assign Roles:
- Project roles: Define permissions for specific jobs
- Global roles: Define system-wide permissions
Implementing Jenkins Pipelines
- Install "Pipeline" plugin
- Create a new "Pipeline" project
- 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
- Install "GitLab Plugin" in Jenkins
- Configure GitLab credentials in Jenkins
- In Jenkins system settings, configure GitLab connection
- In your job configuration:
- Check "Build when a change is pushed to GitLab"
- Set GitLab CI Service URL
- Configure trigger events and branch filters
- 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
- Set up Tomcat server with proper permissions
- Push Java application code to Git repository
- Create a new Maven project in Jenkins
- Configure Git source management
- Set build goals to "clean install -Dmaven.test.skip=true"
- Add "Deploy war/ear to a container" post-build action:
- WAR/EAR files: **/*.war
- Container: Tomcat 8.x
- Tomcat URL: http://target-server:8080
- Credentials: Tomcat admin credentials
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.