Create variable to be set in deployment - jekyll

How do I set a variable in jekyll for a link that will be determined by the environment that it's deployed in?
i.e.
If I deploy in sandbox I want {{site.REDIRECT_LINK}} to be set to a different URL than when it's deployed in production.
I'm assuming I set it in the _config file, but is there anything I need to set up for docker?

You'll need to set variables in your config file that match your environment names:
staging:
redirect_link: http://staging.example.com
production:
redirect_link: http://example.com
Then you can call {{ site[jekyll.environment].redirect_link }}. If you are building in your staging environment you will get "http://staging.example.com"

Related

Context variable for Jekyll in Netlify

Jekyll to detect the build waits the JEKYLL_ENV variable: https://jekyllrb.com/docs/configuration/environments/
Netlify sets the CONTEXT variable: https://www.netlify.com/docs/continuous-deployment/?_ga=2.28134843.62454114.1555938051-984068889.1555938051#environment-variables
As result Jekyll doesn't see that build i
Netlify's CONTEXT variable is not intended to be the same as Jekyll's JEKYLL_ENV. So, you'll want to set JEKYLL_ENV separately, probably using netlify.toml? Something like this may work:
# this will be the default for every branch other than the production branch
[build.environment]
JEKYLL_ENV="development"
# only for production branch, override to production
[context.production.environment]
JEKYLL_ENV="production"
I had the same issue and sorted it by adding JEKYLL_ENV in the environment variables in Netlify settings page. No need to add a toml file.
I successfully tested three different ways to set the JEKYLL_ENV environment variable, which can then be accessed in Liquid templates as jekyll.environment:
netlify.toml: Set the env var in the build command directly
# global context
[build]
publish = "_site/"
command = "JEKYLL_ENV=\"netlify\" jekyll build"
netlify.toml: Set a build environment
# global context
[build]
publish = "_site/"
command = "jekyll build"
environment = { JEKYLL_ENV = "netlify" }
netlify.toml: Run a custom build script
# global context
[build]
publish = "_site/"
command = "sh netlify.sh"
netlify.sh: Set the env var in the build command
JEKYLL_ENV="netlify" jekyll build

How to build Jekyll project production with Windows command line

I try to build my jekyll project on production mode using JEKYLL_ENV variable but it doesn't work.
Jekyll documentation specifies to set a production environment in the build command :
JEKYLL_ENV=production jekyll build
But on Windows, this type of syntax is not correct.
I used this following syntax, but it looks not working:
jekyll build JEKYLL_ENV=production
I also set 'manually' this environment variable but doesn't take effect :
setx JEKYLL_ENV production & jekyll build
and
set JEKYLL_ENV=production & jekyll build
I ran into this as well with my Windows/Jekyll setup. My workaround is to have production and development config files and set the environment variable in each file.
// _config.yml
environment: production
...<other config settings>...
--------
// _config_dev.yml
environment: development
Your prod environment should run jekyll build which automatically uses _config.yml. Your dev environment should run jekyll <command> --config _config.yml,_config_dev.yml. In the Jekyll config docs, "Settings in later [config] files override settings in earlier files." So, you can set the variable in prod and dev config files and use --config _config.yml,_config_dev.yml to set the variable in dev.
To do stuff with it in Jekyll, use Liquid statements to check for the environment variable. Config files set site variables, so check site.environment
// some file that will be processed by Jekyll
{% if site.environment == "production" %}
<do prod stuff>
{% elsif site.environment == "development" %}
<do dev stuff>
{% endif %}
On windows you should run two commands:
the first command set env to production
set JEKYLL_ENV=production
the second command run jekyll build or jekyll server
jekyll build
when you use development evn, run this command again:
set JEKYLL_ENV=development
This worked for me:
set JEKYLL_ENV=production | jekyll build
Running the two commands separately like Thxopen suggested worked. Then I tried running the two commands on one line using the separator character as mentioned here, and it worked nicely.

How to setup multiple environments (dev\stage\production) with elastic beanstalk?

I need to do the following
Change environment variables according to the published env. Set Set up cron jobs according to the dev. I I would like to run just 1 command line "eb deploy dev" or something similar.
Use setenv
You can set environment variables with setenv. These will then be remembered for that environment.
More details: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-setenv.html
Example
For example, suppose you have created an EB environment called 'staging' and you want to set the variable DB to 'localhost', you can use:
eb setenv DB=localhost -e staging
Crons
Now that you have a different environment variables, you can check them in a script etc. to decide if the cron should be set up.
Note that the crons may not actually have access to your environment variables so you need to set those again for the cron while setting up the cron.
This is my solution to the problem, it took some time to setup but now i can do all the changes with 1 command line.
Make your own folder with all the files for all the environments.
In .ebextensions folder setup empty config files for eb.
npm runs a script named "deploy.js" together with the flag of the specific env.
The script will do the following
copy the requested env data to the empty files according to the env
git stash the changes of .ebextensions folder (eb deploys using git)
eb use env
eb deploy
So now i can tun npm run deploy:dev and everything runs

Kubernetes set environment variables depending on namespace

I want to set specific env variables depending on the namespace.
The goal is to have one config yaml file for different namespaces and set different env variables/config maps for dev, qa and prod depending on the namespace which the config file is applied to.
Afaik, kubernetes doesn't come with this capability out of the box.
There are two ways to get around this:
Deploy a standard ConfigMap that contains everything any deployment needs. Make your app recognise the namespace and use the appropriate variable.
Deploy a sidecar app that generates a namespace-specific ConfigMap from a template. This sidecar will need access to kube-apiserver to deploy new ConfigMap manifests automatically.

how to use google api keys based on heroku application name

I've created a few different "environments" for my app that is hosted on heroku so I have:
appName-staging.heroku.com
appName-production.heroku.com
I want to use different google api keys for these applications, how do I do this?
i've created a google.yml file that looks like:
development:
api_key: 'ABCXYZ'
production:
api_key: 'DEFXYZ'
so I use ABCSZY when developing locally, and DEFXYZ for appName-production.heroku.com
question is, how do i get appName-staging.heroku.com to use a different key?
since every application deployed to Heroku is considered to be in "production", both
appName-staging.heroku.com and appName-production.heroku.com use the same key.
You could add a heroku config variable to each environment, allowing you to identify each one from within the app.
Something along the lines of:
$ heroku config:add APP_NAME_ENV=production --app appName-production
$ heroku config:add APP_NAME_ENV=staging --app appName-staging
Then you could grab the current environment from within your app using:
ENV['APP_NAME_ENV']
And if you've got your YAML file as a hash called something like GOOGLE_KEYS, the following would return the correct key for a given environment:
GOOGLE_KEYS[ENV['APP_NAME_ENV']]
The previous answer definitely works but doesn't account for the potential security threats which come with checking files which include private keys into source control. Having your google.yml file in source control will allow anyone with access to your repo to see your private API keys.
A more secure solution would be to delete the google.yml file and create different environment variables on your staging and production servers with the same key:
$ heroku config:add GOOGLE_API_KEY=<production key> --app appName-production
$ heroku config:add GOOGLE_API_KEY=<development key> --app appName-staging
Then, when this is needed you can refer to it in code via
ENV['GOOGLE_API_KEY']
This will allow you to share code without sharing your private API keys.
Some more information on using environment variables on Heroku can be found at https://devcenter.heroku.com/articles/config-vars