Condividi tramite


Build and test Go projects

Servizi di Azure DevOps

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

Accedere ad Azure Pipelines. Dopo l'accesso, il browser passa a https://dev.azure.com/my-organization-name e visualizza il dashboard di Azure DevOps.

  1. In un browser, vai su dev.azure.com e accedi.
  2. Selezionare l'organizzazione.
  3. Creare un nuovo progetto selezionando Nuovo progetto o Crea progetto se si crea il primo progetto nell'organizzazione.
  4. Immettere il nome del progetto.
  5. Seleziona la Visibilità per il tuo progetto.
  6. Fare clic su Crea.

Create the pipeline

  1. Accedere all'organizzazione di Azure DevOps e passare al progetto.

  2. Passare a Pipeline e quindi selezionare Nuova pipeline o Crea pipeline se si crea la prima pipeline.

  3. Eseguire i passaggi della procedura guidata selezionando prima di tutto GitHub come posizione del codice sorgente.

  4. Si potrebbe essere reindirizzati a GitHub per l'accesso. In questo caso, immettere le credenziali di GitHub.

  5. Quando si vede l’elenco dei repository, selezionarne uno.

  6. È possibile che si venga reindirizzati a GitHub per installare l'app Azure Pipelines. In tal caso, selezionare Approva e installa.

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. È possibile usare agenti Linux, macOS o Windows per eseguire le compilazioni.

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

Costruire

Use go build to build your Go project. Aggiungere il frammento di codice seguente al 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 (./...). Aggiungere il frammento di codice seguente al 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. Seleziona Salva ed Esegui.

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.

È ora disponibile una pipeline YAML funzionante (azure-pipelines.yml) nel repository che è pronta per la personalizzazione.

Quando si è pronti per apportare modifiche alla pipeline, selezionarla nella pagina Pipeline e quindi Modificare il azure-pipelines.yml file.

Suggerimento

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.

Creare un'immagine ed eseguire il push nel registro di container

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

Go extension for Visual Studio Code