Редактиране

Споделяне чрез


Build and test Go projects

Azure DevOps Services

Use a pipeline to automatically build and test your Go projects.

Create your first pipeline

New to Azure Pipelines? If so, then we recommend you try this section before moving on to other sections.

Fork the following repo at GitHub:

https://github.com/MicrosoftDocs/pipelines-go

Sign in to Azure Pipelines

Sign-in to Azure Pipelines. After you sign in, your browser goes to https://dev.azure.com/my-organization-name and displays your Azure DevOps dashboard.

  1. In a browser, go to dev.azure.com and sign in.
  2. Select your organization.
  3. Create a new project by selecting New project or Create project if creating the first project in the organization.
  4. Enter a Project name.
  5. Select the Visibility for your project.
  6. Select Create.
  1. In a browser, go to your Azure DevOps Server.
  2. Select your collection.
  3. Create a new project by selecting New project or Create project if creating the first project in the collection.
  4. Enter a Project name.
  5. Select the Visibility for your project.
  6. Select Create.

Create the pipeline

  1. Sign in to your Azure DevOps organization and go to your project.

  2. Go to Pipelines, and then select New pipeline or Create pipeline if creating your first pipeline.

  3. Do the steps of the wizard by first selecting GitHub as the location of your source code.

  4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. When you see the list of repositories, select your repository.

  6. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve & install.

When the Configure tab appears, select Go. Your new pipeline appears, with the azure-pipelines.yml YAML file ready to be configured. See the following sections to learn some of the more common ways to customize your pipeline.

Build environment

You can use Azure Pipelines to build your Go projects without setting up any infrastructure of your own. You can use Linux, macOS, or Windows agents to run your builds.

Update the following snippet in your azure-pipelines.yml file to select the appropriate image.

pool:
  vmImage: 'ubuntu-latest'

Modern versions of Go are pre-installed on Microsoft-hosted agents. For the exact versions of pre-installed Go, refer to Microsoft-hosted agents in Azure Pipelines.

Set up Go

Starting with Go 1.11, you no longer need to define a $GOPATH environment, set up a workspace layout, or use the dep module. Dependency management is now built in.

This YAML implements the go get command to download Go packages and their dependencies. It then uses go build to generate the content that is published with PublishBuildArtifacts@1 task.

trigger: 
 - main

pool:
   vmImage: 'ubuntu-latest'

steps: 
- task: GoTool@0
  inputs:
    version: '1.13.5'
- task: Go@0
  inputs:
    command: 'get'
    arguments: '-d'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
- task: Go@0
  inputs:
    command: 'build'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
- task: CopyFiles@2
  inputs:
    TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
  inputs:
     artifactName: drop

Build

Use go build to build your Go project. Add the following snippet to your azure-pipelines.yml file:

- task: Go@0
  inputs:
    command: 'build'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

Test

Use go test to test your go module and its subdirectories (./...). Add the following snippet to your azure-pipelines.yml file:

- task: Go@0
  inputs:
    command: 'test'
    arguments: '-v'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

When you're ready, Commit a new azure-pipelines.yml file to your repository and update the commit message. Select Save and run.

Save and run button in a new YAML pipeline

If you want to watch your pipeline in action, select the build in the Jobs option on your Azure Pipelines dashboard. Pipeline build in action when the Azure Pipelines Jobs option is selected.

Because your code appeared to be a good match for the Go template, we automatically created your pipeline.

You now have a working YAML pipeline (azure-pipelines.yml) in your repository that's ready for you to customize!

When you're ready to make changes to your pipeline, select it in the Pipelines page, and then Edit the azure-pipelines.yml file.

Tip

To make changes to the YAML file as described in this article, select the pipeline in Pipelines page, and then select Edit to open an editor for the azure-pipelines.yml file. Screenshot showing how to edit the Azure Pipeline from the dashboard with more option selected and Edit highlighted.

Build an image and push to container registry

For your Go app, you can also build an image and push it to a container registry.

Go extension for Visual Studio Code (Microsoft)