Continuous integration and deployment is the process of automatically building, testing, and deploying code changes to production servers. This approach streamlines the development process and ensures that changes are implemented quickly and reliably. Jenkins is a popular open-source tool that makes it easy to set up continuous integration and deployment pipelines.
Setting up Jenkins on Linux
Before we can start using Jenkins, we need to install it on a Linux server. Here are the steps to follow:
- Update your system: Run the following command to update your system’s package list:
sudo apt-get update
- Install Java: Jenkins requires Java to run. Install it by running the following command:
sudo apt-get install default-jdk
- Add the Jenkins repository: Jenkins isn’t available in the default package repositories, so we need to add its repository to our system. Run the following commands:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
- Install Jenkins: Run the following command to install Jenkins:
sudo apt-get install jenkins
- Start Jenkins: Jenkins should start automatically after installation, but you can start it manually by running the following command:
sudo systemctl start jenkins
You should see the Jenkins login page.
- Set up Jenkins: Follow the on-screen instructions to set up Jenkins. You’ll be asked to create an admin user and choose your plugins. Once you’ve completed the setup process, you’re ready to start using Jenkins!
Configuring Jenkins for Continuous Integration
- Create a new job: Run the following command to create a new Jenkins job:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ create-job <job-name>
- Configure the job: Run the following command to configure the new job:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ create-job <job-name> < <job-config.xml>
Replace <job-config.xml> with the path to a configuration file for your job. You can create this file manually or export it from an existing job in the Jenkins UI.
- Build the job: Run the following command to trigger a build of the new job:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ build <job-name>
- Test the job: Run the following command to view the console output of the latest build:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ console <job-name>
You can also use the following command to download the console output as a file:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ get-log <job-name> > <job-log.txt>
Configuring Jenkins for Continuous Deployment
- Install the plugin: Run the following command to install the “Deploy to container” plugin on Jenkins:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ install-plugin deploy
- Configure the plugin: Run the following command to configure the “Deploy to container” plugin:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ groovysh < deploy-config.groovy
Replace deploy-config.groovy with the path to a Groovy script that sets up the plugin configuration. Here’s an example script:
import jenkins.model.*
import hudson.plugins.deploy.*
def instance = Jenkins.getInstance()
def descriptor = instance.getDescriptorByType(DeployPublisher.DescriptorImpl.class)
def credentials = instance.credentials.get("my-ssh-credentials")
descriptor.addAppServer(new TomcatApplicationServer(
"tomcat8", "Tomcat 8", "/usr/share/tomcat8"
))
descriptor.addContainer(new RemoteContainer(
"my-remote-server", "192.168.1.10", "22", credentials, "/opt/my-app"
))
This script adds a Tomcat application server and a remote container to the plugin configuration.
- Create a new job: Run the following command to create a new Jenkins job:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ create-job <job-name> < <job-config.xml>
Replace job-name with the name you want to give your job, and replace job-config.xml with the path to a configuration file for your job. Here’s an example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<actions/>
<description>Deploy my-app to my-remote-server</description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/my-org/my-repo.git</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="empty-list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.plugins.deploy.DeployBuilder>
<deployables>
<hudson.plugins.deploy.Deployable>
<id>my-app</id>
<contextPath>/my-app</contextPath>
<war>${WORKSPACE}/target/my-app.war</war>
<remote>
<containerId>my-remote-server</containerId>
<userName>my-ssh
</userName>
<password>${SECRET_PASSWORD}</password>
</remote>
<onFailure>false</onFailure>
<onSuccess>true</onSuccess>
</hudson.plugins.deploy.Deployable>
</deployables>
</hudson.plugins.deploy.DeployBuilder>
</builders>
<publishers/>
<buildWrappers/>
</project>
This configuration sets up a Git SCM, builds the project, and deploys it to the remote container using the “Deploy to container” plugin.
- Trigger the job: Run the following command to trigger the new job:
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ build <job-name>
- Verify the deployment: Check the logs of the job to make sure that the deployment was successful. You can also check the remote container to make sure that the application was deployed correctly.
Congratulations! You’ve successfully set up continuous integration and deployment with Jenkins on Linux. With this configuration, every time you push changes to your Git repository, Jenkins will automatically build and deploy the latest version of your application to your remote container. This saves you time and ensures that your application is always up-to-date.