Inject Golang environment variables into Azure Pipeline

PHPz
Release: 2024-02-13 13:40:09
forward
1062 people have browsed it

将 Golang 环境变量注入 Azure Pipeline

php editor Xiaoxin brings you an article about injecting Golang environment variables into Azure Pipeline. In modern software development, using continuous integration and continuous delivery processes has become standard practice. Azure Pipeline is a powerful tool that helps developers automate building, testing, and deploying applications. This article will introduce how to inject Golang environment variables in Azure Pipeline to facilitate the use of Golang-related tools and libraries during the build and deployment process. By understanding these steps, you will be able to leverage Azure Pipeline more efficiently to build and deploy Golang applications.

Question content

I am currently migrating some build components to azure pipelines and trying to set some environment variables for all golang related processes. I want to execute the following command in a pipeline:

cgo_enabled=0 goos=linux goarch=amd64 go build [...]
Copy after login

When leveraging the provided golang integration, it is easy to add parameters for go related processes, but set an environment variable >go# for all (or everyone) ## The process seems impossible. It doesn't appear to be supported by either gotool or the default go tasks, and there doesn't appear to be support for executing script tasks with shell execution either.

I also tried adding an environment variable to the entire pipeline process to define the required flags, but these seem to be ignored by the

go task provided by azure pipelines itself.

Is there a way to add these flags to each (or individual) go process, like how I do it in the following block of code (which creates the

flags input line by me Finish)?

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

Workaround

Based on the information I was able to find and long hours of debugging, I ended up using a workaround, running within a

golang task cmdline@2 command. This is possible due to the way gotool@0 sets up the pipeline and environment.

So the code snippet below serves my purpose.

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)'
Copy after login

The above is the detailed content of Inject Golang environment variables into Azure Pipeline. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template