登录
首页 >  Golang >  Go问答

构建Go模块的正确Azure管道

来源:stackoverflow

时间:2024-03-14 10:39:26 171浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《构建Go模块的正确Azure管道》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

由于用于构建 go 代码的默认 azure-pipelines.yml 模板不支持 go 模块,因此支持它的方式并不明显。

这是默认模板,不适用于 go.modules:

# Go
# Build your Go project.
# Add steps that test, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/go

trigger:
- master

pool:
  vmImage: ubuntu-latest

variables:
  GOBIN:  '$(GOPATH)/bin' # Go binaries path
  GOROOT: '/usr/local/go1.11' # Go installation path
  GOPATH: '$(system.defaultWorkingDirectory)/gopath' # Go workspace path
  modulePath: '$(GOPATH)/src/github.com/$(build.repository.name)' # Path to the module's code

steps:
- script: |
    mkdir -p '$(GOBIN)'
    mkdir -p '$(GOPATH)/pkg'
    mkdir -p '$(modulePath)'
    shopt -s extglob
    shopt -s dotglob
    mv !(gopath) '$(modulePath)'
    echo '##vso[task.prependpath]$(GOBIN)'
    echo '##vso[task.prependpath]$(GOROOT)/bin'
  displayName: 'Set up the Go workspace'

- script: |
    go version
    go get -v -t -d ./...
    if [ -f Gopkg.toml ]; then
        curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
        dep ensure
    fi
    go build -v .
  workingDirectory: '$(modulePath)'
  displayName: 'Get dependencies, then build'

解决方案


我还想在这里分享正确构建 go 模块包的模板的答案。也许这只是为了你的灵感需要考虑的事情。我花了一些时间才到达那里。

主要的痛点是默认模板将 gopath 设置为管道工作目录,如果您通过 go mod download 将模块下载到其中,那么这是错误的。这将导致下一次管道运行时无法访问文件,从而使管道在存储库签出期间失败。

以下方法仅将 gopath 设置为 agent.homedirectory,这也使下载的模块可用于后续管道运行。

也许它对某人有帮助

# Go
# Build your Go project.
# Add steps that test, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/go

trigger: 
- main
- feature/*

pool: ubuntu-latest

variables:
  GOPATH: '$(Agent.HomeDirectory)/go' # Go workspace path
  GOBIN:  '$(GOPATH)/bin' # Go binaries path
  GOROOT: '/opt/hostedtoolcache/go/1.15.8/x64' # Go installation path
  

stages:
- stage: Build
  displayName: Build image
  
  jobs:  
  - job: BuildAndTest
    displayName: Build And Test
    pool: ubuntu-latest
    steps:
    - checkout: self
    - script: |
        export PATH="$(GOROOT)/bin:$(PATH)"
        printenv
        ls -la
        go env
        go version
        go mod download
        go build ./...
        go test ./... 
      workingDirectory: '$(Build.SourcesDirectory)'
      displayName: 'Get dependencies, then build and test'

来自 learn.microsoft 的 .yml 文件可以工作,但它要求 go.mod 和 main.go 位于根目录中。管道应支持典型的 go 项目结构,其中包含 cmd/main.gopkg/internal/ 目录

今天关于《构建Go模块的正确Azure管道》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>