1. Introduction to Jenkins
Jenkins is a widely used open-source automation server that helps you build, test, and deploy applications. It provides hundreds of plugins to support building, deploying, and automating your projects.
2. Prerequisites
- Operating System: Linux (Ubuntu/Debian commands are used here)
- Java: Jenkins requires Java (preferably Java 11 or later).
- Internet Connection: For downloading packages, plugins, and source code.
3. Installing Jenkins
3.1 Update Your System and Install Java
First, update your package lists and install Java. Jenkins typically runs on Java 11.
Check the Java version to confirm installation:
3.2 Add Jenkins Repository and Key
-
Add the Jenkins Debian repository key:
-
Add the Jenkins repository:
-
Update the package database:
3.3 Install Jenkins
Now install Jenkins with:
3.4 Start and Enable Jenkins Service
Start Jenkins immediately and enable it to start on boot:
3.5 Adjust the Firewall (Optional)
If you’re using UFW (Uncomplicated Firewall), allow traffic on port 8080 (default for Jenkins):
4. Accessing and Initial Setup
4.1 Open the Jenkins Web Interface
Open a browser and navigate to:
4.2 Unlock Jenkins
The first time you access Jenkins, it will ask for an initial admin password. Retrieve it using:
Copy the password from the terminal and paste it into the browser prompt.
4.3 Customize Jenkins
After unlocking, you can:
- Install Suggested Plugins: This installs a set of common plugins.
- Create the First Admin User: Set up your username, password, and full name.
- Instance Configuration: Confirm the URL (e.g.,
http://<your-server-ip>:8080
).
5. Configuring Jenkins
5.1 Install Additional Plugins
You can install more plugins as needed:
- Go to Manage Jenkins > Manage Plugins.
- Browse the Available tab to install plugins like Git, Pipeline, Blue Ocean, etc.
5.2 Global Tool Configuration
Under Manage Jenkins > Global Tool Configuration, configure:
- JDK: Point to your installed Java (if not auto-detected).
- Git: Ensure Jenkins knows where Git is installed (often
/usr/bin/git
).
6. Creating Your First Job
6.1 Creating a Freestyle Project
-
Create New Job:
- Click New Item.
- Enter a name (e.g.,
MyFirstJob
) and select Freestyle project. - Click OK.
-
Configure the Job:
- Source Code Management: If using Git, select Git and enter your repository URL.
- Build Triggers: Optionally set up triggers like Poll SCM.
- Build Steps: Add a build step (e.g., “Execute shell”) and enter a command:
- Post-build Actions: Optionally add actions like archiving artifacts.
-
Save and Run:
- Click Save.
- On the job’s page, click Build Now.
- Monitor progress by clicking on the build number and then Console Output.
7. Using Jenkins Pipeline
7.1 Creating a Pipeline Job
-
Create a New Pipeline:
- Click New Item, enter a name (e.g.,
MyPipeline
), and select Pipeline. - Click OK.
- Click New Item, enter a name (e.g.,
-
Define Your Pipeline Script:
- In the Pipeline section, choose Pipeline script.
- Enter a simple pipeline script:
-
Save and Run:
- Click Save.
- Click Build Now.
- Monitor the pipeline progress under the Blue Ocean view (if installed) or by clicking the build and viewing its logs.
8. Integrating Jenkins with Git
8.1 Configure Git Integration
-
Install the Git Plugin:
- In Manage Jenkins > Manage Plugins, ensure the Git plugin is installed.
-
Create a Job with Git SCM:
- When configuring a new Freestyle or Pipeline job, go to Source Code Management.
- Select Git and enter your repository URL.
- If needed, add credentials by clicking Add next to the Credentials field.
-
Using a Jenkinsfile from a Repository:
- Create a Pipeline job and select Pipeline script from SCM.
- Choose Git as the SCM, enter the repository URL, and specify the branch.
- Set the Script Path (default is
Jenkinsfile
).
9. Advanced Jenkins Configurations
9.1 Managing Plugins and Updates
- Regularly update plugins via Manage Jenkins > Manage Plugins > Updates.
- Explore additional plugins to support various build, test, and deployment strategies.
9.2 Distributed Builds with Jenkins Agents
-
Set Up an Agent:
- In Manage Jenkins > Manage Nodes and Clouds, click New Node.
- Configure the node details (remote machine’s name, remote root directory, etc.).
-
Launch Agent:
- Follow the instructions provided by Jenkins to launch the agent (often via a Java Web Start or SSH).
9.3 Backup and Maintenance
- Backup Jenkins Configuration:
- Regularly back up the
/var/lib/jenkins
directory.
- Regularly back up the
- Upgrade Jenkins:
- Update Jenkins via your package manager:
- Update Jenkins via your package manager:
10. Final Thoughts
-
Explore Further:
Jenkins is highly extensible. Look into creating shared libraries for pipelines, using Blue Ocean for a modern UI, and automating complex workflows. -
Documentation and Community:
Refer to the official Jenkins documentation and community forums for advanced topics and troubleshooting.
This tutorial should help you get started with Jenkins—from installation and initial configuration to creating jobs and setting up pipelines for continuous integration and continuous delivery (CI/CD). Enjoy automating your builds and deployments!
Comments
Post a Comment