如何在 Azure DevOps 上的 windows 管道中为 Go 生成系统文件

How to generate a syso-file for Go in a windows pipline on Azure DevOps

我想使用 go build 的系统文件来为我的可执行文件设置图标。

在我的 win10 笔记本上一切正常,但是当我使用相同的系统文件(使用 git lfs 签入)和 ubuntu-latest 或 windows-latest 我收到此消息:

C:\Users\VSSADM~1\AppData\Local\Temp\go-build430615882\b001\_pkg_.a(rsrc.syso): not an object file

##[error]The Go task failed with an error: Error: The process 'C:\hostedtoolcache\windows\go.14.2\x64\bin\go.exe' failed with exit code 2

当我尝试重新创建 syso 文件时,我收到此消息:bad magic number 在管道中调用可执行文件 ($env:GOPATH+"\bin\rsrc.exe") 时。

关于我的问题,如何在 Azure DevOps 的 windows 管道中为 Go 生成系统文件并将其与 go build 一起使用?

pool:
  vmImage: 'windows-2019'

variables:
  GOBIN:  '$(GOPATH)/bin'
  GOPATH: '$(system.DefaultWorkingDirectory)/gopath'
  ExecutableName: tool
  GoFilePath: '$(System.DefaultWorkingDirectory)/cmd/Tool/'

steps:
- task: GoTool@0
  inputs:
    version: '1.14.2'
- task: Go@0
  inputs:
    command: 'get'
    arguments: '-d ./...'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: PowerShell@2
  displayName: Set last tag to variable
  inputs:
    targetType: 'inline'
    script: |
      $VERSION_TAG = git describe --tags (git rev-list --tags --max-count=1)
      Write-Host("##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG")
      Write-Host("##vso[build.addbuildtag]$VERSION_TAG")
      Write-Host($VERSION_TAG)

- task: PowerShell@2
  displayName: Set date to variable
  inputs:
    targetType: 'inline'
    script: |
      $DATE = Get-Date -Format "yyyy-MM-dd"
      Write-Host("##vso[task.setvariable variable=DATE]$DATE")

- task: Go@0
  displayName: 'Create release for windows x64'
  enabled: true
  env: 
    GOOS: windows
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).exe $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'


- task: Go@0
  displayName: 'Create release for windows x86'
  enabled: true
  env: 
    GOOS: windows
    GOARCH: 386
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName)_x86.exe $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: Go@0
  displayName: 'Create release for linux x64'
  enabled: true
  env: 
    GOOS: linux
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).bin $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'    


- task: Go@0
  displayName: 'Create release for linux x86'
  enabled: true
  env: 
    GOOS: linux
    GOARCH: 386
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName)_386.bin $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'    


- task: Go@0
  displayName: 'Create release for macOS x64'
  enabled: true
  env: 
    GOOS: darwin
    GOARCH: amd64
  inputs:
    command: 'build'
    arguments: '-ldflags "-s -w -X main.Version=$(VERSION_TAG) -X main.BuildTime=$(DATE)" -trimpath -o release_build/$(ExecutableName).app $(GoFilePath)'
    workingDirectory: '$(System.DefaultWorkingDirectory)'    

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)/release_build'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)/docs'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'Tool Executables'

在我写问题时,我发现我对这些文件使用 git lfs。 添加此步骤后,我可以使用资源文件编译可执行文件。

steps:

- checkout: self
  lfs: true

但是在构建目标 GOOS 之后 windows 我必须删除这些文件,否则我会收到错误消息:

/opt/hostedtoolcache/go/1.14.2/x64/pkg/tool/linux_amd64/link: running gcc failed: exit status 1


/usr/bin/ld: i386 architecture of input file `/tmp/go-link-782633042/000000.o' is incompatible with i386:x86-64 output

collect2: error: ld returned 1 exit status 

删除这些文件的步骤:

- task: PowerShell@2
  displayName: Remove syso files
  inputs:
    targetType: 'inline'
    script: |
      Remove-Item $(System.DefaultWorkingDirectory)/cmd/AnalyseTool/*.syso

说明

这是 v1 文本指针的示例:

version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 12345
(ending \n)

来源:https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md

下载源代码时不支持 lfs 放置此文本文件而不是 真实 文件。

很明显,这个文件无法执行(错误的幻数)或者引用的 bin 文件格式错误(不是目标文件)。