Build and test a Node.js application

更新时间:
复制 MD 格式

Flow provides an all-in-one solution for testing and building Node.js projects. It helps you quickly configure CI/CD pipelines and automate testing, code compilation, dependency management, and artifact uploads. You can configure a Node.js · Test, Build pipeline through the visual interface.

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. In the Select a pipeline template dialog box, select Node.js · Test, Build and click Create.

    image

Step 2: Configure a pipeline source

  1. On the New Pipeline page, go to the Process Configuration tab. In the pipeline source stage, click Add pipeline source.

  2. Add a code source. This example uses the sample code provided by Flow. Click Sample code source > NodeJS > Add.

Step 3: Configure test and build tasks

The Node.js · Test, Build template includes three preset tasks: JavaScript Code Scan, Node.js Unit Testing, and Node.js Build and Upload. These tasks have similar configurations. The following example uses the Node.js Build and Upload task.image

Node.js build and upload configuration

  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, the build environment is allocated to you. You have full control over the build environment and the job execution script.

    Download Pipeline Source

    If you enable this option, 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 Task steps.

    Based on your selected build environment, follow the corresponding configuration instructions below.

    Container and VM environments

    1. Configure .npmrc file

      image

    2. Install Node environment

      Configure parameters such as Node version and npm type as needed.

      Important

      Version selection method supports Select from preset versions, Enter a specific version, or Use the version from the .nvmrc file in the code repository. However, if a .nvmrc file exists, the version specified in the .nvmrc file is used regardless of the selected option.

      image

    3. Execute command

      Enter custom build commands, and select Specify shell to run if needed. To configure environment variables, see environment variables.

      image

    4. Upload artifact

      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

      This 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. You cannot push duplicate versions of the same artifact. 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

      This 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

    The preset pipeline templates in Flow use a container environment. If you switch to the default environment, existing task steps become invalid. Clear the original configuration and re-add task steps as follows.

    1. Node.js build

      In the task steps, click . Modify parameters such as Node version and Build command as needed.

      image

    2. Upload artifact

      In the task steps, click .

      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

      This 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. You cannot push duplicate versions of the same artifact. 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

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

JavaScript code scan parameters

Parameter

Description

Use local ESLint configuration

Uses the repository's own ESLint configuration for scanning instead of the system rule set. If enabled, you must declare the required dependencies in the Dependencies to run local ESLint configuration option and ensure that the repository root contains a .eslintrc.* file.

Dependencies to run local ESLint configuration

Dependencies required to run the repository's ESLint configuration, separated by spaces. This value is passed directly to npm install. For example, if you use eslint-config-standard locally, specify eslint eslint-config-standard, eslint-plugin-standard, eslint-plugin-promise, eslint-plugin-import, and eslint-plugin-node. Set this to * to install all dependencies from the package.json file.

Important

This option is valid only if the Use local ESLint configuration option is enabled.

Enable incremental scan mode

If this option is not enabled, a full code scan is performed.

Excluded subdirectories

The directories or files to exclude from the scan. The format is similar to a .gitignore file. For example, entering test/ excludes all files in the test/ directory from the scan.

Quality gate

Define the success criteria for the code scan based on the following four metrics:

  • Total Issues

  • Blocker Issues

  • Critical Issues

  • Major Issues

Node.js unit testing parameters

Parameter

Description

Execute command

Custom commands for running Node.js unit tests.

Test report path

Path to the test report generated after the test commands run. Example: mochawesome-report/mochawesome.html.

Testing tool

The testing tool or framework to use. Example: Node.js-Mocha.

Step 4: Run the pipeline and view results

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

  2. After the pipeline run is complete, click Logs on the task node to view the execution logs.image

  3. If you uploaded the artifact to a private generic artifact repository, you can view it in the corresponding repository in Packages. For more information, see NPM artifact management.