Help Center/ CodeArts Artifact/ Getting Started/ Releasing/Obtaining a Go Component via a Build Task
Updated on 2024-11-11 GMT+08:00

Releasing/Obtaining a Go Component via a Build Task

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

Prerequisites

Releasing a Component to a Go Repository

  1. Download the configuration file.

    1. Log in to CodeArts Artifact and access the Go repository. Click Tutorial on the right of the page.
    2. In the displayed dialog box, click Download Configuration File.

  2. Configure a repository.

    1. Go to Repo. Create a Go repository. For details, see Creating a Repository Using a Template. 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 . 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 a 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.
      • 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 variables 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 Save and Run on the right of the page to start the build task.

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

Obtaining a Dependency from a Go Repository

The following procedure uses the Go component released in Releasing a Component to a Go Repository as an example to describe how to obtain a dependency from a Go repository.

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

    1. On the Repo page, select a 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.
      • 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 Save and Run on the right of the page 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