小编典典

如何在Azure DevOps的Windows Pipline中为Go生成syso文件

go

我想使用syso文件go build为可执行文件设置图标。

一切在我的win10笔记本电脑上都可以正常运行,但是当我在ubuntu-latest或Windows-latest上使用相同的syso文件(通过git
lfs签入)时,我收到此消息:

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\1.14.2\x64\bin\go.exe' failed with exit code 2

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

对于我的问题,如何在Azure DevOps上的Windows管道中为Go生成syso文件并将其用于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'

阅读 425

收藏
2020-07-02

共1个答案

小编典典

当我写出我想出的问题时,我将其git lfs用于这些文件。添加此步骤后,我可以使用资源文件编译可执行文件。

steps:

- checkout: self
  lfs: true

但是在以GOOS窗口为目标的构建之后,我必须删除这些文件,因为否则会出现错误:

/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文件格式错误(不是目标文件)。

2020-07-02