首先,总结一下失败的努力:
powershell_script 'git-sync' do
cwd "#{ENV['HOME']}']}"
code <<-EOH
git pull
EOH
end
不起作用,因为资源运行带有 -NoProfile 标记的 powershell 并且未加载用户的设置
ruby_block 'git-sync' do
block do
script <<-EOH
git pull
EOH
end
result = powershell_out(script)
if (!result.stdout.to_s.empty?)
Chef::Log.info("GIT command output::\n#{result.stdout.to_s}")
end
end
同样的问题,没用。
是的
git "#{node.run_state['git_directory']}" do
repository "#{node.run_state['git_repo']}"
revision "#{node.run_state['git_branch']}"
action :sync
user "#{ENV['USER']}"
end
上面的那个不起作用,因为必须指定user 属性并且由于抱怨某个 nil 目录的荒谬错误而失败。经过检查,我意识到该资源可能仅适用于 linux,底层 ruby 文件使用 Etc.getpwnam(..)
现在,解决方案:
配方git_sync.rb:
ruby_block 'git-sync' do
block do
psFile = "#{Chef::Config[:file_cache_path]}/cookbooks/#{cookbook_name}/files/git_align.ps1"
node.run_state['git_directories'].each { |location|
scriptOutput = `powershell.exe -NoLogo -NonInteractive -File #{psFile} #{location} #{node.run_state['git_branch']}`
Chef::Log.info("Git Sync Output::\n#{scriptOutput}")
}
end
end
powershell 脚本git_align.ps1:
Param(
[string]$location,
[string]$branch_name
)
Try{
$output = ""
$loc = Resolve-Path -path $location
if ($loc -eq $null) {throw "$location not found"}
Set-Location $loc
$output+= "At location $loc`n"
$curr_branch = git rev-parse --abbrev-ref HEAD
if ($curr_branch -ne $branch_name){
$output+= git checkout $branch_name
$output+= "`n"
}
if ($? -eq $false) {throw "GIT checkout failed"}
$output+= git reset --hard origin/$branch_name
$output+= "`n"
if ($? -eq $false) {throw "GIT reset failed"}
$output+= git pull
$output+= "`n"
if ($? -eq $false) {throw "GIT pull failed"}
Write-Output $output
}
Catch{
$exc = $_.Exception | format-list -force
Write-Output $exc
Throw $exc
}
希望对您有所帮助!大厨有时就像一片海洋,没有人应该独自游泳!