Building a Jenkins Pipeline for CI/CD with SonarQube and PHP
In this article, we will walk through the process of setting up a Jenkins Freestyle Project to automate Continuous Integration (CI), Continuous Testing (CT), and Continuous Deployment (CD) for a PHP application. Additionally, we will integrate SonarQube for code quality analysis.
This Jenkins pipeline will perform the following tasks:
- Clone the Git repository containing the PHP application.
2. Install PHP dependencies via Composer.
3. Run PHPUnit tests to validate the application.
4. Run SonarQube analysis to check code quality.
5. Deploy the application to the production server.
Prerequisites
Before you begin, ensure you have:
1. Jenkins installed and running.
2. SonarQube installed and configured on your server.
3. PHP and Composer installed on the Jenkins server.
4. JUnit and PHPUnit configured for testing.
5. GitHub repository containing the PHP application.
6. SSH access configured between Jenkins and the production server.
Step 1: Setting Up a Freestyle Project in Jenkins
To begin creating a Jenkins pipeline using a Freestyle Project, follow these steps:
1. Log in to the Jenkins Dashboard: Open your browser and go to your Jenkins server (usually http://localhost:8080).
2. Create a New Job:
• Click on New Item on the left sidebar.
• Choose Freestyle Project and give it a name (e.g., yourname_php_app_deployment).
• Click OK to proceed.
3. Configure the GitHub Repository:
• Under the Source Code Management section, select Git.
• In the Repository URL field, enter your GitHub repository URL (e.g., https://github.com/novrian6/php_app.git).
• Under Branch Specifier, specify the branch you want to use (e.g., master).
4. Set Build Triggers:
• You can configure Jenkins to trigger builds automatically on commits to the Git repository.
- Select GitHub hook trigger for GITScm polling if you want Jenkins to automatically start a build when there is a push to the GitHub repository. Ensure you’ve set up a webhook in your GitHub repository to notify Jenkins.
for scm polling use the following to run every 15 minutes.
*/15 * * * *
5. Define Build Environment:
• Under Build Environment, you can enable Delete workspace before build starts to ensure that the workspace is clean for each build.
6. Add Build Steps:
• Next, you will define the steps Jenkins should take to build, test, and deploy the PHP application.
Step 2: Define Build Steps in the Freestyle Project
Now, let’s break down the steps for CI, CT, and CD using the Bash script we provided earlier. In Jenkins, we’ll use Execute Shell build steps to run the shell commands.
1. Clean Up Old Files in Jenkins Workspace
In this step, we remove any old files in the Jenkins workspace before starting the build to ensure a clean slate.
• Build Step: Execute Shell
• Command:
echo "Cleaning up old files in Jenkins workspace..."
rm -rf $WORKSPACE/php_app
2. Clone the Repository and Install PHP Dependencies
Next, we clone the repository from GitHub and install any required PHP dependencies using Composer.
• Build Step: Execute Shell
• Command:
echo "Cloning the repository from branch '$BRANCH'..."
git clone --branch $BRANCH $REPO_URL $WORKSPACE/php_app
cd $WORKSPACE/php_app
echo "Installing PHP dependencies..."
composer install --no-interaction --prefer-dist
3. Run PHPUnit Tests
For Continuous Testing (CT), we’ll run PHPUnit tests to ensure that the code is functioning correctly.
• Build Step: Execute Shell
• Command:
echo "Running PHPUnit tests..."
vendor/bin/phpunit tests/ApiTest.php
4. SonarQube Analysis
For Continuous Integration (CI), we’ll run a SonarQube analysis to ensure code quality. You need to have SonarQube installed and running on a server (in this case, http://nn-ubuntu:9000).
• Build Step: Execute Shell
• Command:
echo "Running SonarQube analysis..."
/opt/sonar-scanner/bin/sonar-scanner \
-Dsonar.projectKey=$SONARQUBE_PROJECT_KEY \
-Dsonar.projectName="PHP Application" \
-Dsonar.projectVersion=1.0 \
-Dsonar.sources=. \
-Dsonar.tests=tests \
-Dsonar.php.exclusions=**/*.test.php \
-Dsonar.php.exclusions=**/tests/** \
-Dsonar.php.coverage.reportPaths=coverage.xml \
-Dsonar.host.url=$SONARQUBE_URL \
-Dsonar.login=$SONARQUBE_TOKEN
5. Deploy the Application to Production
After the tests and SonarQube analysis, if everything passes, we proceed with Continuous Deployment (CD) by copying the updated code to the production server.
• Build Step: Execute Shell
• Command:
echo "Deploying to the production server..."
# Clean up the existing files on the production server
ssh $DEPLOY_USER@$DEPLOY_HOST "rm -rf $DEPLOY_PATH/*"
# Copy files from Jenkins workspace to the production server
scp -o StrictHostKeyChecking=no -r $WORKSPACE/php_app/* $DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH
# Set proper permissions and restart the application
ssh $DEPLOY_USER@$DEPLOY_HOST "chmod -R 755 $DEPLOY_PATH && sudo service php-fpm restart"
Step 3: Post-Build Actions
Once the build steps are completed, you can configure Jenkins to take further actions, such as sending notifications via email or integrating with tools like Slack to notify the team about the build status. This is especially useful for monitoring the health of your application.
• Under Post-build Actions, you can select Editable Email Notification to send notifications on build success or failure.
Step 4: Build and Test the Pipeline
Once you’ve configured the build steps, click Save to finalize the configuration. Then, trigger a build by clicking Build Now. Jenkins will:
1. Clone the repository from GitHub.
2. Install dependencies with Composer.
3. Run PHPUnit tests to ensure the code is working.
4. Perform SonarQube analysis to check code quality.
5. Deploy the application to the production server.
You can track the build progress from the Build History section, view logs, and troubleshoot any issues that may arise during the process.
This Jenkins Freestyle Project setup demonstrates a complete CI/CD pipeline for a PHP application. By integrating SonarQube for continuous code quality analysis, running PHPUnit tests for continuous testing, and automating deployment to production, you ensure that your PHP application is always up-to-date, reliable, and of high quality.
By following these steps, you can automate your entire workflow and ensure that your PHP application is consistently deployed with confidence.