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 Flow console. In the upper-right corner, click Create Pipeline.

  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

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

    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

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.

    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.

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.