0

What is the purpose of cwd attribute in script resource? For some reason the running the first block works but the second one doesn't work as intended (i.e. cloning the git repo under /var/apps)

script 'clone_repo' do
  interpreter "bash"
  code <<-EOH
    cd /var/apps
    git clone https://gitlab.com/dsura/test-go-web-app.git
    EOH
  not_if { ::File.directory?("/var/apps/test-go-web-app") }
end

and

script 'clone_repo' do
  interpreter "bash"
  cwd ::File.dirname('/var/apps')
  code <<-EOH
    git clone https://gitlab.com/dsura/test-go-web-app.git
    EOH
  not_if { ::File.directory?("/var/apps/test-go-web-app") }
end
denniss
  • 17,229
  • 26
  • 92
  • 141

1 Answers1

1

I don't think File.directory is a method that exists, but if you mean cwd '/var/apps' then there isn't a difference. It sets the working directory before running the subprocess (aka. the script). This matters more for the execute resource where you might not want to use cd like that, but script inherits from execute so it just comes along for the ride.

You could write that whole thing more concisely as:

execute 'git clone https://gitlab.com/dsura/test-go-web-app.git /var/apps/test-go-web-app' do
  creates '/var/apps/test-go-web-app'
end

Or use a git resource for even less:

git '/var/apps/test-go-web-app' do
  action :checkout
  repository 'https://gitlab.com/dsura/test-go-web-app.git'
end

It you leave off the action :checkout, the default :sync action will update to the master branch each time Chef runs which might be better too.

Tensibai
  • 15,557
  • 1
  • 37
  • 57
coderanger
  • 52,400
  • 4
  • 52
  • 75