【问题标题】:How to execute GIT commands from Chef recipe on Windows when repository is AWS CodeCommit?当存储库是 AWS CodeCommit 时,如何在 Windows 上从 Chef 配方执行 GIT 命令?
【发布时间】:2017-11-10 00:23:50
【问题描述】:

我的存储库位于 AWS CodeCommit 上。我已经运行了配置 aws cli 的命令:

aws configure --profile test

我已将策略“AWSCodeCommitPowerUser”分配给我的 IAM 用户。 我已让我的 GIT 凭证管理员使用我的 AWS 个人资料:

git config --global credential.helper '!aws --profile test codecommit credential-helper $@'
git config --global credential.UseHttpPath true

我已验证在 C:\Users\[my_user]\ 中为我的用户正确创建了 .aws 目录和 .gitconfig 文件。

我已验证我能够在所需的 git 控制目录中从我的 powershell 控制台执行git pull 命令。

但是,当我在 Chef 配方中运行相同的命令时,它无法更新 git 目录。我不断收到 git timeout 错误,并注意到任务管理器中正在运行 Git Credential Manager 任务;之后我的食谱失败了。我的主厨客户的版本是:12.14.89

如何让它工作?

【问题讨论】:

  • 哪个用户正在运行厨师食谱?如果它是其他用户或通过以其他用户身份启动的服务运行,您也需要为该用户配置 aws 和 git
  • 我已经确保了这一切,通过 windows_task 资源运行我的主厨客户端,该资源从我为其配置内容的域用户开始。

标签: windows git powershell chef-recipe aws-codecommit


【解决方案1】:

首先,总结一下失败的努力:

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
}

希望对您有所帮助!大厨有时就像一片海洋,没有人应该独自游泳!

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 2017-01-23
    • 2015-10-28
    • 2020-06-03
    • 2022-09-27
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多