Home > Software engineering >  Injection of Golang environment variables into Azure Pipeline
Injection of Golang environment variables into Azure Pipeline

Time:01-24

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'

CodePudding user response:

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