How do I exclude my API KEY pushing to github? - heroku

if I exclude the file with API KEY and push to gitHub and then to Heroku the app doesn't work because the app can't get access to the api key.
What is the workaround? I'm quite a novice so comments or info with human readable language would be highly appreciated. Thanks!

You need to store the API key as an environment variable.
In heroku, go to your app, then settings and click on "Reveal Config Vars".
In there you can store your API key for your deployed server to use. This is also where you will store DB urls too, etc.

Heroku has interface for setting up environment variables.
So use environment variable locally (from environment or e.g. .env file) and remotely.
Also read on 12-factor-app

Usually this kind of stuff should be kept in Heroku config variables. I find heroku command line more convenience. You can download it here. Then, you can use this command to setup the new Heroku configVar at will.
heroku config:set API_KEY=1234567890 --app your_app_name
To see all configs,
heroku config --app your_app_name
Then, based on the language developed, you can access this configVar from code. For example, you can do this in Ruby on Rails' code.
<%= ENV["MY_API"] %>

Related

How can I change the database name according to the database credentials provided by heroku during production?

Heroku provides its own database name and other credentials, but my local database name is different.How can I change the database name according to the database credentials provided by heroku during production?
Use a package like dotenv. dotenv and variants of it likely exist for whatever language you're using.
Basically, you want to use environment variables instead of hard coding values into your code. So, instead of writing something like this:
my_database_connect('my_username', 'abc123')
You'd write:
my_database_connect(process.env.DB_USERNAME, process.env.DB_PASSWORD)
Heroku will already have these environment variables set on the "config" tab of your app. Then for local development, you'll create a file called .env and have this text in it:
DB_USERNAME=my_username
DB_PASSWORD=abc123
Don't commit .env to your git repository – it should only live on your machine where you develop. Now your code will run locally as well as on Heroku, and connect to the proper database depending on the environment it's running in.
Here's an article that explains this more thoroughly for node.js, although this is basically the best practice for general development: https://medium.com/#rafaelvidaurre/managing-environment-variables-in-node-js-2cb45a55195f
First I created an application name on Heroku. Then I deployed my app to heroku by connecting to github.
Heroku provides the database credentials after we deploy our applications. Then I redeployed the app through github by changing the configuration in application.properties file as follows:
#localhost configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost/transactions?useSSL=false
SPRING_DATASOURCE_USER=postgres
SPRING_DATASOURCE_PASSWORD=some_pass
#server database configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://ec2-23-23-247-222.compute-1.amazonaws.com/d6kk9c4s7onnu?useSSL=false
SPRING_DATASOURCE_USER=rimjvlxrdswwou
SPRING_DATASOURCE_PASSWORD=dd903753bc0adffb96ce541b1d55fb043472e32e28031ddc334175066aa42f69
Then you have to edit the config vars according to your application.properties files as shown in the figure below
config_var.png

Is it possible to get the release version in Heroku using PHP?

I'd like to get the release version of my app in Heroku on PHP. I was hoping it might be available as an environment variable, for example like $_ENV['HEROKU_RELEASE'] might retrieve v23. Is there a way to get ahold of this?
Yes, that is possible, by enabling dyno metadata on your app with the following command:
heroku labs:enable runtime-dyno-metadata -a
Then, Heroku will set a HEROKU_RELEASE_VERSION environment variable on your app (as well as other ones, which are all described in the article linked above).
That variable includes the number of the current release for your app.

How can I set Heroku config vars programmatically from inside the app running on Heroku?

I have an app running on Heroku with a small handful of settings that I want to change from time to time, and I'd like to use Heroku config vars to store these settings so they persist durably.
I know I can modify the settings from the Heroku dashboard or the Heroku CLI, but I'd like to modify them from within the app UI.
I can't seem to figure out how to modify the app's Heroku config from within the app code. If I simply modify the dyno's environment, for example, those changes do not persist to the app's config.
You can use Heroku Platform API for this. Especially this part. There is also a ruby client

Is there a way to set a default app for Heroku Toolbelt?

I have more than one app/git remote at heroku and I would like to know if it is possible to configure a default application so that, whenever I forget to specify the app (--app), the toolbelt would use it.
You can set the heroku.remote key in your repo's Git config to the name of the default remote. For example, if your remote is called staging, you could do this:
$ git config heroku.remote staging
To see how this works, here is the relevant source.
For more, information about this, see Managing Multiple Environments for an App.
You could also go for:
heroku git:remote -a <name-of-the-app>
or if you tend to make a lot of mistakes in the configuration of wrong apps, you can use this library I made: https://github.com/kubek2k/heroshell
This is a Heroku wrapper shell that allows you to work in the context of a given Heroku application
You can set the HEROKU_APP environment variable.
Found this question while searching for it myself. The other answers refer to Heroku's old ruby-based CLI. The new JS CLI doesn't seem to support the same git-remote-reading feature. A quick search of the source code on GitHub found this.

How can i create a clone of an existing app on heroku from another heroku app as separate app?

I have a main app on heroku and another app A on git in location github:a.
I want to create, when it is necessary, copies of A as A1,A2,A3...AN as separate apps on heroku from my main app automatically with different parameters.
How can i do that?
Edit: This process should be done by my main app automatically.
Updating this answer due to Heroku command DEPRECATION:
heroku fork has been deprecated as a core command as of 12/01/2017.
You will need to install the heroku-fork plugin to continue using this command.
heroku plugins:install heroku-fork
Here is a link to the Github plugin repo.
Use heroku fork to copy an existing application, including add-ons, config vars, and Heroku Postgres data.
See this KB page: Forking Applications.
Heroku toolbelt now provides a fork method to clone an existing application, see my answer here :
how to clone a project on heroku
There is a new feature on Heroku called Review Apps. One can create copies of the app manually or set up automatic copies from new PRs on Github.
Read more at: https://devcenter.heroku.com/articles/github-integration-review-apps
Simply create new applications and push your code to them. If you need to copy data, checkout the pgbackups transfers.
For management purposes, check out this dev center article.
To do this programatically, you'll need to look at the Heroku gem, and then figure out a way of getting something to git push to the appropriate remote. I would be surprised if this was possible to be honest.

Resources