简体   繁体   English

注入Golang环境变量到Azure Pipeline

[英]Injection of Golang environment variables into Azure Pipeline

I am currently migrating some build components to Azure Pipelines and am attempting to set some environment variables for all Golang related processes.我目前正在将一些构建组件迁移到Azure 管道,并试图为所有 Golang 相关进程设置一些环境变量。 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.使用提供的Golang集成时,很容易为Go相关进程添加 arguments,但似乎不可能为所有(或每个人) Go进程设置环境变量。 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. GoTool或默认的Go任务似乎都不支持它,并且似乎也不支持执行带有 shell 执行的脚本任务。

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.我还尝试将环境变量添加到定义所需标志的整个管道过程中,但这些似乎被 Azure 管道本身提供的Go任务忽略了。

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)?是否有办法将这些标志添加到每个(或单个)go 进程,例如我如何在以下代码块中执行此操作(其中标志输入行由我组成)?

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

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.根据我能够找到的信息和许多小时的调试,我最终使用了一种变通方法,即在CmdLine@2任务中运行golang命令。 Due to how GoTool@0 sets up the pipeline and environment, this is possible.由于GoTool@0设置管道和环境的方式,这是可能的。

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)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM