How to push certain commits only to Heroku but not github - heroku

I am using a github repository that is public and deploy my app on Heroku. How do I only push commits to Heroku and not to github when I then push to origin again?
More specifically, I need to edit a .env file for Heroku that I do not want to publish on Github. I did a few commits on that file and pushed it to heroku via git push heroku master.
I do not want those commits to be pushed to github as well. Now when I make new commits and push it via git push origin master, are the previous commits (which were only ment for Heroku) pushed as well? If yes, how do I avoid this?

A few things:
1) .env probably shouldn't be checked in. A common pattern is to create a .env.sample file with placeholder values, add .env to gitignore and instruct users to copy .env.sample to .env and populate the placeholder values for development.
2) Those sensitive values can be set in Heroku's environment on the CLI via heroku config:set FOO=bar and will be available to your app through: ENV['FOO']

Related

Why can't I connect Heroku project with my private Github repository? [duplicate]

This post relates to a rapidly changing event.
I simply want to connect to my GitHub account. When I do it, I get this error message as a small red pop up on the upper right side of the page:
Items could not be retrieved, Internal server error.
As of May 25, 2022, at 19:52 UTC, GitHub integration has been re-enabled:
We are happy to report that the GitHub integration is re-enabled! You can now reconnect with GitHub and restore your Heroku pipeline functionality, including Review Apps, with newly generated tokens.
You can connect to GitHub immediately or wait for the enhanced integration as described in this blog post. To re-establish your GitHub connection now, please follow these instructions.
Here is what the referenced blog post says about "enhanced integration":
In an effort to improve the security model of the integration, we are exploring additional enhancements in partnership with GitHub, which include moving to GitHub Apps for more granular permissions and enabling RFC8705 for better protection of OAuth tokens. As these enhancements require changes by both Heroku and GitHub, we will post more information as the engagement evolves.
No timeline is mentioned for availability of the enhanced integration.
Between April 15 and May 25, 2022, Heroku's GitHub integration feature was disabled while Heroku investigated a security breach. During that time, deploying was still possible via other means, most notably via git push.
To mitigate impact from potentially compromised OAuth tokens, we will revoke over the next several hours all existing tokens from the Heroku GitHub integration. We are also preventing new OAuth tokens from being created until further notice. Your GitHub repositories will not be affected in any way by this action.
Which Heroku features have become non-operative due to the removal of the Heroku-GitHub integration?
Enabling review apps
Creating (automatic and manual) review apps
Deploying (automatic and manual) review apps
Deploying an app from GitHub (either manual or automatic)
Heroku CI cannot create new runs (automatically or manually) or see GitHub branch list
Heroku Button: unable to create button apps from private repositories
ChatOps: unable to deploy or get deploy notifications
Any app with a GitHub integration may be affected by this issue. To address specific integration issues, please open a case with Heroku Support
Migrating from GitHub deployment to Git deployment
At 2022-04-21 23:53 UTC, Heroku provided extended instructions for migrating from GitHub-based deployment to Git-based deployment:
While our customers remain unable to reconnect to GitHub via the Heroku dashboard, we wanted to share a supplement to the code deployment methods previously provided. For instructions on how to change your deployment method from GitHub to Heroku Git, please refer to the following Help article: How to switch deployment method from GitHub to Heroku Git with all the changes/app code available in a GitHub repo.
This is due to an issue reported at their status portal, here.
For now, the solution is to use another pushing strategy.
The best one, for now, is using their remote with Heroku CLI. The steps for it are:
1. Install the Heroku CLI if not yet installed
Further information about installation and setup can get here
2. Perform login with the heroku login command
You will be prompted with your default browser window for completing the login at the Heroku portal.
3. Assuming you already have your app set up at Heroku (if not, please follow this), you just need to add a new remote for your Git repository with Heroku CLI.
Run heroku git:remote -a example-app - substitute "example-app" with your app name.
4. git remote -v to check if the remote has been set successfully
You should see something like this appear as a response:
heroku https://git.heroku.com/your-app-name.git (fetch)
heroku https://git.heroku.com/your-app-name.git (push)
5. Push your branch to the new heroku remote
git push heroku your_branch_name
6. You should see the progress of your deployment being displayed in the terminal
The reference for this answer has been taken from here, which can also be used if further information other than the one provided in this answer is needed.
I'm in the same situation, and, as others stated, it's due to a Heroku security issue. In the meantime, you can deploy your code by using the Heroku CLI.
So, on the Heroku web dashboard, select Heroku Git:
Then set up the Heroku CLI with heroku login.
Finally, if your repository already exists on GitHub, you need to add a new remote by running:
heroku git:remote -a your_app_name
git push heroku master
You can find more information about this solution in the official documentation.
It is just a temporary thing, and more details about this issue are here.
You could push to both GitHub and Heroku at once for a temporary solution:
git push -u origin <branch>
git push heroku <branch>
I see the previous answers, but since I was facing an issue with review-apps (PR apps), mostly you will be working with different branches in that case, so here is a solution for pushing your stuff other than the (master/main) branch to Heroku.
First make sure your remote origin is set up correctly
heroku git:remote -a your_awesome_app
You can also confirm it by git remote -v and you should see your origin pointing to your Heroku application.
git remote -v
heroku https://git.heroku.com/your_awesome_app.git (fetch)
Here origin name is heroku.
If you want to deploy your changes from the main branch
git push heroku main
If you want to push your changes from feature branch (other than the* main branch)
Then push your feature branch to Heroku using the below command
git push heroku feature:main
heroku - is your origin name (confirm your origin name with git remote -v
feature - is your current branch which is other than main/master branch (check your branch name with git branch or git status)
For those who are using this integration for deployment purposes this, I suggest you use the deploy to a Heroku GitHub action here:
Deploy to Heroku
That way, you do not have to make significant changes to your deployment workflow.
I had the same issue. I already had cli installed.
git remote
Output:
heroku
origin
git remote -v
Output:
heroku https://git.heroku.com/YOUR-APP.git (fetch)
heroku https://git.heroku.com/YOUR-APP.git (push)
origin https://github.com/GitUserName/yourRepo.git (fetch)
origin https://github.com/GitUserName/yourRepo.git (push)
Verify using the Git branch, if your branch is named main. For example, you would do:
git push heroku main
For me it is
git push heroku master
Now push your local changes
git push heroku master
Output:
To https://git.heroku.com/YOUR-APP.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.heroku.com/YOUR-APP.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I do not care that it is different than its remote. I've been developing locally and did not realize the automatic Git deploys had been failing. I care about the local changes:
git push heroku master -f
Now the deployed application is working as expected.
If you are getting an error while trying to push, it may be due to having different branch names for development vs deployment. In such a case, follow the instruction below;
If you are developing on a branch and deploying via Git you must run:
git push heroku <branchname you're developing on>:<branch you're deploying from>
This article goes into details on the behavior:
Duplicate Build Version Detected
Here's the easy work pattern I have used for Heroku. This is intended to help others who may not have gone through this before. I used this previously (2014-5) and had to set it up again last night to push.
First add the Heroku remote:
git remote add heroku https://git.heroku.com/YOUR-APP.git
As GitHub is often "origin" (git push origin...), this adds another remote destination, "heroku" (git push heroku...)
git remote
Output:
heroku
origin
My pattern for code/git/pushing:
Local development is the same. Push to GitHub, merge, and nothing changes.
Set your deploy to "Heroku git" as #a-chris outlines.
To push to Heroku, simply push the correct branch to the newly added "heroku" remote source. I use the --force option to dismiss any possibility of conflict. Unless you have been using Heroku Git and branching previously, there should only be one branch - typically "master" to use.
This will trigger a deploy. You can watch or review in the dashboard as well as the terminal. Treat the new "heroku" source as a directory to dump code to promote and not a repository you want to keep history, etc. A second-class citizen in this particular pattern.
I push from my local terminal now instead of auto-deploy or via the dashboard button. If your organisation is large, I recommend controlling access. Many developers may not have experience juggling multiple destination repositories or to catch an accidental push.
To trigger a local push, be sure your master (or whatever) is up to date...watch your commit hashes!
This will set you up to follow advice such as BR19_so and others.
Henrique Aron's answer is working for the local machine.
For a remote SSH server, you will face an IP mismatch error.
To resolve it:
Log in to the Heroku website
Go to account settings
Reveal the API keys in the panel
In the server CLI, type heroku login -i
Input email, use the API key as the password, and you can follow the rest of the steps of a Git remote push
I am using a 2014 MacBook Pro with macOS 11.6.5 (Big Sur) version for command
brew tap heroku/brew && brew install heroku
I was getting an error and a message to update to the latest Xcode. The latest Xcode needed the latest macOS, which I could not upgrade to (I needed a 2015 MacBook Pro or newer). For a 2014 MacBook Pro, I was able to Install Xcode_13.2.1.xip and was able to run. Now I can push updates to Heroku.
brew tap heroku/brew && brew install heroku
There is an update regarding this issue. You can now enable GitHub integration as mentioned in the update.
If it doesn't work, you can try removing the App connection to GitHub (disconnect) and adding the connection again. Adding the connection again can also be done in incognito mode because sometimes an issue occurs related to cookies (mentioned here).

How to check if my repository has been deployed to heroku?

I have several repositories and only one has been deployed to heroku. How do I find out which one? Because I made a change in one repo and tried to push it but got a warning fatal: 'heroku' does not appear to be a git repository. Is there a command?
It looks like you have not created heroku repository yet or you are in wrong directory. Go to the directory where you have your app then create heroku with the help of below commands.
git init
heroku create
after creating heroku you can add and commit heroku with the help of
git add .
git commit -am "any comment"
last step will be to push all your data to heroku server by
git heroku push master
If repository is already created, you can check associated repository by hitting below command
$ git remote -v
You may need to run heroku git:remote command to associate a Git repository with an existing application
for more information on how to deploy app click here

keep config file on heroku but remove from public repo on github?

I am trying out newrelic on a test app which is deployed to heroku.
I have both remotes origin(the public github repo) and heroku.
After pushing to heroku with the newrelic.yml file included, I would like to be able to push changes back to origin but not include the newrelic.yml file because it contains a license_key.
I tried putting config/newrelic.yml in .gitignore but it still tracks the file and pushes the file to origin. Then I tried deleting the file but can't get a clean "git status".
I tried rm config/newrelic.yml but then even with it in .gitignore the next time I push to heroku it removes the file on heroku causing newrelic monitoring to stop.
I saw other similar issues on SO with answers referring to heroku config vars https://devcenter.heroku.com/articles/config-vars but don't understand how this relates if it does, to removing the config file for github.
Thanks
Heroku take all your files from Github, each push replace all your files by the new ones. I don't think you can do something like this.
If you put your file in the .gitignore, you won't be able to push it to Heroku.
A solution may be to use branch on Github, on the master you keep your code without the config file, and on the branch "Heroku", you add the file and use it to do the push on Heroku.

Managing Heroku Multiple environments with existing app

I've got an app, that has 'heroku' configured as a remote, to one application.
Let's call this app 'MyAppDev'
However, I have another app, called 'MyAppLive'
I want to configure deployment like this:
git push staging
push to MyAppDev
git push production
push to MyAppLive
How can I do this?
Also, what about environment variables?
Both apps have MongoLab, so I'd like the MyAppDev app to use it's own db....
Here are the steps that you'd need to follow
git remote rm heroku - this will remove the heroku remote from your application
git remote add production <production apps heroku git repo url> - this will add a new remote named 'production' pointing at the production apps git repo url (you can get this from the My Apps page on heroku.com
git remote add staging <staging apps heroku git repo url>
This now means you can do git push production master or git push staging master to push your codebase to either repo.
NOTE If you need to push branches to Heroku you need to push them into the master branch of Heroku.
eg. assuming a staging branch locally you would do;
git push staging staging:master
To push your locally staging branch into master of the staging remote.
Any addons you use would need to be duplicated to the staging application.
Config variables can either be done manually via heroku config:set or you can use the plugin detailed at the bottom of this page https://devcenter.heroku.com/articles/config-vars which allows you to push and pull your Heroku variables into a .env file suitable for running with Foreman locally. Becareful about overriding variables though - I tend to do my variables manually as I don't typically have many.

GIt Deployment + Configuration Files + Heroku

I'm using Heroku to host a Rails app, which means using Git to deploy to Heroku. Because of the "pure Git workflow" on Heroku, anything that needs to go upstream to the server has to be configured identically on my local box.
However I need to have certain configuration files be different depending on whether I'm in the local setup or deployed on Heroku. Again, because of the deployment method Heroku uses I can't use .gitignore and a template (as I have seen suggested many times, and have used in other projects).
What I need is for git to somehow track changes on a file, but selectively tell git not to override certain files when pulling from a particular repo -- basically to make certain changes one-way only.
Can this be done? I'd appreciate any suggestions!
You can have config vars persistently stored ON each heroku app's local setup so they do not have to be in your code at all! so the same code can run on multiple heroku sites but with different configuration. It very simple, easy, elegant...
It's the approach we used. (We used it for the SAME thing... we have multiple clones of the SAME app at Heroku, but we want only ONE source at github, in our dev local directory we do the PUSH to ORIGIN (github), then when we have it the way we like it, we CD to the prod local directory, which goes to the SAME github repository, and we ONLY PULL from GITHUB into this directory, never push (eg, all pushes to github come from our dev directory, the prod directory is just a staging area for the other heroku app.)
By having the different configs ON the different HEROKU sites (as explained below), the EXACT SAME CODE works on BOTH heroku sites.
So our workflow is: (the key is that BOTH directories point to SAME github repo)
cd myDEVdir
*....develop away....*
git add .
git commit -am "another day, another push"
git push origin *(to our SINGLE github repo)*
git push heroku *(test it out on heroku #1)*
cd ../myPRODdir
git pull *(grabs SAME code as used on other site *)
git push heroku *(now the SAME code runs on Heroku #2)*
that's it!
Now here's how you keep your site-specific config vars ON the heroku site:
http://docs.heroku.com/config-vars
on your local command line, for EACH of your two local directories, do:
$ heroku config:add FIRST_CONFIGVAR=fooheroku1
Adding config vars:
FIRST_CONFIGVAR => fooheroku1
$ heroku config:add SECOND_CONFIGVAR=barheroku1
Adding config vars:
SECOND_CONFIGVAR => barheroku1
to see the ones you have defined:
$ heroku config
FIRST_CONFIGVAR => fooheroku1
SECOND_CONFIGVAR => barheroku1
then cd to your other directory myPRODdir and do the SAME thing, only set the same remote heroku vars to fooheroku2 and barheroku2.
then in your rails app you simple refer to them like so:
a = ENV['FIRST_CONFIGVAR']
One app will read 'fooheroku1' the other app will read 'fooheroku2'
And finally, on your LOCAL directory myDEVdir, where you run in DEV mode, put the same config commands in your config/environment/development.rb file your 'dev' version of the config vars will be set to whatever they should be:
ENV['FIRST_CONFIGVAR'] = "foodev"
ENV['SECOND_CONFIGVAR'] = "bardev"
Easy, elegant. Thanks, Heroku!
Here are a few solutions:
1) If you want to ignore files only on heroku, use slugignore
2) If your changes are minor, stay DRY and use universal config files, inserting switches for server-specific behavior
if Rails.env == "production"
#production server code
elsif Rails.env == "development"
#development server code
else
#test server code
end
3) If your changes are major, write one config file and add a "smudge file" to config/initializers for each additional server. Basically, you would have separate files using the technique in (2).
4) If your changes are SWEEPING (unlikely), then maintain separate branches for each server.
5) This script can do exactly what you requested, but may be overkill.
I hope that helped.

Resources