Circle CI with Rails
In this tutorial, learn how to integrate Dotenv Vault with Circle CI. Setup CI file to work with rails application. example
You can find a complete example repo here.
Install dotenv-vault
Create your local .env
file.
HELLO="World"
Add dotenv-vault-rails gem to Gemfile
// Add 'dotenv-vault-rails' to Gemfile
gem 'dotenv-vault-rails'
Require dotenv-vault as early as possible in your Rails application. For a Rails application require dotenv-vault/load in application.rb
// config/application.rb
require 'dotenv-vault/load'
Test for Circle CI
describe "hello spec" do
it "returns World" do
print(ENV["HELLO"])
expect(ENV["HELLO"]).to eql("World")
end
end
Next, we need to build our encrypted .env.vault file.
Build .env.vault
First set a CI value. Run dotenv-vault open to edit CI values.
$ npx dotenv-vault open ci
Then build your localized encrypted .env.vault file.
$ npx dotenv-vault build
Great! Commit your .env.vault file to code. It is safe to do so. It is a localized encrypted vault of your environment variables.
Set DOTENV_KEY
Lastly, set the DOTENV_KEY on Circle CI.
Run npx dotenv-vault keys ci to get your CI decryption key.
$ npx dotenv-vault keys ci
remote: Listing .env.vault decryption keys... done
dotenv://:[email protected]/vault/.env.vault?environment=ci
Then in Circle CI click projects settings
and Add Environment Variable
That’s it!
Commit your changes to code and push.
When the build runs, it will recognize the DOTENV_KEY, decrypt the .env.vault file, and load the CI environment variables to Circle CI. If a DOTENV_KEY is not set (like during development on your local machine) it will fall back to regular dotenv.
You will know it worked when you see the message ‘Loading env from encrypted .env.vault’.