gpt4 book ai didi

powershell - 如何在 Azure DevOps 上的 Windows 管道中为 Go 生成 syso 文件

转载 作者:行者123 更新时间:2023-12-01 21:13:19 27 4
gpt4 key购买 nike

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

在我的 win10 笔记本上一切正常,但是当我在 ubuntu-latest 或 windows-latest 中使用相同的 syso 文件(使用 git lfs checkin )时,我收到以下消息:

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'

最佳答案

当我在写我想出的问题时,我使用 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 支持 放置此文本文件而不是实际文件。

所以很明显,该文件无法执行(错误的魔数(Magic Number))或引用的 bin 文件格式错误(不是目标文件)。

关于powershell - 如何在 Azure DevOps 上的 Windows 管道中为 Go 生成 syso 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62230694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com