Using Jenkins Pipeline for Continuous Integration and Deployment of PHP Applications
This article explains how to set up Jenkins and its Pipeline plugin to automate the continuous integration, testing, and deployment of a PHP application, covering prerequisite installations, Jenkinsfile creation with stages for checkout, build, test, and deploy, and how to run and verify the pipeline.
Preparation
Before starting, ensure Jenkins is installed and configured, install required plugins (Pipeline, Git, PHP, Deploy to container), and have the PHP source code hosted in a Git repository with proper access permissions.
Create Jenkins Pipeline
In Jenkins, create a new Pipeline project, set the definition to "Pipeline script from SCM", choose Git as the SCM, provide the repository URL, and specify the path to the Jenkinsfile.
Write Jenkinsfile
The Jenkinsfile defines the CI/CD workflow. A simple example includes four stages: Checkout, Build, Test, and Deploy. The code below shows the full Jenkinsfile:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/example/repo.git'
}
}
stage('Build') {
steps {
sh 'composer install'
}
}
stage('Test') {
steps {
sh 'vendor/bin/phpunit'
}
}
stage('Deploy') {
steps {
deploy adapters: [glassfish(credentialsId: 'credential-id', containerId: 'container-id', contextPath: '', war: '**/*.war')]
}
}
}
}Each stage performs specific actions: checking out code from Git, installing dependencies with Composer, running PHPUnit tests, and deploying the built artifact using the Deploy to container plugin. Parameters such as credentialsId and containerId must be adjusted to match your environment.
Run Jenkins Pipeline
After saving the Jenkinsfile, trigger the pipeline by clicking "Build Now". Jenkins will execute the defined stages sequentially, and you can monitor progress and logs in the build console. Successful execution results in the PHP application being packaged and deployed to the target server, which can be verified by accessing its URL.
Conclusion
Using Jenkins Pipeline streamlines and accelerates the continuous integration, testing, and deployment process for PHP applications. By defining the pipeline in code and leveraging appropriate plugins, teams can automate code checkout, dependency installation, testing, and deployment, leading to faster iteration cycles and higher software quality.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.