Help Center/ CodeArts Artifact/ Best Practices/ Publishing/Obtaining a Go Package via a Build Task
Updated on 2026-03-13 GMT+08:00

Publishing/Obtaining a Go Package via a Build Task

In enterprise software development, teams often use private packages to build and deploy application for secure and exclusive code. However, fetching dependencies from a repository can be challenging, sometimes causing build failures. This practice describes how to publish a private package to a Go repository via a build task and fetch its dependencies from the repository to complete the build task, resolving the challenges mentioned earlier.

Billing

This function requires CodeArts Repo and CodeArts Build. You need to purchase a CodeArts package.

Prerequisites

  • You already have a project. If no project is available, create one. For example, create a project named project_go.

Publishing a Package to a Go Repository

  1. Download the configuration file.

    1. Use your Huawei Cloud account to access self-hosted repos.
    2. Select the created Go repository. Click Tutorial on the right of the page.
    3. In the displayed dialog box, click Download Guide File.

  2. Configure a repository.

    1. Go to Repo and create a Go repository. For details, see Creating a Repository This procedure uses the Go web Demo template.
    2. Prepare the go.mod and upload it to the root directory of the repository. For details, see Uploading Code Files to CodeArts Repo The following figure shows the go.mod file used in this example.

  3. Configure and run a build task.

    1. On the Repo page, select the repository and click Create Build Task in the upper right.

      Select Blank Template and click OK.

    2. Add the Build with Go action.

    3. Edit the Build with Go action.
      1. Select the desired tool version. In this example, go-1.13.1 is used.
      2. Add the following commands to the end of the Build with Go action.
        cd $GOPATH/src/
        zip -D -r v1.0.0.zip  example.com/
      3. Copy the steps under Configuring Go Environment Variables on LINUX from the file to the command box.

        Copy the Go publish command segment in the configuration file to the command box, and replace the parameters in the commands by referring to Go Modules. (In this example, the package version is v1.0.0.)

        Example:

        curl -u "<USER>:<PASSWORD>" -X PUT https://<URL>//example.com/demo/@v/v1.0.0.zip -T $GOPATH/src/v1.0.0.zip
        curl -u "<USER>:<PASSWORD>" -X PUT https://<URL>/example.com/demo/@v/v1.0.0.mod -T $GOPATH/src/example.com/demo/go.mod
    4. Click Save and Run on the right of the page to start the build task.

      When the message build successful is displayed, go to the Go repository page and find the uploaded Go package.

Obtaining a Dependency from a Go Repository

  1. Download the configuration file by referring to Publishing a Package to a Go Repository.
  2. Go to Repo and create a Go repository. For details, see Creating a Repository This procedure uses the Go web Demo template.
  3. Configure and run a build task.

    1. On the Repo page, select the repository and click Create Build Task in the upper right.

      Select Blank Template and click OK.

    2. Add the Build with Go action.
    3. Edit the Build with Go action.
      1. Select the desired tool version. In this example, go-1.13.1 is used.
      2. Delete the existing command. Then open the downloaded configuration file and copy the steps under Configuring Go Environment Variables on Linux to the command box.
      3. Copy the Go download commands in the configuration file to the command box and replace the <modulename> parameter with the actual value. (In this example, the parameter is set to example.com/demo).

        Example:

        go get -v example.com/demo@v1.0.0

  4. Click Save and Run on the right of the page to start the build task.

    If the message can't request explicit version of path in main module is displayed when you run the download command, check whether the package to download is built from the current repository.

    When the message build successful is displayed, view the task details. If information similar to the following is found in the log, the dependency has been downloaded from the Go repository.

Go Modules

This section describes how to build and upload Go packages through Go modules.

Perform the following steps:

  1. Create a source folder in the working directory.
    mkdir -p {module}@{version}
  2. Copy the code to the sources folder.
    cp -rf . {module}@{version}
  3. Compress the package into a ZIP package.
    zip -D -r [package name] [package root directory]
  4. Upload the ZIP package and the go.mod file to the self-hosted repo.
    curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/{filePath} -T {{localFile}}

The package directory varies according to the package version. The version can be:

  • Versions earlier than v2.0: The directory is the same as the path of the go.mod file. No special directory structure is required.
  • v2.0 or later:
    • If the first line in the go.mod file ends with /vX, the directory must contain /vX. For example, if the version is v2.0.1, the directory must contain v2.
    • If the first line in the go.mod file does not end with /vN, the directory remains unchanged and the name of the file to be uploaded must contain +incompatible.

The following are examples of package directories for different versions.

  • Versions earlier than v2.0

    The go.mod file is used as an example.

    1. Create a source folder in the working directory.
      The value of module is example.com/demo and that of version is 1.0.0. The command is as follows.
      mkdir -p ~/example.com/demo@v1.0.0
    2. Copy the code to the sources folder.

      The command is as follows (with the same parameter values as the previous command).

      cp -rf . ~/example.com/demo@v1.0.0/
    3. Compress the package into a ZIP package.

      Run the following command to go to the upper-level directory of the root directory where the ZIP package is located.

      cd ~

      Then, use the zip command to compress the code into a package. In this command, the package root directory is example.com and the package name is v1.0.0.zip. The command is as follows.

      zip -D -r v1.0.0.zip  example.com/
    4. Upload the ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file.

      • For the ZIP package, the value of filePath is example.com/demo/@v/v1.0.0.zip and that of localFile is v1.0.0.zip.
      • For the go.mod file, the value of filePath is example.com/demo/@v/v1.0.0.mod and that of localFile is example.com/demo@v1.0.0/go.mod.

      The commands are as follows (replace username, password, and repoUrl with the actual values).

      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/@v/v1.0.0.zip -T v1.0.0.zip
      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/@v/v1.0.0.mod -T example.com/demo@v1.0.0/go.mod
  • v2.0 and later, with the first line in go.mod ending with /vX

    The go.mod file is used as an example.

    1. Create a source folder in the working directory.
      The value of module is example.com/demo/v2 and that of version is 2.0.0. The command is as follows.
      mkdir -p ~/example.com/demo/v2@v2.0.0
    2. Copy the code to the sources folder.

      The command is as follows (with the same parameter values as the previous command).

      cp -rf . ~/example.com/demo/v2@v2.0.0/
    3. Compress the package into a ZIP package.

      Run the following command to go to the upper-level directory of the root directory where the ZIP package is located.

      cd ~

      Then, use the zip command to compress the code into a package. In this command, the package root directory is example.com and the package name is v2.0.0.zip. The command is as follows.

      zip -D -r v2.0.0.zip  example.com/
    4. Upload the ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file.

      • For the ZIP package, the value of filePath is example.com/demo/v2/@v/v2.0.0.zip and that of localFile is v2.0.0.zip.
      • For the go.mod file, the value of filePath is example.com/demo/v2/@v/v2.0.0.mod and that of localFile is example.com/demo/v2@v2.0.0/go.mod.

      The commands are as follows (replace username, password, and repoUrl with the actual values).

      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/v2/@v/v2.0.0.zip -T v2.0.0.zip
      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/v2/@v/v2.0.0.mod -T example.com/demo/v2@v2.0.0/go.mod
  • v2.0 and later, with the first line in go.mod not ending with /vX

    The go.mod file is used as an example.

    1. Create a source folder in the working directory.
      The value of module is example.com/demo and that of version is 3.0.0. The command is as follows.
      mkdir -p ~/example.com/demo@v3.0.0+incompatible
    2. Copy the code to the sources folder.

      The command is as follows (with the same parameter values as the previous command).

      cp -rf . ~/example.com/demo@v3.0.0+incompatible/
    3. Compress the package into a ZIP package.

      Run the following command to go to the upper-level directory of the root directory where the ZIP package is located.

      cd ~

      Then, use the zip command to compress the code into a package. In this command, the package root directory is example.com and the package name is v3.0.0.zip. The command is as follows.

      zip -D -r v3.0.0.zip  example.com/
    4. Upload the ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file.

      • For the ZIP package, the value of filePath is example.com/demo/@v/v3.0.0+incompatible.zip and that of localFile is v3.0.0.zip.
      • For the go.mod file, the value of filePath is example.com/demo/@v/v3.0.0+incompatible.mod and that of localFile is example.com/demo@v3.0.0+incompatible/go.mod.

      The commands are as follows (replace username, password, and repoUrl with the actual values).

      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/@v/v3.0.0+incompatible.zip -T v3.0.0.zip
      curl -u {{username}}:{{password}} -X PUT {{repoUrl}}/example.com/demo/@v/v3.0.0+incompatible.mod -T example.com/demo@v3.0.0+incompatible/go.mod