Rails deploying Gemfile.lock to development and production repositories - ruby-on-rails

I have a single app directory for development: d:/some/dir/myapp. I have a single repository called sample-repo, but it has two branches: develop and master.
My app's Gemfile uses the sample-repo repository's environment-specific branch:
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
gem 'sample-repo', git: "git#bitbucket.org:MyUserName/sample-repo.git",
branch: "#{ RAILS_ENV.to_sym == :production ? :master : :develop }"
My web host has two separate directories; one for development and the other for production. Each are updated via a git pull from their respective branches (development => develop, production => master).
This is a problem because when I run bundle update on my development machine, the Gemfile.lock contains the sample-repo gem, specifying the develop branch. I then git push the Gemfile.lock to my repo (both develop and master), and now the master repo contains a Gemfile.lock that specifies the develop branch!
i.e., the problem is that there is only one Gemfile.lock file to go around between the develop and master branches.

This answer solved my problem. I created a file called .rails-env which contains a simple string specifying the environment bundler should use. This file must be kept out of the repository. If not present, bundler defaults to development.
The second part of the problem was that I had to adjust my deployment procedure. I created a small script that runs bundle update under the development environment and waits for a key press before doing the same for production. Once the development Gemfile.lock files are generated, I push the changes to my develop repository branches. After it runs through production, I push to master.

Related

forcefully bundle update in heroku rails

Let's say I have 2 codebases/git repos. project A and gem B.
project A uses gem B.
Here is the entry in the project A Gemfile.
gem 'B', git: 'https://yashdfjwehrlhkhklbRrKwgNq:x-token-auth#bitbucket.org/pwa-abcde/B.git', branch: 'dev/B-api'
Now today I made some changes to gem B and pushed it to git.
But project A never gets this update as it is already using the old version of the gem.
My main project(A) is hosted in heroku.
Now my doubt is how can I force heroku to fetch the latest changes for the gem?
You can achieve it in below two ways:
set ref that represent commit hash in your Gemfile instead of branch name and run bundle install. Now when you push again to heroku it will fetch right commmit.
gem 'B', git: 'https://yashdfjwehrlhkhklbRrKwgNq:x-token-auth#bitbucket.org/pwa-abcde/B.git', ref: 'commmit-hash'
Create new branch for your commit for gem B changes and set new branch.
This is happening because you didn't pusdev/B-apih any change in Gemfile. Heroku run bundle install if there is any change in Gemfile and Gemfile.lock.
Instead of running bundle update within Project A at Heroku, run bundle update locally first. This will update your Gemfile.lock. Test if the Project A still runs locally as expected.
Then commit and push the new Gemfile.lock to Heroku.

Deploying an app with engine using git and local versions

I am trying to understand the workflow for deploying a rails engine. I read this answer. But I don't fully understand what is going on between Gemfile, bundler, Capistrano and rails.
I have a situation where I am working on an app locally pushing to a git repository and using Capistrano to deploy. In my apps Gemfile
#gem 'my_engine', git: "git#myrepo.com/myengine.git"
gem 'my_engine', path: '/local/path/to/MyEngine'
When I am developing I often comment out the git repository and use my local( I know some may take issue with this but another time another question) what I want to know is:
when is the engine included or mounted?
If I am working with the local engine and decide to deploy the app is the local engine included at this point?
Would the local current branch/state be what is used?
If I decide to switch to the repo for production(and or dev) at what point is that included?
Does Capistrano run bundle install during the deployment?
Would Capistrano be able to use my local copy or would it need me to use the git repo?
Engine and Host application are in same repository then specifying
gem 'my_engine', path: '/local/path/to/MyEngine'
will work.
If you had different repository for engine and application then need specify gem as
gem 'my_engine', git: 'git#github.com:my_engine.git',
branch: 'master', revision: 'cb1a8d2495168d411676f58bdfc9015fe728948c'
branch and revision are optional but its recommended if you want to point it to specific commit or branch.
Make sure the deploy(user on server used to deploy application) user has access to engine repository.

How to update forked github repo on heroku

I have a forked Github repo which is used by my app deployed on heroku. I included it in my Gemfile like this :
gem 'service-client', :git => 'https://github.com/blabla/client-stuff'
Now I changed some parts of the forked repo, which requires no changes in my heroku app.
So how can I make my heroku app, rebundle or do the bundle install or whatever to pick up the latest changes from https://github.com/blabla/client-stuff master branch?
You should update gem localy and write changes to Gemfile.lock by bundle update service-client then commit changes and push to origin. Heroku update you gemset when look in to the changes in Gemfile.lock.
Unfortunately it does require changes in your heroku app.
It requires the newest gem version. You will have to push up to heroku again so that it can re bundle your Gemfile.
there is not way to run a bundle update on heroku itself because this could cause git fast-forward issues along with other unexpected consequences where a newer gem does not work appropriately.
Heroku is a production environment and all changes should be tested locally before deploying
See this SO Question

Issue with capistrano deployment

I am currently having a git project consisting of multiple projects. My project structure is similar to this
=> application-1(rails)
=> application-2
I am using capistrano for deployment. Since the home folder does not have Gemfile.lock, capistrano is throwing an error.
The --deployment flag requires a Gemfile.lock. Please make sure you have checked your Gemfile.lock into version control before deploying.
Is there a way to deploy just a specific folder in the git repository?
Found the answer here.
set :bundle_gemfile, "app/Gemfile"

Why do I need to "bundle install" before pushing a rails app to heroku?

I suppose the bundling is going to happen on the heroku servers anyway. What is the purpose of doing it on the local machine?
bundle install
This ensures that all gems specified in Gemfile, together with their dependencies, are available for your application. Running bundle install also generates a Gemfile.lock file, which should be added to your git repository. Gemfile.lock ensures that your deployed versions of gems on Heroku match the version installed locally on your development machine.
If the platforms section of your Gemfile contains Windows entries,
such as mswin or mingw, then the Gemfile.lock file will be ignored.
Heroku also uses that file to resolve and install your application dependencies automatically. All you need to do is to push it.
Refer this link : Click Here
This will update your Gemfile.lock, that heroku uses to install all your gems on your virtual server. The Gemfile.lock contains all information about your gems and their respective versions.
It has two purposes :
It ensures you that, on your machine, you have all the dependencies for your application satisfied;
It updates the Gemfile.lock file. While the Gemfile has the list of your app's gems, the Gemfile.lock has a more.. "detailed" version of it, with the gem's own dependencies, their version constraints... It basically is a snapshot of your project dependencies. This way, your app in production will run with the exact same versions of third-party code as do your code in local.
This ensures that all gems specified in Gemfile, together with their dependencies, are available for your application. Running bundle install also generates a Gemfile.lock file, which should be added to your git repository. Gemfile.lock ensures that your deployed versions of gems on Heroku match the version installed locally on your development machine.
Source: https://devcenter.heroku.com/articles/bundler

Resources