Operations 6 min read

Using Jenkins Pipeline for Continuous Integration and Deployment of PHP Applications

This article provides a step‑by‑step guide on setting up Jenkins Pipeline to automate the continuous integration, testing, and deployment of PHP applications, covering prerequisite installations, plugin configuration, Jenkinsfile creation with stages for checkout, build, test, and deployment, and how to run the pipeline.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using Jenkins Pipeline for Continuous Integration and Deployment of PHP Applications

Jenkins is a widely used continuous integration and deployment tool that offers many plugins and features to simplify build and deployment processes. Jenkins Pipeline, a newer plugin, lets users define CI/CD workflows using a full‑featured domain‑specific language (DSL).

Preparation

Before starting, ensure the following preparations are completed:

1. Install and configure Jenkins according to the official documentation.

2. Install and configure the required plugins via Jenkins' plugin manager:

Pipeline – supports Jenkins Pipeline syntax.

Git – pulls code from a Git repository.

PHP – provides commands and scripts for PHP.

Deploy to container – deploys the PHP application to the target server.

3. Configure a Git repository to host the PHP source code and ensure access permissions.

Create Jenkins Pipeline

1. Open Jenkins' management page and create a new Pipeline project.

2. In the Pipeline configuration, set "Definition" to "Pipeline script from SCM".

3. Choose Git as the SCM and enter the repository URL.

4. Specify the path to the Jenkinsfile in the "Script Path" field. The Jenkinsfile defines the entire pipeline workflow.

5. Save and apply the changes.

Write Jenkinsfile

The Jenkinsfile is the core file that defines the pipeline stages and steps. Below is a simple example:

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')]
            }
        }
    }
}

The Jenkinsfile defines four stages: Checkout, Build, Test, and Deploy. Each stage contains steps that perform specific actions such as pulling code, installing dependencies, running tests, and deploying the built artifact.

Note that parameters like credentialsId and containerId must be adjusted to match your environment.

Run Jenkins Pipeline

After the Jenkinsfile is written, trigger the pipeline by clicking the "Build Now" button on the pipeline’s page. Jenkins will execute the defined stages sequentially, and you can monitor each step’s output in the build logs.

When the build succeeds, the PHP application is packaged and deployed to the target server. Verify the deployment by accessing the server’s URL.

Summary

Using Jenkins Pipeline streamlines and accelerates the continuous packaging and deployment process for PHP applications. By defining a pipeline file and leveraging appropriate plugins, you can automate code checkout, dependency installation, testing, and deployment, leading to faster iteration, higher release quality, and improved development efficiency.

CI/CDautomationDevOpsPHPpipelineJenkins
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.