【问题标题】:How to checkout a folder from git using a chef recipe如何使用厨师食谱从 git 签出文件夹
【发布时间】:2016-10-01 09:30:48
【问题描述】:

我正在尝试在 aws-opworks 上设置与 Git、Chef 的持续集成。

要签出 git 中的特定文件夹,请说 node"node_path"。我可以将destination: 标签用于destination directory,但我不知道如何在git 中指定source 目录

node_path = "/my/home/MyPrj/node"
git node_path do
    repository "https://something.com/MyCo/MyPrj.git"
    reference "prod"
    action :sync
    destination : node_path
end

这里的目标文件夹node 是符号链接。我可以 git check out 到一个文件夹,识别修改后的代码片段,然后复制新文件。但我希望这由action :sync 自动完成 - 怎么做?

我使用的参考文献是:

【问题讨论】:

  • 您是说要检出项目根目录以外的目录吗?你能告诉我们如何在不破坏.git 目录的情况下使用 git 本身执行此操作吗?
  • 我在这里查看了堆栈溢出并完成了它。 [可能链接是] (stackoverflow.com/questions/10124223/…)
  • Chef git 资源不会这样做。 (它甚至不支持 fetch,更不用说子目录检出。)您需要使用 execute 或 bash_block 资源来执行这些 git 命令。
  • 我在目的地遇到语法错误:node_path。我认为它需要一个字符串而不是变量。有人可以帮忙吗?

标签: git chef-infra directory aws-opsworks


【解决方案1】:

您将使用git 资源和link 资源:

git '/srv/MyPrj' do
  repository 'https://something.com/MyCo/MyPrj.git'
  branch 'prod'
end

link '/my/home/MyPrj/node' do
  to '/srv/MyPrj/node'
end

【讨论】:

  • 这应该可以解决问题!但是节点目录中的 npm 安装会发生什么 - 它们会被删除吗?
  • 如果/my/home/MyPrj/node 已经存在,则收敛将失败,就像ln -s 会失败一样。
【解决方案2】:

如果你尝试从 git 部署代码,那么你应该看看deploy resource,它的语法是这样的:

deploy 'private_repo' do
  repo 'git@github.com:acctname/private-repo.git'
  user 'ubuntu'
  deploy_to '/tmp/private_code'
end

这是我如何使用它从我的仓库部署代码

include_recipe 'deploy'
application = search(:aws_opsworks_app).first
deploy = node[:deploy][application['shortname']]
directory deploy[:deploy_to] do
  group deploy[:group]
  owner deploy[:user]
  mode "0775"
  action :create
  recursive true
end
prepare_git_checkouts(
  user: deploy[:user],
  group: deploy[:group],
  home: deploy[:home],
  ssh_key: "#{application['app_source']['ssh_key']}"
)
ruby_block "change HOME to #{deploy[:home]} for source checkout" do
  block do
    ENV['HOME'] = deploy[:home]
  end
end
deploy application['shortname'] do
  before_migrate do
    link_tempfiles_to_current_release
    # Do NPM Install here.
  end
  branch                      application['app_source']['revision']
  create_dirs_before_symlink  deploy[:create_dirs_before_symlink]
  deploy_to                   deploy[:deploy_to] # defaults to 'name' if not specified
  environment                 OpsWorks::Escape.escape_double_quotes(application[:environment])
  group                       deploy[:group]
  keep_releases               deploy[:keep_releases]
  migrate                     false
  provider                    Chef::Provider::Deploy.const_get(deploy[:chef_provider])
  purge_before_symlink(deploy[:purge_before_symlink]) unless deploy[:purge_before_symlink].nil?
  repository                  application['app_source']['url']
  revision                    application['app_source']['revision']
  scm_provider                Chef::Provider::Git
  shallow_clone               deploy[:shallow_clone]
  symlinks(deploy[:symlinks]) unless deploy[:symlinks].nil?
  user                        deploy[:user]
  action                      deploy[:action] # defaults to :create if not specified
  rollback_on_error           true
end

ruby_block "change HOME back to /root after source checkout" do
  block do
    ENV['HOME'] = "/root"
  end
end

这里, 我们正在使用 Opsworks 提供的部署说明书。所以prepare_git_checkouts 来自 Opsworks。

现在,

  1. 将此配方设置为在部署生命周期事件上执行。
  2. 每次在您的存储库中发生变化。您应该在 opsworks 上触发部署,应该没问题。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多