I read somewhere but cannot seem to find where to add secret keys into Heroku without needing to put it into the source code git repository?
I guess that helps keep it secure when I am pushing into github.
How do I do that and does that make sense to do?
http://docs.heroku.com/config-vars
Then add the development keys to an initializer:
#config/initializers/keys.rb
development:
SOME_KEY = 'abc123' #not your production key
testing:
SOME_KEY = 'abc123' #not your production key
#production:
#blank
Optionally add the initializer to .gitignore. Not required as your production key isn't stored.
As Mark has suggested, the best way would be Heroku environment vars. you can read about them here:
To do so, you need to use Heroku CLI which you need to download and install it based on your operating system. Don't forget to set up Heroku CLI with these 3 steps:
$ heroku login
$ cd ~/myapp
$ heroku create (your Heroku app name)
now it's time to set up config variable. The command is:
$ heroku config:set <ENVIRONMENT_VARIABLE>=<VALUE>
for example I'm gonna save my API key here as a config var:
$ heroku config:set DARKSKY_API_KEY=8e11111111162218d22222222229cc22222c6
and now it's time to use it in you server side code. For Nodejs you can access them by:
process.env.DARKSKY_API_KEY
like so:
const weatherURL =`https://api.darksky.net/forecast/${process.env.DARKSKY_API_KEY}/${latitude},${longitude}?units=si`;
For other languages like Ruby, Java, ... check this link.
you can view your config vars by typing:
$ heroku config
or removing a config var:
$ heroku config:unset DARKSKY_API_KEY
Also, I was thinking about a .env file for heroku config vars that we can edit them locally and then upload them on heroku. Finally, I come up with this solution.
To save the cofig vars locally from heroku and be able to change them locally in a file, later down the road when it's needed, we can run:
$ heroku config | sed 's/: */=/g; /^=/d' >> HEROKU_CONFIG_ENV.env
which HEROKU_CONFIG_ENV.env is just a file name and you can name whatever you like.This script is gonna save HEROKU_CONFIG_ENV.env file on the root of your project.
After modifying the keys, it's the time to upload them on Heroku and set heroku config vars by running:
$ heroku config:set $(cat HEROKU_CONFIG_ENV.env | sed '/^$/d; /#[[:print:]]*$/d')
that's it.
Related
I am trying to push images I have built locally to the GitHub Container Registry aka Packages.
I have authenticated GitHub using PAT and authorized access to the organization. Let's name this organization EXAMPLEORG.
used the following command:
export CR_PAT=ghp_example_pat ; echo $CR_PAT | sudo docker login ghcr.io -u exampleuser --password-stdin
After that, I used the following command to push the image to ghcr.io:
docker push ghcr.io/exampleorg/exampleapp:v0.5
Unfortunately, I am getting this message after trying to upload image layers:
unauthorized: unauthenticated: User cannot be authenticated with the token provided.
Does somebody knows what I am missing here?
Followed this guide:
https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry
Is there something more I need to do in order to manually push image to Org packages (not interested to do it from the workflow at the moment).
Apparently, it was due to the wrong content of the ~/.docker/config.json file. During the first command, it happens to fail while writing. So I used sudo to circumvent this, and indeed it was circumvented, but the new file is now written in /root/.docker/config.json which is not desired outcome. Using docker login afterward will not read the config file from the root's home.
The solution to this is not to use sudo instead delete ~/.docker/config.json and then execute:
export CR_PAT=ghp_example_pat ; echo $CR_PAT | docker login ghcr.io -u exampleuser --password-stdin
So currently I'm working on a project on Heroku with Drupal and my issue is that I want to reset the database each time I deploy to master, yes I know it not ideal but its a development env because I'm working Drupal plugin and it would be nice if changes happened it could just reset to a state.
But when I try to connect using psql and some variables I just get password authentication failed for user even tho I know its the right password because I got it from Heroku itself.
Currently, I have tried using the console to try to make in connection soi could run a DROP TABLE command for me to afterword import an SQL file with the basic setup using pg_dump, and put it into a .sh script and run it with and release: in a procfile
Until now I have this as a release.sh file where I only tried in the console on heroku
PGHOST=HOST PGPORT=5432 \
PGDATABASE=DB \
PGUSER=USER PGPASSWORD=SOMEPASS \
psql
Try below command to reset DB
heroku pg:reset DATABASE_URL
When I run "sbt ~run" I can see that mode is set Dev as expected. However, when I run "heroku local web" the server runs in Prod mode. Any idea how I can get this set to Dev mode? Do I have to set any variable with heroku config CLI ? My intention is to test with heroku local before pushing to Heroku git.
Have tried this in my Procfile:
web: target/universal/stage/bin/myapp -Dhttps.port=${PORT} -Dhttp.port=disabled -Dhttps.keyStore=conf/generated.keystore -Dlogback.configurationFile=conf/logback.xml -Dapplication.mode=DEV
But still it shows Prod. When server runs, the conf file is set programmatically with "-Dconfig.resource=root-dev.conf".
You'll want to use environment variables for the application.mode in some fashion (after all, that's what environment variables are for: changes specific to an environment). There are a few way you could do this, one is to create an APPLICATION_MODE env var, and use it in your Procfile thusly:
-Dapplication.mode=${APPLICATION_MODE:-DEV}
This will set -Dapplication.mode from APPLICATION_MODE and default to DEV if the env var is not set. Then you can set the env var on Heroku like this:
$ heroku config:set APPLICATION_MODE="PROD"
Or you could put the whole -Dapplication.mode=DEV in JAVA_OPTS in your .env file in your local repo like this:
JAVA_OPTS="-Dapplication.mode=DEV"
The heroku local command will pick up .env and load it. In this way you don't need to change anything on Heroku.
I can't manage to share two PostgreSQL heroku dbases between two heroku apps.
Doesn't work
What I've tried so far (I'm using the Heroku CLI):
$ heroku config:set DATABASE_URL=`heroku config:get DATABASE_URL -a my-heroku-app-original`
Setting config vars and restarting my-heroku-app... done, v20
DATABASE_URL: postgres://...correct url...
$ heroku config
=== msite-poniai Config Vars
DATABASE_URL: postgres://...correct url...
When I try to access psql, depending on whether I've added a new db or not, I get these two answer (I both cases heroku doesn't see the DATABASE_URL I've just provided.
$ heroku pg:psql
! Your app has no databases.
$ heroku pg:psql
! Unknown database. Valid options are: HEROKU_POSTGRESQL_NEWLYADDED_URL
Works
If I provide psql with the exact location of the db (in this case the original app that has the db as an add-on) it does manage to connect.
$ heroku pg:psql HEROKU_POSTGRESQL_ORIGINAL_URL --app my-heroku-app-original
psql (9.1.9)
I've read
I've closely followed advice given in Share database between 2 apps in Heroku and heroku-postgresql#create-new-db.
Simply copying a config var from one app to another does not attach the referenced database to the app. Were the host or password to change on the first, your second app would be broken. There is a highly alpha plugin you can try https://github.com/heroku/heroku-attachable-resources but it does some weird things like no longer give default names to databases, and overwrites DATABASE_URL if you don't pass in --config and sometimes doesn't let you remove databases.
I'd like to use heroku_san to deploy multiple environments to heroku. I'm using dragonfly for image handling and S3 for storage. Usually you can add your key and secret for the storage using heroku config:add S3_KEY=… S3_SECRET=… directly.
So I've added these details to the heroku.yml file used by heroku_san:
staging:
app: app-staging
config: &default
BUNDLE_WITHOUT: "development:test"
S3_KEY: XXXXXXXXXXXXXXXXXX
S3_SECRET: XXXXXXXXXXXXXXXXXX
S3_BUCKET: app-staging
but when deploying I'm still getting:
rake aborted!
ENV variable 'S3_KEY' needs to be set - use
heroku config:add S3_KEY=XXXXXXXXX
What am I missing here? Is there a better way then storing this information in a YML file?
There's no need to run heroku config:add manually. Just run heroku_san's config task:
$ rake all heroku:config
Repeat this whenever you update the heroku.yml file.
I was confused about this too, since it's strangely absent from heroku_san's documentation, but the option does appear in the list of rake tasks:
$ rake -T
and in the heroku_san code: https://github.com/fastestforward/heroku_san/blob/master/lib/heroku_san/tasks.rb
A simple solution/work-around:
heroku config:add S3_KEY=XXX S3_SECRET=XXX --app app-staging
Any better ideas?
I think you need to run the command rake all heroku:rack_env. This command will then set the environment configurations for you based on your heroku_san YAML configuration.