Set environment variables in Counterclockwise - heroku

I am trying to set production and dev profiles in Eclipse in order to deploy my compojure/ring app on Heroku. However, the Counterclockwise plug-in doesn't load up environment variables from the profile. I have added the variables in the environment tab and have restarted the REPL but the variables are still not available. This is how I added them: environment variables.
I have also tried adding the variables to profiles.clj but to no avail:
`:profiles
{:production
{:ring
{:open-browser? false,
:stacktraces? false,
:auto-reload? false}
:env {:port 3000
:db-url "//localhost/login"
:db-user "test"
:db-pass "test"
:galleries-path "test"}}
:dev
{:dependencies [[ring-mock "0.1.5"]
[ring/ring-devel "1.2.0"]]
:env {:port 3000
:db-url "//localhost/gallery"
:db-user "test"
:db-pass "testProd"
:galleries-path "galleries"}}}`

When you use environ, it automatically coerces "DB_URL" into the more idiomatic :db-url. Looking at environ's coercion code, it doesn't seem like it should matter, but I would try uppercasing and underscoring all of the environment variables you have set in the Environment tab.

Related

How to separate development and production keys in Leiningen app on heroku?

I want have two different sets of API keys for development and production and I want to use them automatically depending on whether the app is run locally or on heroku.
For nodejs apps, I do the following:
if (process.env.NODE_ENV === 'production') {
// ./prod contains the production keys
module.exports = require('./prod');
} else {
// ./dev contains the development keys
module.exports = require('./dev');
}
How to do something similar in a Clojure Leiningen app? What would the process.env.NODE_ENV analogue be, and how to implement it?
Clojure runs on JVM, so you could use java System class. Assuming you have set APP_ENVIRONMENT variable in bash.
(if (= "production"
(or (System/getenv "APP_ENVIRONMENT")
"development"))
(start/production)
(start/development))
I would say the cleanest solution would be to use the environ library together with the lein-environ plugin.
With these you store development version of API keys in env vars in the leiningen dev profile and on production pass them as actual environment variables. E.g. you can have the following in your project.clj:
(defproject ...
...
:profiles {:dev {:env {:my-api-key "whatever-dev" } } }
...)
On the production environment you could simply set an environment variable MY_API_KEY to "whatever-prod".
In your code you will access the key like this:
(require '[environ.core :refer [env]])
(env :my-api-key) ;; would yield "whatever-dev" on dev and "whatever-prod" on prod environment
In particular, this allows you not to store the production keys under source control.
environ provides a couple more options - in particular with env files, which may also fit your needs. Do check its capabilities.
If you also need access to environment variables in your CLJS (on frontend), you can't simply access environment variables there indeed (because there's no such thing as environment anymore - the code runs on the client).
Still, if you just wish to drop the values into the resulting js during compilation, you can use environ + a macro for that. You will write a macro in a clj file that just returns the env var and then refer it in your cljs file. During compilation the appropriate value will be injected.
In a clj file:
(ns yourns.environment
(:require [environ.core :refer [env]]))
(defmacro my-api-key []
(env :my-api-key))
In cljs:
(ns yourns.core
(:require-macros [yourns.environment :refer [my-api-key]))
(my-api-key) ;; will return the appropriate value
Note, however, again that the variable value will be injected in compile time and can't be changed later.
Please see here for a complete code example.

How can I use different Cypress.env() variables for Circle testing?

I am doing some automatic testing on Circleci, with different enviromental variables: I need one port for my local testing and a different one for Circleci.
How can I make Cypress do that? I tried making cypress.env.circle, but that does not seem to work
The cypress docs explain 5 ways to set variables.
To use one port locally and one on CircleCI I would:
Add a default port to cypress.json under the env section for local use so you don't have to think about it, and anyone else contributing will have a working version.
Set an environment variable in CircleCI named cypress_VAR_NAME which will override default in cypress.json
cypress.json example
{
"env": {
"the_port": 5000
}
}
CircleCI variable would then be cypress_the_port and you would read it in your specs as parseInt(Cypress.env('the_port')) (assuming your spec needs an integer for port)

How can I identify a Buildbot environment by environnment variable?

Does Buildbot provide an environment variable in CI jobs to allow it's identification like e.g. Travis does with TRAVIS?
Last I checked Buildbot does not set an environment variable which has for purpose to indicate that build code is being run through buildbot. In my own setup I do need a few variables that my build code uses so I've setup a dictionary like this:
from buildbot.plugins import util
env = {
'BUILDBOT': '1',
'BUILD_TAG': util.Interpolate("%(prop:buildername)s-%(prop:buildnumber)s"),
'BUILDER': util.Property('buildername')
}
This dictionary can then be used to configure builders:
util.BuilderConfig(
name="foo",
workernames=["a", "b"],
env=env, ...)
The env parameter makes it so that all shell commands issued by this builder will use the environment variables I've declared in my dictionary.
I use BUILDBOT to detect whether the code is running in buildbot at all. The other variables are passed over to services like Sauce Labs and BrowserStack in order to identify the builds there, or they are used for diagnostic purposes.

Set application name on stage files on capistrano

I'm trying to deploy my application with the multistage/ext estensions. It works fine when each stage is in different servers. But for one particular proyect I want to deploy two stages to the same server.
For that I need to set a different application name on each server. And that is what isn't working.
I moved the application variable to the stage file and define the deploy_to variable with lazy loading.
config/deploy.rb
set :stages, %w(production beta)
set :default_stage, "beta"
require 'capistrano/ext/multistage'
#set :application, "myapp-beta"
set :user, "deploy"
set (:deploy_to) { "/home/#{user}/applications/#{application}" }
config/deploy/beta.rb
server "my.server.com", :web, :app, :db, primary: true
set :application, "myapp-beta"
set :domains, "beta.myapp.com"
set :branch, "beta"
I get Please specify the name of your application, set :application, 'foo' error
Maybe I should be thinking about deploying to a different server, but now I really want to understand why this isn't working.
thanks
There is nothing functionally wrong with your code. As Lavixu mentioned, as long as you specify the stage name when deploying (i.e. cap beta deploy:setup, cap beta deploy) then the application variable will be recognised.
set (:deploy_to) { "/home/#{user}/applications/#{application}" }
Should be
set :deploy_to, -> { "/home/#{user}/applications/#{application}" }
I had to use fetch to reference other capistrano variables. ie:
set :deploy_to, -> { "/var/deploy/#{fetch(:application)}" }
Trying to access application directly raised a undefined local variable or method 'application' for main:Object error.

How do I define my ENV variables?

I have the following configuration for both my production and development environments.
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:user_name => ENV["MANDRILL_USERNAME"],
:password => ENV["MANDRILL_PASSWORD"]
}
I googled this for a while but I didn't find an answer that made me understand. How do I set the ENV variables both for development and for production for my rails project?
For development, you'll want to use a configuration file that is ignored in version control. It could be YAML, JSON, ruby, bash... it's a trivial choice. You'll then use an initializer file to make sure these variables are loaded when the app boots. For production, you can copy the config file (it may even have different settings) into the right place as part of your deploy process; or if you're using heroku, you can set these from command line using heroku config:add YADA=yada.
Here's a basic example with settings coming from a YAML file:
# config/settings.yml
development:
MANDRILL_USERNAME=secret_username
MANDRILL_PASSWORD=secret_password
ANOTHER_SECRET_SETTING=the_list_goes_on_and_on
production:
MANDRILL_USERNAME=different_username
MANDRILL_PASSWORD=another_password
ANOTHER_SECRET_SETTING=get_the_idea?
# .gitigore
# ...
config/settings.yml
# config/initializers/environment_settings.rb
environment_settings = YAML.load_file('./config/settings.yml')[Rails.env]
environment_settings.each do |key, value|
ENV[key] ||= value
end
They're environment variables; you don't set them for development and production.
You can set application config variables in a variety of ways, or you can set them in the shell before running, or you can use environment-specific names, or...

Resources