Go test and build

Updated at:

A Go test and build pipeline automates code checkout, compilation, unit testing, results recording, and test statistics collection, giving developers detailed logs to find and fix 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. In the Select Pipeline Template dialog box, select Go · Test, Build, and then click Create.

Step 2: Configure pipeline source

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

  2. Select Sample Code Source, set Code Type to Go, and click Add.

Step 3: Configure test job

Flow runs test jobs in separate Test stages. Each job can use a different build cluster and build environment.

  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 Go unit test job.

    Configure the test job for the build environment you selected.

    Container and VM environments

    1. In the Install Go step, select an appropriate Golang Version for your project and set the GOPATH environment variable to your Go workspace directory.

    2. Add and configure the Execute Test Command step. Set the Step name to Execute Test Command, and in the Execute Command field, enter the shell commands. For example, use mkdir -p golang-report to create a report output directory, and then run go test to execute unit tests, outputting the results as a jsonl report.

    3. In the Unit test report step, configure the following parameters.

      Parameter

      Description

      Test report file path

      The path to the test report file. Must point to a specific file. The directory containing this file is uploaded automatically. Example: golang-report/report.jsonl.

      Test tool

      The testing tool or framework used. Example: Golang-Test.

      Fail on error

      When enabled, the pipeline stops if a unit test fails. Enabled by default.

    Default environment

    Note

    By default, the pipeline template uses the specified container environment. If you switch to the Default environment, the existing job steps become invalid. You must clear the existing configuration and add the required steps again.

    Navigate to Add Step > Test > Golang Unit Test, and configure the parameters.

    Parameter

    Description

    Step name

    A custom name for the step.

    Go version

    Select a Go version based on your project requirements.

    Test command

    The command to run tests and generate a unit test report.

    Test report directory

    The directory where the HTML test report is generated. The pipeline uploads all files from this directory. Example: golang-report.

    Test report file

    The test report file generated by Golang, such as report.jsonl. The pipeline parses and displays the report content.

    Test report entry file

    The entry file for the HTML test report, such as index.html. This page opens by default when you access the generated report link.

    Quality gate

    Conditions that determine whether the job succeeds or fails.

    For example, set Go version to go1.14.x, configure the Test command with export GOPROXY, mkdir -p golang-report, and go test ./... -v -json > golang-report/report.jsonl to generate a test report, and set a quality gate rule requiring 100% test pass rate.

Step 4: Configure build job

In the Go build stage, configure the build job for the build environment you selected.

Container and VM environments

  1. In the Install Go step, select an appropriate Golang Version for your project and configure the GOPATH environment variable to point to your Go workspace directory.

  2. In the Execute Golang build command step, enter make build in the Execute Command area. The default command is make build.

  3. In the Upload artifact job, 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

Note

By default, the pipeline template uses the specified container environment. If you switch to the Default environment, the existing job steps become invalid. You must clear the existing configuration and add the required steps again.

  1. Navigate to Add Step > Build > Golang Build and configure the build commands.

    Parameter

    Description

    Step name

    A custom name for the step.

    Golang version

    Select a Golang version for your project.

    Build command

    The build command to run. The command runs in the root directory of the code repository. Set credentials for downloading dependencies.

    Note

    Default build command for Golang:

    # goproxy.cn is used by default.
    export GOPROXY=https://goproxy.cn
    # Input your command here.
    make build

    Expand Advanced Settings to configure the Build environment specification (default: DEFAULT) and Timeout (default: 240 minutes).

  2. Navigate to Add Step > Upload > Upload artifact and configure the artifact 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.

Step 5: Run pipeline and view results

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

  2. After the pipeline finishes, click Logs in the lower-right area of the job node to view the execution log. The Recent Runs page shows the run details: the pipeline source section shows the code repository and branch, the Test stage shows Go unit test results including line coverage and pass rates, and the Build stage shows Go build results and the generated artifact, such as Artifacts_3428715.

  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 Manage generic artifacts.