将Golang环境变量注入到Azure Pipeline中

huangapple go评论86阅读模式
英文:

Injection of Golang environment variables into Azure Pipeline

问题

我目前正在将一些构建组件迁移到Azure Pipelines,并尝试为所有与Golang相关的进程设置一些环境变量。我希望在流水线中执行以下命令:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build [...]

在使用提供的Golang集成时,很容易为Go相关的进程添加参数,但是似乎无法为所有(或每个单独的)Go进程设置环境变量。无论是*GoTool还是默认的Go任务似乎都不支持此功能,并且在其中执行shell命令的script*任务似乎也不受支持。

我还尝试将一个定义所需标志的环境变量添加到整个流水线进程中,但是这些变量似乎被Azure Pipelines自身提供的Go任务忽略了。

是否有办法为每个(或单个)go进程添加这些标志,就像我在以下代码块中所做的那样(其中flags输入行是我自己编写的)?

- task: Go@0
  inputs:
    flags: 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64'
    command: 'build'
    arguments: '[...]'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
  displayName: 'Build the application'
英文:

I am currently migrating some build components to Azure Pipelines and am attempting to set some environment variables for all Golang related processes. I wish to execute the following command within the pipeline:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build [...]

When utilizing the provided Golang integrations, it is easy to add arguments for Go related processes, but setting an environment variable for all (or for every individual) Go process does not seem possible. Neither GoTool or the default Go task seem to support it, and performing a script task with a shell execution in it do not seem to be supported either.

I have also tried adding an environment variable to the entire pipelines process that defines the desired flags, but these appear to be ignored by the Go task provided by Azure Pipelines itself.

Would there be a way to do add these flags to each (or a single) go process, such as how I do it in the following code block (in which the flags input line was made-up by me)?

- task: Go@0
  inputs:
    flags: 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64'
    command: 'build'
    arguments: '[...]'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
  displayName: 'Build the application'

答案1

得分: 0

根据我找到的信息和多个小时的调试,我最终采用了一种解决方法,即在CmdLine@2任务中运行golang命令。由于GoTool@0设置了管道和环境,这是可能的。

因此,下面的代码片段适用于我的目的。

steps: 
- task: GoTool@0
  inputs:
    version: '1.19.0'
- task: CmdLine@2
  inputs:
    script: 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
英文:

Based on the information I was able to find and many hours of debugging, I ended up using a workaround in which I ran the golang commands in a CmdLine@2 task, instead. Due to how GoTool@0 sets up the pipeline and environment, this is possible.

Thus, the code snippet below worked for my purposes.

steps: 
- task: GoTool@0
  inputs:
    version: '1.19.0'
- task: CmdLine@2
  inputs:
    script: 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

huangapple
  • 本文由 发表于 2023年1月19日 19:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75171491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定