当Vagrant配置时,如何在项目目录中自动运行Bundler?

时间:2017-01-02 15:09:40

标签: ruby-on-rails ruby vagrant bundler vagrant-provision

我希望Bundler在Vagrant配置新VM时安装Rails项目依赖项。这就是我目前所拥有的,但我打印了这一行==> default: Could not locate Gemfile or .bundle/ directory。项目目录包含Gemfile。

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.require_version ">= 1.9.1"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "bento/ubuntu-16.04"

  # Configurate the virtual machine to use 2GB of RAM
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.name = "Nuggets_API_Env"
  end

  # Forward the Rails server default port to the host
  config.vm.network :forwarded_port, guest: 3000, host: 3000

  # Use Chef Solo to provision our virtual machine
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["cookbooks"]

    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
    chef.add_recipe "nodejs"
    chef.add_recipe "openssl"
    chef.add_recipe "ruby_build"
    chef.add_recipe "ruby_rbenv::user"
    chef.add_recipe "vim"
    chef.add_recipe "postgresql::config_initdb"
    chef.add_recipe "postgresql::server"
    chef.add_recipe "postgresql::client"

    chef.json = {
      postgresql: {
        apt_pgdg_postgresql: true,
        version: "9.5",
        password: {
          postgres: "test1234",
        }
      },
      rbenv: {
        user_installs: [{
          user: 'vagrant',
          rubies: ["2.4.0"],
          global: "2.4.0",
          gems: {
            "2.4.0" => [
              { name: "bundler" }
            ]
          }
        }]
      },
    }
  end

  config.vm.provision "shell" do |s|
    s.path = "VM_Local_Setup.sh"
    s.upload_path = "/vagrant/VM_Local_Setup.sh"
    s.privileged = false
  end

end

Cheffile

site "https://supermarket.getchef.com/api/v1"

cookbook 'apt'
cookbook 'build-essential'
cookbook 'nodejs'
cookbook 'openssl'
cookbook 'postgresql'
cookbook 'ruby_build'
cookbook 'ruby_rbenv'
cookbook 'vim'

VM_Local_Setup.sh

#!/bin/bash

sudo apt-get update
sudo apt-get upgrade -y
bundler install
rbenv rehash

1 个答案:

答案 0 :(得分:0)

在项目目录(/ vagrant)中完成配置,以及我的Gemfile所在的位置。

添加cd /vagrant解决了这个问题