160

I'm wondering if there is a way to force Heroku to recompile the slug without pushing new commits and/or updating the config variables.

Why would I want to do this?:

I am using the Cedar stack on Heroku for a Rails 3.2 app, and I am having problems with the rake assets:precompile task failing (during compilation only --- later it works fine with a heroku run). I highly suspect this is due to certain environment variables not being available during slug compilation time, and I think the heroku labs:enable user_env_compile experimental feature will solve this.

However, with the user_env_compile feature turned on, config changes do not trigger a recompilation of the slug, and my code hasn't changed, so I don't have any new commits to push.

Of course, I could push a "dummy" commit with a trivial change, which is probably the simplest answer --- but I'm wondering if there's a heroku command that will let me directly recompile the slug.

Thanks!

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
Nathan
  • 7,816
  • 8
  • 33
  • 44
  • 1
    Nothing in the heroku CLI client? – Matt Ball Mar 15 '12 at 02:37
  • Not a direct answer, but it's always wise to document dependency changes like relying on SAAS features, etc. My advise is to add an entry to a CHANGELOG somewhere mentioning you now rely on this feature, and push that change to trigger a rebuild of the slug. – patcoll May 15 '13 at 13:16

10 Answers10

265

The simplest workaround for now is to push an empty commit.

git commit --allow-empty -m "empty commit"
git push heroku master
Weston Wedding
  • 136
  • 1
  • 8
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • 64
    You can do this and keep a clean commit history by force-pushing the previous commit afterwards: `git reset HEAD~; git push -f heroku master`. Incurs the time cost of another Heroku deploy, but clean history FTW. – Paul Annesley Jul 24 '13 at 09:33
  • 2
    Just to complement this an alias in your `~/.profile` add this: `alias heroku-rebuild="git reset HEAD~; git push -f heroku master"` or `alias heroku-rebuild="git commit --allow-empty -m 'empty commit' && git push heroku master"` so you can simply type `heroku-rebuild` – unmultimedio Dec 01 '16 at 02:42
  • 1
    regardless of the correctness of this answer, and subsequent comments, I can't help going 'wtf no way'. How is there not a better way? – user3190036 Oct 25 '21 at 19:16
  • I have variant on this that allows me to rebase and remove the empty commits easier using --auto-squash. `git commit --allow-empty --fixup HEAD && git push heroku master` – bas080 Apr 07 '22 at 16:36
100

Slug compilation is invoked with a git pre-recieve hook, so the only way to recompile is to push a new commit.

For completeness see this article on Heroku for the slug compiler. It discussed the use of the pre-recieve hook to invoke the slug compile process under the Compilation heading.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
nmott
  • 9,454
  • 3
  • 45
  • 34
  • 24
    Thanks for your answer. It's true that a git pre-receive hook compiles the slug. (see, for example: http://devcenter.heroku.com/articles/slug-compiler). That doesn't necessarily mean that heroku doesn't (or couldn't) provide an alternate mechanism to invoke the slug compilation such as a heroku CLI command. That said, no one's mentioned such a command, so I'm presuming your answer is spot on, at least for now. Thanks! – Nathan Mar 21 '12 at 02:50
  • 1
    @Nathan Maybe you could ask Heroku support for a way to trigger slug compilation via the Heroku Toolbelt? If they go for it, add another answer! – culix Aug 28 '12 at 14:56
  • 1
    FYI for me this solution did not work when I had a new ENV value for a setting in a js file that gets compiled into the Rails application.js. It took committing a space to the js file and a push to get the asset to actually recompile. – Josh Diehl Oct 14 '14 at 02:35
  • 1
    The heroku-repo addon looks nice but after trying it I do *not* recommend it. There is a bad bug: it ignores the --app flag. This caused me to accidentally rebuild our production environment, when I meant to rebuild staging. This bug has been open against the repo for well over a year now with no resolution. I have uninstalled this plugin. – jasoncrawford Feb 20 '15 at 18:25
  • please note that the command might have changed since this was posted. I just tried this and it kept giving me `! \`repo:rebuild\` is not a heroku command.`, then I looked at the help for `repo` after making sure the plugin is installed and I think the new command (not 100% sure about this though) is `heroku repo:purge_cache -a appname` – omnidan Jun 04 '15 at 13:04
  • 13
    `repo:rebuild` is no longer a valid command in the plug-in, as can be seen here: https://github.com/heroku/heroku-repo/commit/fb1306de5813dffadc046e82cd327175ca58c44b – blindstuff Jun 24 '15 at 21:41
34

My general approach is to do:

git commit --amend -C HEAD
git push heroku master -f

Not sure I'd do this in production without being certain, as it does technically rewrite the last commit but it shouldn't cause any issues in theory. It's perfectly fine for when you are testing things in staging though.

As an added bonus since most people are problem using Vim to edit commit messages SHIFT-ZZ will quickly save and exit the commit message for you without making any changes to it.

On a related note I'm mildly shocked Heroku still doesn't have this feature. I've often seen Heroku fail to deploy due to problems on their end.

Thanks to Michael Mior for the idea to use -C HEAD to avoid opening up an editor.

kuboon
  • 9,557
  • 3
  • 42
  • 32
Chris Nicola
  • 14,384
  • 6
  • 47
  • 61
  • 3
    Or just use `git commit --amend -C HEAD` to avoid opening up an editor at all and keep the commit message the same. – Michael Mior Jan 22 '14 at 02:12
  • Thanks @MichaelMior this worked great for me and didn't mess with my perceived git history. – James Ward Feb 21 '14 at 03:44
  • 2
    One thing to be careful with here is if you have a different central repo (in addition to heroku). If so, this will result in a merge / duplication of the commit in the history next time you pull if you've already pushed the last commit out. – Nick F Jun 09 '15 at 18:07
  • why is this so anti user ? – Lucke Apr 18 '20 at 14:39
24

Heroku have released a plugin that does this: https://github.com/heroku/heroku-repo

To install it:

$ heroku plugins:install heroku-repo

To force a rebuild:

$ heroku repo:purge_cache -a appname
$ heroku repo:reset -a appname
$ git push heroku
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • `purge_cache` sounds way more scary than just pushing an empty commit, `This will delete the contents of the build cache stored in the repository. This is done inside a run process on the application.` – jmunsch Apr 13 '21 at 16:23
  • To uninstall the plugin afterwards, run: `heroku plugins:uninstall heroku-repo` – Flimm Nov 07 '22 at 14:57
16

Update: heroku repo:rebuild has been removed.

Heroku has a Build API you can use, see: Building and Releasing Using the API


You can use the repo:rebuild command if the heroku-repo add-on.

heroku repo:rebuild -a appname

https://github.com/heroku/heroku-repo

Weston Wedding
  • 136
  • 1
  • 8
khamaileon
  • 197
  • 1
  • 5
  • 3
    Just commented on the other thread as well, but it's worth repeating here: The heroku-repo addon looks nice, but after trying it I do *not* recommend it. There is a bad bug: it ignores the --app flag. This caused me to accidentally rebuild our production environment, when I meant to rebuild staging. This bug has been open against the repo for well over a year now with no resolution. I have uninstalled this plugin. – jasoncrawford Feb 20 '15 at 18:27
  • 6
    The current version of heroku-repo does not have the rebuild subcommand – Mike Slinn Oct 18 '15 at 21:40
  • I have to reset the remote repo as the rebuild command is not working – Vaibhav Jain Feb 19 '16 at 10:48
  • 1
    You updated and you say that it works with the "Build API" but I don't really understand how it works... Any help ? Do I have to send a HTTP POST request to the API to rebuild my app ? Which on ? There is a token (security reason) ? – Dam Fa Aug 04 '16 at 13:39
9

Looks like this is not yet available. However, a feature request has be opened on the heroku github repo

It also mentions "an alternate way to build that is not reliant on git push"

https://github.com/ddollar/heroku-anvil

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
Kostia
  • 6,284
  • 1
  • 18
  • 15
4

There is a heroku plugin for this.

$ heroku plugins:install heroku-releases-retry
Installing plugin heroku-releases-retry... done
$ heroku releases:retry
Retrying v16 on ⬢ murmuring-lowlands-3398... done, v17
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
2

Heroku doesn't make it easy to find, but you can do it via the heroku dashboard if you navigate to your application. Select the "Deploy" tab and then scroll to the bottom of the page where you should see a section title "Manual deploy". In the input field enter your branch name and click the button "Deploy Branch"

IliasT
  • 3,973
  • 1
  • 24
  • 26
2

If you app is connected to a repo, then simply:

  1. Open the deploy tab and scroll to the bottom
  2. Click Deploy branch under Manual deploy

How to deploy branch manually

Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
-4

Remove the branch, then re-push it. No need to use a plugin.

git push heroku :master
git push heroku master
Elliot Winkler
  • 2,336
  • 20
  • 17