【问题标题】:Automatically signing ALL compiled output files自动签署所有编译的输出文件
【发布时间】:2020-06-17 17:12:43
【问题描述】:

我在 gitlab 中有一个 .net-core 3.1 项目 (Microsoft.NET.Sdk.Web),并且正在为其设置 CI/CD 管道。作为最后一步,我希望所有 .dll 和 .exe 输出文件都由我们的密钥签名。我可以通过手动将一些“signtool”命令作为 gitlab-ci.yml 文件的一部分来执行此操作,但这需要进入每个项目并手动调整文件以反映该项目的具体情况。

另一方面,我已经能够在 .csproj 文件中添加一个通用目标,该文件调用 signtool 作为 $(TargetDir)$(TargetFileName) 上的执行/命令。这在一定程度上有效 - 但它并没有签署所有内容。在这种情况下,它签署(例如)cthulhu.dll,但不签署 cthulhu.Views.dll(两者都显示在 'dotnet msbuild' 的 CLI 输出中),还有一个 cthulhu.exe 已生成但未生成显示在 msbuild 输出中,也未签名。

csproj 文件将此作为目标(尝试在重建后和发布后运行):

  <Target Name="SignAssembly" AfterTargets="Rebuild">
<Message Text="Signing assembly '$(TargetDir)$(TargetFileName)'" Importance="high" />
<Exec Command="signtool sign /f &quot;mykey.pfx&quot; /p &quot;snip&quot; /tr http://timestamp.digicert.com  &quot;$(TargetDir)$(TargetFileName)&quot;" />

并且 gitlab-ci.yml 文件看起来像这样(我知道我正在使用 sdk docker 容器,但它似乎让我无论如何都支持 .net-core 构建,而有一个错误阻止了特定的 .net-core 容器工作):

image: mcr.microsoft.com/dotnet/framework/sdk:4.8

stages:
  - build
  - release

build:
  stage: build
  script:
    - 'dotnet build -r win-x64'

release:
    stage: release
    script:
      - 'dotnet add package signtool --version 10.0.17763.132'
      - 'dotnet msbuild cthulhu.csproj /t:"Restore;Rebuild;SignAssembly;Publish" /p:SelfContained=True /p:PublishProtocol=FileSystem /p:Configuration=Release /p:Platform=x64 /p:TargetFrameworks=netcoreapp3.1 /p:PublishDir=bin\Release\netcoreapp3.1\publish\win-x64 /p:RuntimeIdentifier=win-x64 /p:PublishReadyToRun=False /p:PublishTrimmed=True'
    artifacts:
      name: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME" 
      paths:
        - '.\bin\Release\netcoreapp3.1\publish\win-x64'

【问题讨论】:

    标签: c# .net-core msbuild gitlab-ci signtool


    【解决方案1】:

    为什么不使用标准工具 - Directory.Build.props

    但是,现在您可以一步为每个项目添加新属性 通过在名为 Directory.Build.props 的单个文件中定义它 包含您的源的根文件夹。当MSBuild 运行时, Microsoft.Common.props 在您的目录结构中搜索 Directory.Build.props 文件(和 Microsoft.Common.targets 查找 Directory.Build.targets)。如果它找到一个,它会导入该属性。 Directory.Build.props 是一个用户定义的文件,它提供 对目录下项目的自定义

    表示应该对程序集进行签名:

    <SignAssembly>true</SignAssembly>
    

    使用以下属性指定密钥:

    <AssemblyOriginatorKeyFile>key.pfx</AssemblyOriginatorKeyFile>
    

    也就是说,结果,这些属性将被导入到项目中,您将获得所需的结果。

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 2011-04-17
      • 2011-01-18
      • 1970-01-01
      • 2011-06-10
      • 2013-05-10
      • 2017-12-02
      • 2013-01-04
      • 2019-07-17
      相关资源
      最近更新 更多