PHP test and build

更新时间:
复制 MD 格式

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 Alibaba Cloud DevOps console and click Create Pipeline in the upper-right corner.

  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

    Enter a custom name for the job, or use the default.

    Build Cluster

    The following build clusters are supported:

    Build Environment

    The following build environments are supported:

    • Specify Container Environment (for Alibaba Cloud DevOps default build cluster): Flow starts a specified container on the build machine and performs the build in that single-container environment. Alibaba Cloud DevOps provides a list of official images. You can also specify your own image as the runtime environment.

    • Default VM Environment (supported only for private build clusters): You must select Specify Build Nodes. Flow installs the environment and runs the job directly on the build machine. To speed up the job, we recommend installing the required SDKs and environment on the build machine in advance.

    • Default Environment (for Alibaba Cloud DevOps default/private build cluster): Flow uses different default container images based on the job type. These images come pre-installed with the necessary SDKs and environments and cannot be modified. (Deprecated)

    Note

    During job execution, you have full control over the build environment and the job execution script.

    Download Pipeline Source

    If enabled, the configured code source is downloaded to the corresponding working directory. The available options are:

    • 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.

      Private generic artifact repository

      Parameters:

      Parameter

      Description

      Add Service Connection

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

      Repository

      After you add the service connection, you can select a Generic Artifacts repository in Packages as the target repository.

      Note

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

      Artifact Name

      Distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name such as target1.

      Artifact Version

      The version uniquely identifies your artifact. Duplicate versions of the same artifact cannot be pushed. We recommend using a dynamic variable such as ${DATETIME}.

      Packaging Path

      Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

      Include packaging path in artifact

      If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

      Public storage

      Parameters:

      Parameter

      Description

      Artifact Name

      Distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name such as target1.

      Packaging Path

      Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

      Include packaging path in artifact

      If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

    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.

      Private generic artifact repository

      Parameters:

      Parameter

      Description

      Add Service Connection

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

      Repository

      After you add the service connection, you can select a Generic Artifacts repository in Packages as the target repository.

      Note

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

      Artifact Name

      Distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name such as target1.

      Artifact Version

      The version uniquely identifies your artifact. Duplicate versions of the same artifact cannot be pushed. We recommend using a dynamic variable such as ${DATETIME}.

      Packaging Path

      Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

      Include packaging path in artifact

      If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

      Public storage

      Parameters:

      Parameter

      Description

      Artifact Name

      Distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name such as target1.

      Packaging Path

      Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

      Include packaging path in artifact

      If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

    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.