Subscribe

Tag: environment variables

Meilix Generator is a webapp whose task is to take input as a configuration and start the Meilix build. But an anonymous person cannot start the Meilix build of any user and deploy the release in the repository. There are ways which are used as authentication passes through environment variable to start the build. In this article, I show the way I used to trigger Meilix by setting up environment variables in Meilix Generator.

Environment variables are of great use when one has to supply personal token in an open-source project for accessing the repository. So down there, we will have ways to configure the variables in Heroku and Travis. There are so many wikis out there but this one is the blend of both Heroku and Travis.

Heroku

There are several ways to setup variables in Heroku. The way I’m going to describe below is used to access Travis build using Heroku. Using the Heroku variable generated using Travis will help to trigger the build on Travis.

How:

Idea:We will use Travis CLI to generate a token (unique and keep it secret). Then provide the token as a variable name to the Heroku.Backdoor:This Travis token will give access to the Heroku to trigger the build on that particular Travis account. We use variable to provide the token since in the script we will use this variable as an environment variable to fetch the token in the place of token like as $token.Implementation:Open your terminal and type the following:

You will get a token, copy and paste it into your Heroku app’s settings config vars token. You have to use the `KEY` as the variable which is used in the script for triggering the build. Save it and you are done the setting of the token in the Heroku.

Travis

Now it’s time for Travis token. It is used to deploy the build to that repository only.We can use the token in two ways either paste it in the setting of that repository on Travis or pasting the encrypted form of that in the .travis.yml file in that repository. Both will work. But one thing to remember that you must have the write access to that repository.

How

,Idea:It is used in .travis.yml file as an environment variable to successfully build and deploy the application as a Github release.Backdoor:The token gives the permission to Travis to deploy the build application in the GitHub release of the repo(if one using to deploy it there only).Implementation:Head up to Github and generate a personal access token with scope repo. Copy the generated token in a safe place.

Way 1:

Paste the token in the setting of the repo in Travis in Environment Variable option. Now it will access the Github repository since it has got the permission from the personal token generated from Github.

Way 2:Open terminal:

cdrepo_name# cd into the cloned repotravisencryptsecret_token#replace secret_token with the token generated

or

travisencryptsecret_token-ruser/repo#if you are not in the repo

Copy that encrypted token and paste it in proper format in the .travis.yml file. Now you enabled Travis giving permission to release the build.

How can this idea be helpful to a developer

A developer can use this to build the Github Release in its repository. One can secure its token using this technique. One can use it to trigger its personal project in Travis using Heroku.

FOSSASIA‘sOpen Event Serveruses some environment variables to maintain different configuration for different deployment. However one con of it was, for local development, developers have to run many export commands.

It all started with the “It is not convenient, we humans are lazy.” phrase. A recent suggestion was to read this environment variables with files rather than running commands every time.This few example environment variable commands were:

In the above example, DATABASE_URL is a string and we store this string in SQLALCHEMY, setting the default by the default parameter(None in this case).

The type of environment variable is originally string, we can simply use env to parse these strings. For simplicity and understanding we can also use env.str. For different types, we can simply type cast them like env.bool(for boolean),env.int (for integer) etc. One good advantage of this envparse feature is the direct use of these type cast rather than checking for strings (i.e. reading the string from command line).

This helps in parsing as boolean instead of string. So, if we have INTEGRATE_SOCKETIO=truein .env file, we can write INTEGRATE_SOCKETIO = env.bool(‘INTEGRATE_SOCKETIO’, default=False). This automatically converts the true to boolean True in python.

In order to use envparse we install envparse with:

$pipinstallenvparse

In the case of Open Event Server project, you only have to install the requirements.txt and copy the .env.example already mentioned in installation doc.This is how we finally remove the headache of writing big exports again and again and fairly helps beginners to start off with Open Event Server project much more easily and quickly.