Help Center> CloudArtifact> Getting Started> Releasing/Obtaining a Self-Hosted Go Component via a Build Task
Updated on 2022-09-29 GMT+08:00

Releasing/Obtaining a Self-Hosted Go Component via a Build Task

This section describes how to release a component to a self-hosted Go repo via a build task and obtain a dependency from the repository for deployment.

Prerequisites

  1. You already have a project. If no project is available, create one.
  2. You have created a self-hosted Go repo.

Releasing a Component to a Self-Hosted Go Repo

  1. Download the configuration file of the self-hosted repo.

    1. Log in to the DevCloud homepage and access the self-hosted Go repo. Click Operation Guide.

    2. In the dialog box that is displayed, click Download Configuration File.

  2. Configure a code repository.

    1. Go to CodeHub. Create a Go repository. For details, see Cloud Repository Creation. This procedure uses the Go web Demo template.
    2. Prepare the go.mod file and upload it to the code repository. The following figure shows the go.mod file used in this example.

  3. Configure and execute a build task.

    1. On the code repository page, click Create Build Task in the upper right. The Create Task page is displayed.

      Select Blank build template and click OK.

    2. Add the Build in Go action.

    3. Edit the Build in Go action.
      • Select the desired tool version. In this example, go-1.13.1 is used.
      • Delete the existing commands, open the configuration file downloaded in 1, and copy the commands for configuring Go environment variable in Linux to the command box.
      • Copy the Go upload command segment in the configuration file to the command box, and replace the parameters in the commands by referring to Go Module Packaging. (In this example, the package version is v1.0.0.)

    4. Click Create and Run to start the build task.

      When a message is displayed indicating build successful, go to the self-hosted repo and find the uploaded Go component.

Obtaining a Dependency from a Self-Hosted Go Repo

The following procedure uses the Go component released in Releasing a Component to a Self-Hosted Go Repo as an example to describe how to obtain a dependency from a self-hosted Go repo.

  1. Download the configuration file of the self-hosted repo by referring to Releasing a Component to a Self-Hosted Go Repo.
  2. Go to CodeHub and create a Go repository. For details, see Cloud Repository Creation. This procedure uses the Go web Demo template.
  3. Configure and execute a build task.

    1. On the code repository page, click Create Build Task in the upper right. The Create Task page is displayed.

      Select Blank build template and click OK.

    2. Add the Build in Go action.

    3. Edit the Build in Go action.
      • Select the desired tool version. In this example, go-1.13.1 is used.
      • Delete the existing commands, open the downloaded configuration file, and copy the commands for configuring Go environment variables in Linux to the command box.
      • 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).

  4. Click Create and Run to start the build task.

    When a message is displayed indicating build successful, view the task details. If information similar to the following is found in the log, the dependency has been downloaded from the self-hosted repo.

Go Module Packaging

This section describes how to build and upload Go components through Go module packaging.

Perform the following steps:

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

The component 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 component 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 source to the source 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 component 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 component 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 component ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file of the self-hosted repo.

      • 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 command is 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 source to the source 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 component 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 component 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 component ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file of the self-hosted repo.

      • 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 command is 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 source to the source 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 component 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 component 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 component ZIP package and the go.mod file to the self-hosted repo.

      Parameters username, password, and repoUrl can be obtained from the configuration file of the self-hosted repo.

      • 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 command is 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