PHP test and build

Updated at:

A PHP test and build pipeline automates code checkout, compilation, unit testing, and result logging to reduce manual work and help developers identify and resolve issues faster.

Step 1: Create a test and build pipeline

  1. Log on to the Flow console. In the upper-right corner, click Create Pipeline.

  2. On the Select a pipeline template page, select PHP · Build, deploy to Alibaba Cloud ECS/self-hosted server, and then click Create.

Step 2: Configure the pipeline source

  1. In the Pipeline Source stage, click Add Pipeline Source.

  2. Select Sample Code Source. For Code Type, select PHP, and then click Add.

Step 3: Configure testing and build

In the Test stage, select a build cluster and build environment to run build jobs.

  1. Configure the basic parameters.

    Parameter

    Description

    Job Name

    You can specify a custom job name or use the default name.

    Build Cluster

    The following build clusters are supported:

    Build Environment

    The following build environments are supported:

    • Specify Container Environment (Alibaba Cloud DevOps default build cluster): Flow starts a specified container on the build machine and performs the build in that container. You can select from the provided Flow System Images or specify your own custom image as the runtime environment.

    • Default VM Environment (supported only for self-hosted build clusters): You must select a build node from the Specify Build Nodes list. Flow installs the required environment and runs the job directly on the build machine. To speed up the build process, we recommend that you install the necessary SDKs and environment dependencies on the machine beforehand.

    • Default Environment (Alibaba Cloud DevOps default build cluster/self-hosted build cluster): (Deprecated) Flow uses a default container image based on the job type. The image comes pre-installed with the required SDKs and environment, and cannot be changed.

    Note

    During job execution, the build environment is exclusively allocated to you. You have full control over the environment and can customize the execution scripts.

    Download Pipeline Source

    When enabled, the configured source files are downloaded to the working directory. The following options are available:

    • Download All Pipeline Sources

    • Do Not Download Pipeline Sources

    • Download Selected Pipeline Sources

  2. Configure the task steps.

    Specified container and default VM

    Unit testing

    1. In an Empty Job > New Stage, click Add Step and select Build > Execute Commands to configure the execution commands.

    2. Then, click Add Step again and select Test > Unit Test Report to configure the unit test report parameters.

    Parameter

    Description

    Step name

    A custom name for the step.

    Container image address

    The full container image address. This parameter is required only for the Specify container environment option.

    Provide the full image address, for example, build-steps-public-registry.cn-beijing.cr.aliyuncs.com/build-steps/php:<version_number>.

    Execute commands

    Specify the correct PHP version in your code repository. Otherwise, commands may fail.

    mkdir report
    php --version
    wget https://rdc-public-software.oss-cn-hangzhou.aliyuncs.com/composer/2.1.5/composer.phar -O /usr/local/bin/composer
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
    composer install
    composer require --dev phpunit/phpunit:^10
    vendor/bin/phpunit --testdox-html ${PROJECT_DIR}/report/index.html
    echo "=================="
    cat report/index.html
    echo "=================="

    Test report file path

    Path to the test report file. The directory containing this file is uploaded. Example: report/index.html.

    Testing tool

    The testing tool or framework. Select Php-UnitTest.

    Stop pipeline on test failure

    If enabled, the pipeline stops when a unit test fails. Default: Enabled.

    PHP build

    In the PHP Build stage, select a build environment based on the selected build cluster to run the build job.

    1. Configure build environment parameters and related commands.

      Parameter

      Description

      Container image address

      Provide the full image address, for example, build-steps-public-registry.<region_ID>.cr.aliyuncs.com/build-steps/alinux3:latest. This parameter is required only for the Specify container environment option.

      Use private image repository

      Add a service connection. For more information, see Manage service connections. Required only for the Specify container environment option.

      Download pipeline source

      When enabled, the pipeline downloads your configured source files to the working directory.

      Execute commands

      The default command for PHP is php --version.

    2. In the Upload Artifact task step, configure the Upload Method and related parameters.

      Select an upload method and configure its parameters.

      Upload to Packages

      Parameters:

      Parameter

      Description

      Add Service Connection

      Click Add Service Connection and follow the on-screen instructions to create a service connection from Flow to Packages. For more information, see Manage service connections.

      Repository

      After the service connection is created, select a Generic repository in Packages as the destination repository.

      Note

      For more information about Generic repositories, see Manage generic artifacts.

      Artifact Name

      This name distinguishes the output from different builds and is used by deployment jobs. We recommend using dynamic variables. The default name is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

      Artifact Version

      The version uniquely identifies your artifact file. You cannot push an artifact with a duplicate version to the same repository. We recommend using a dynamic variable, such as ${DATETIME}.

      Package Path

      The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

      Include package path in artifact

      If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

      Archive to public storage

      Parameters:

      Parameter

      Description

      Artifact Name

      This name distinguishes the output from different builds and is used by deployment jobs. We recommend using dynamic variables. The default name is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

      Package Path

      The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

      Include package path in artifact

      If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

    Default environment

    Unit testing

    In a New Stage > Empty Job, click Add Step, select Test > PHP Unit Test, and configure the task parameters.

    Parameter

    Description

    Step name

    A custom name for the step.

    PHP version

    Select a pre-installed PHP version based on your requirements.

    Test command

    Set the PHP unit test command based on your requirements.

    Report relative path

    Set the relative path for the output report based on your requirements.

    Redline rule

    Set conditions to determine whether the task succeeds or fails.

    Example configuration: Set PHP Version to 8.1. For Test Command, run composer install and then phpunit --testdox-html index.html. Set Report Relative Path to index.html. For Redline Rule, set the test pass rate to be greater than 100.

    PHP build

    By default, pipelines created from this template use the specified container environment. Switching to the Default Environment invalidates the existing task steps. You must clear the configuration and re-add the steps.

    1. Click Add Step > Build > PHP Build and configure the build commands.

      Parameter

      Description

      Step name

      A custom name for the step.

      PHP version

      Select the appropriate PHP version for your project.

      Execute commands

      The default build command for PHP is php -v.

    2. Click Add Step > Upload > Upload Artifact and configure the artifact-related parameters.

      Select an upload method and configure its parameters.

      Upload to Packages

      Parameters:

      Parameter

      Description

      Add Service Connection

      Click Add Service Connection and follow the on-screen instructions to create a service connection from Flow to Packages. For more information, see Manage service connections.

      Repository

      After the service connection is created, select a Generic repository in Packages as the destination repository.

      Note

      For more information about Generic repositories, see Manage generic artifacts.

      Artifact Name

      This name distinguishes the output from different builds and is used by deployment jobs. We recommend using dynamic variables. The default name is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

      Artifact Version

      The version uniquely identifies your artifact file. You cannot push an artifact with a duplicate version to the same repository. We recommend using a dynamic variable, such as ${DATETIME}.

      Package Path

      The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

      Include package path in artifact

      If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

      Archive to public storage

      Parameters:

      Parameter

      Description

      Artifact Name

      This name distinguishes the output from different builds and is used by deployment jobs. We recommend using dynamic variables. The default name is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

      Package Path

      The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

      Include package path in artifact

      If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

    Host deployment

    In the Host Deployment stage, deploy the artifact from the PHP build stage to hosts in a host group. For more information, see Host deployment.

    Set Host group to go-test-env, Download path to /home/admin/app/package.tgz, and Execution user to root. In the deployment script, add the decompression command tar zxvf /home/admin/app/package.tgz -C /home/admin.

Step 4: Run and view the pipeline job

  1. After configuring the pipeline, click Save and Run in the upper-right corner. In the dialog box that opens, click Run to start the pipeline.

  2. After the pipeline job is complete, click Logs at the bottom right of the job node to view the execution logs. A successful pipeline run completes three stages: PHP unit test (5 of 5 test cases passed, 100% pass rate), PHP build (generates the build artifact), and Host deployment. The total runtime is about 1 minute and 41 seconds.

  3. If you uploaded the artifact to an organization's private generic artifact repository, you can view the uploaded artifact in the corresponding repository in Packages. For more information, see Manage generic artifacts.