Jenkins is a powerful automation server that enables developers to build, test, and deploy their applications. One of the key features of Jenkins is its ability to create pipelines, which are a series of automated steps that can be executed in a specific order. In this blog post, we will explore how to create two types of Jenkins pipelines: a basic pipeline that prints "Hello World" and an advanced pipeline that runs a PHP script and its corresponding tests.
1. Basic Jenkins Pipeline: Printing "Hello World"
Step 1: Setting Up Jenkins
Before we dive into creating the pipeline, ensure that you have Jenkins installed and running. You can download it from the official Jenkins website.
Step 2: Create a New Pipeline Job
Open Jenkins in your web browser.
Click on "New Item" in the left sidebar.
Enter a name for your job (e.g., HelloWorldPipeline).
Select "Pipeline" and click "OK."
Step 3: Configure the Pipeline
Scroll down to the "Pipeline" section.
In the "Definition" dropdown, select "Pipeline script."
In the script area, enter the following code:
pipeline {
agent any
stages {
stage('Print Hello World') {
steps {
script {
echo 'Hello World'
}
}
}
}
}
Step 4: Save and Build
Click "Save" to save your pipeline configuration.
Click "Build Now" to run the pipeline.
You can view the console output by clicking on the build number in the "Build History" section.
Output
You should see "Hello World" printed in the console output.
2. Advanced Jenkins Pipeline: Running PHP Code and Tests
In this section, we will create an advanced pipeline that runs a PHP script and its tests. We will use PHPUnit for testing.
Step 1: Create a PHP Script
Create a simple PHP script that prints "Hello World." Create a file named hello.php with the following content:
<?php
function sayHello() {
return "Hello World";
}
echo sayHello();
?>
Step 2: Create a PHPUnit Test
Next, create a PHPUnit test for the PHP script. Create a file named HelloTest.php with the following content:
<?php
use PHPUnit\Framework\TestCase;
class HelloTest extends TestCase {
public function testSayHello() {
$this->assertEquals("Hello World", sayHello());
}
}
?>
Step 3: Set Up Jenkins Pipeline
Create a new pipeline job in Jenkins (e.g., AdvancedHelloWorldPipeline).
In the "Pipeline" section, enter the following code:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
// Checkout the code from GitHub
git branch: 'main', url: 'https://github.com/Ehteshamali1109/hello-test-php'
}
}
stage('Install Dependencies') {
steps {
// Install Composer dependencies
bat 'composer install'
}
}
stage('Run PHP Script') {
steps {
// Run the hello.php file using the Windows command
bat 'php hello.php'
}
}
stage('Run PHPUnit Tests') {
steps {
// Run PHPUnit tests
bat 'vendor\\bin\\phpunit HelloTest.php'
}
}
}
}
Step 4: Save and Build
Click "Save" to save your pipeline configuration.
Click "Build Now" to run the pipeline.
Check the console output for results.
Output
You should see the output of the PHP script and the results of the PHPUnit tests in the console output.
Conclusion
In this blog post, we explored how to create both basic and advanced Jenkins pipelines. The basic pipeline simply prints "Hello World," while the advanced pipeline runs a PHP script and its tests using PHPUnit. Jenkins pipelines are a powerful way to automate your development workflow, and with these examples, you can start building your own pipelines tailored to your project's needs. Happy coding!
Top comments (0)