1

I am trying to get setup Continuous integration with Git, Chef on aws-opworks.

To checkout a particular folder in git say node to "node_path". I can use destination: tag for the destination directory but I cannot find out how to specify the source directory in git.

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

Here the Destination folder node is Symbolic link. I can git check out to a folder, identify the modified code pieces and then copy the new files over. But I want this to be done automatically by action :sync - how can it be done?

References I have used are:

chicks
  • 2,393
  • 3
  • 24
  • 40
user 923227
  • 2,528
  • 4
  • 27
  • 46
  • Are you saying you want to check out a directory other than the root of the project? Can you show us how would you do this with git itself, without breaking the `.git` directory? – Martin Jun 01 '16 at 21:52
  • I have looked up here on Stack overflow and done it. [Possibly the link was] (http://stackoverflow.com/questions/10124223/pulling-just-one-directory-out-of-a-git-repo) – user 923227 Jun 01 '16 at 22:04
  • 1
    The Chef git resource won't do that. (It doesn't even support fetch, let alone subdirectory checkout.) You'll need to execute those git commands using an execute or bash_block resource. – Karen B Jun 02 '16 at 05:35
  • I am getting a syntax error at destination : node_path .I think it expects a string than a variable.Can anyone pls help? – shikha singh Dec 10 '19 at 04:30

2 Answers2

1

You would use a git resource and a link resource:

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
coderanger
  • 52,400
  • 4
  • 52
  • 75
0

If you are trying to deploy code from git, then You should look at the deploy resource, its syntax is like this:

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

Here is how I use it to deploy code from my repo

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

Here, we are using the deploy cookbook provided by Opsworks. so prepare_git_checkouts is coming from Opsworks.

Now,

  1. Set this recipe to execute on the deploy lifecyle event.
  2. every time things change in your repo. you should trigger a deploy on opsworks & it should be okay.

Hope it helps

CuriousMind
  • 33,537
  • 28
  • 98
  • 137