从 C# 程序集中提取版本作为 Jenkins 管道的一部分

缺口

我想从 C# 程序集中提取构建版本,作为在 Windows 服务器上运行的 Jenkins 管道的一部分。程序集文件如下所示:

// [...snip...]

[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BulkUpdater")]
[assembly: AssemblyCopyright("Copyright ©  2016")]

// [...snip...]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.4.1.9")]
[assembly: AssemblyFileVersion("3.4.1.9")]

我对 Jenkins 相当陌生,但对 Groovy 很熟悉,所以我转向了明显的 Groovy 工具来实现这一点:

String captureVersionFromAssembly(String assemblyFilename) {
  String version
  File file = new File(assemblyFilename)
  file.eachLine { line ->
    // avoid commented AssemblyVersion
    if (line.contains("AssemblyVersion") && !line.startsWith('//')) {
      version = line.split('"')[1]
    }
  }
  version
}

3.4.1.9在 Jenkins 管道之外正确返回

我知道 Jenkins 管道插件要求您授予管道脚本使用File该类的权限我已经在注册它这样做in-process script approval的部分Manage Jenkins

在 Jenkins 管道中,File该类似乎无法找到文件(即使bat 'dir'显示它们在那里。)

dir('omega-elms')  {
    bat 'dir'
    bat 'dir ELMS_Desktop_Application'
    echo "Working directory: ${pwd()}"
    fileExists('ELMS_Desktop_Application')
    fileExists('ELMS_Desktop_Application\\CARS')
    fileExists('ELMS_Desktop_Application\\CARS\\BulkUpdater')
    fileExists('ELMS_Desktop_Application\\CARS\\BulkUpdater\\Properties')
}

def fileExists(String filename) {
   echo "'$filename' exists? ${new File(filename).exists()}"
}

给出以下日志:

[windows] E:\Jenkins\Production\workspace\test_version_extract_buildonly\omega-elms>dir
[windows]  Volume in drive E is New Volume
[windows]  Volume Serial Number is 6695-F35A
[windows]
[windows]  Directory of E:\Jenkins\Production\workspace\test_version_extract_buildonly\omega-elms
[windows]
[windows] 29/08/2017  11:38 AM    <DIR>          .
[windows] 29/08/2017  11:38 AM    <DIR>          ..
[windows] 29/08/2017  11:38 AM    <DIR>          ELMS_Desktop_Application
[windows]                0 File(s)              0 bytes
[windows]                3 Dir(s)  105,175,678,976 bytes free
[Pipeline] [windows] bat
[windows] [omega-elms] Running batch script
[windows]
[windows] E:\Jenkins\Production\workspace\test_version_extract_buildonly\omega-elms>dir ELMS_Desktop_Application
[windows]  Volume in drive E is New Volume
[windows]  Volume Serial Number is 6695-F35A
[windows]
[windows]  Directory of E:\Jenkins\Production\workspace\test_version_extract_buildonly\omega-elms\ELMS_Desktop_Application
[windows]
[windows] 29/08/2017  11:38 AM    <DIR>          .
[windows] 29/08/2017  11:38 AM    <DIR>          ..
[windows] 29/08/2017  11:38 AM    <DIR>          AppData
[windows] 29/08/2017  11:38 AM    <DIR>          CARS
[windows] 29/08/2017  11:38 AM             1,322 README.md
[windows] 29/08/2017  11:38 AM    <DIR>          Tests
[windows]                1 File(s)          1,322 bytes
[windows]                5 Dir(s)  105,175,674,880 bytes free
[Pipeline] [windows] pwd
[Pipeline] [windows] echo
[windows] Working directory: E:\Jenkins\Production\workspace\test_version_extract_buildonly\omega-elms
[Pipeline] [windows] pwd
[Pipeline] [windows] echo
[windows] 'ELMS_Desktop_Application' exists? false
[Pipeline] [windows] echo
[windows] 'ELMS_Desktop_Application\CARS' exists? false
[Pipeline] [windows] echo
[windows] 'ELMS_Desktop_Application\CARS\BulkUpdater' exists? false
[Pipeline] [windows] echo
[windows] 'ELMS_Desktop_Application\CARS\BulkUpdater\Properties' exists? false
[Pipeline] [windows] echo
[windows] 'ELMS_Desktop_Application\CARS\BulkUpdater\Properties\AssemblyInfo.cs' exists? false

我还预先添加了工作目录以提供绝对文件路径:

String assembly = "${pwd()}\\ELMS_Desktop_Application\\CARS\\BulkUpdater\\Properties\\AssemblyInfo.cs"

但这也不能解决问题。

有什么建议么?

缺口

以下是丑陋但有效的:

appversion.batassemblyDir

@echo off
REM extract version from assembly ignoring commented out version
FOR /F delims^=^"^ tokens^=1^,2^* %%A IN ('findstr /L /B /C:"[assembly: AssemblyVersion" "AssemblyInfo.cs"') DO (
   set "version=%%B"
)
echo %version%

在詹金斯管道中:

String versionNumber
dir(assemblyDir) {
  bat './appversion.bat >stdout'
  versionNumber = readFile('stdout').trim()
}
// ...use versionNumber here

尚不清楚为什么这些File方法在 Jenkins 管道中找不到文件。更好地理解会很有趣。例如,这是故意的安全功能吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章