登录
首页 >  Golang >  Go问答

在 Azure DevOps 的 Windows pipeline 中生成 Go 的 syso 文件的步骤

来源:stackoverflow

时间:2024-03-03 17:48:23 282浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《在 Azure DevOps 的 Windows pipeline 中生成 Go 的 syso 文件的步骤》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

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

在我的 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

对于我的问题,如何在 azure devops 上的 windows pipline 中生成 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'

解决方案


当我写这个问题时,我发现,我对这些文件使用 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 文件格式错误(不是目标文件)。

今天关于《在 Azure DevOps 的 Windows pipeline 中生成 Go 的 syso 文件的步骤》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>