powershell - 如何设置 Jenkins 在 Powershell 脚本中使用 Knife

标签 powershell jenkins chef-infra knife

我在 Windows Server 2012 R2 上使用 Jenkins。我想运行一个构建的作业,在强制升级时,它应该告诉远程 Chef 服务器 Recipe 已更改或更新,然后在远程节点上启动 Chef 客户端。

我已经安装了 Chef Identity 插件、Promoted-builds 插件、MSbuild 插件、TFS-4.0.0 插件和 Deploy 插件

到目前为止,构建工作和促销工作。 我已经设置了两个 Powershell 脚本应该运行的强制升级。 第一个工作正常,它只进行基本的数据管理和文件命名。

但是第二个说这是失败的,但是促销仍然通过并且没有任何证据表明它有效。

我已经明确使用了 Knife 的路径,因为当我不使用它时,Jenkins 会给出一个错误:

WARNING: No knife configuration file found
ERROR: Could not find cookbook MyFirstCookingLesson in your cookbook path, skipping it
ERROR: ArgumentError: Cannot sign the request without a client name, check that :node_name is assigned

如果我添加 -V 标志并通过右键单击它并在 Jenkins 工作区中使用 Powershell 运行它来运行脚本,我得到以下信息:

enter image description here

ps1 文件包含(我们称之为“Chef-client.ps1”):

C:\opscode\chefdk\bin\knife cookbook upload MyFirstCookingLesson
C:\opscode\chefdk\bin\knife job start 'chef-client' WIN-COMPNAME

使用执行 Windows 批处理命令调用 Chef-client.ps1:

cd C:\chef-source\cookbooks
PowerShell.exe -File  C:\chef-source\cookbooks\Chef-client.ps1

我还在 Jenkins 凭证中设置了 Chef 身份,如下所示:

Identity name: geoff
user.pem key : <a whole bunch of secret stuffs and gibberish>

knife.rb : 

log_level :info
log_location STDOUT
node_name 'geoff'
client_key 'C:/Users/user123/.chef/geoff.pem'
validation_client_name  'geoff.pem'
validation_key 'C:/Users/user123/.chef/secretName-validator.pem'
chef_server_url 'https://secretLocation.268lab.local/organizations/secretName/'
syntax_check_cache_path 'C:/Users/user123/.chef/syntax_check_cache'
cookbook_path [ 'c:/chef-source/cookbooks/' ]
http_proxy 'http://22.151.32.85:3128'
https_proxy 'http://22.151.32.85:3128'
no_proxy 'secretLocation.268lab.local'
knife[:editor] = '"C:\Program Files (x86)\Notepad++\notepad++.exe"';

所以我还发现 Jenkins 似乎在它自己的用户配置文件中运行,我觉得它可能会把所有东西都扔掉。

我已经阅读了如此多的设置博客,但没有任何内容真正告诉我如何明确地进行设置。这些博客要么假设它已经完成,要么使用不同的工具。

我还在 Jenkins 工作区和“有希望的”用户配置文件空间中放置了 .chef 文件夹的副本,希望它能起作用。

谁能帮我指导一下?

最佳答案

评论太长了:

So I have also found out that Jenkins seems to run in its own Userprofile which I have a feeling might be throwing everything off.

如果你的 jenkins 用户无法读取 user123 home,它就不可能读取 knife 配置,因为它无法读取 geoff.pem 文件,因此无法正确验证。

解决此问题的一个解决方案是将配置、 key 等放在特定目录中,并使用 knife 的 -c 选项为其提供 conf 文件的路径。另一种选择是使用 KNIFE_HOME 环境变量,但它在 Jenkins 中不太容易且容易出错。

关于powershell - 如何设置 Jenkins 在 Powershell 脚本中使用 Knife,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34199657/

相关文章:

powershell - 在 PowerShell 数组内连接字符串

linux - 在 shell 脚本中使用 7z 时不解压所有文件

powershell - Office 364审核日志:Search-UnifiedAuditLog不被识别为cmdlet的名称

Jenkins Docker 容器 - 403 请求中未包含有效的面包屑

jenkins - 如何在类里面使用 Jenkins 的内置方法?

amazon-web-services - Chef 12 + AWS Opsworks + 从 s3 部署应用程序

powershell - 如何使用 PowerShell 访问第三方 OLE DLL 的方法和属性?

java - 如何将 Jenkins 工作区编译为 .jar

ruby - 无法在 Windows 7 上安装 librarian-chef Recipe

chef-infra - Chef 自定义资源是否应该使用属性作为默认属性值?