Working around ‘error: failed to push some refs’ on Heroku

I had a bad week this week trying to spin up some new Heroku dynos, getting loads of “pack-objects died of signal 13” which is basically the connection being interrupted, even on our relatively small 130MB repo.

The Problem: the initial push to Heroku takes a while and can fail especially on large repositories. If you’re deploying an experimental branch for a new Heroku setup with a high probability of the deploy being refused, even if the push succeeds, it can be very time consuming and frustrating.

The solution? Use EC2 as a conduit.

That is, spin up an instance of EC2, checkout and deploy your code right from within the same datacenter that Heroku uses.

 

FromĀ the AWS console, create security group, then a new EC2 instance following these 4 steps (click to view a larger image).

1. Create a security group to allow port 22 inbound (if you don’t have one already)

Security Group

2. Create a new instance, using this security group and create a new key (download this key)

Select Image

3. Confirm & launch

Launch Instance

4. View the instance to get the hostname

Launched Instance

Then from bash on your local machine:

From the directory to which you downloaded the keyfile (i.e. heroku.pem), in bash:

# connect to EC2 instance (replace hostname with that of your new instance, step 4)
ssh -i heroku.pem [email protected]

# install git
sudo yum install git -y
cd ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''
cat id_rsa.pub

# manual step: copy to clipboard
# manual step: add key to Heroku 
# manual step: add key to github 

# checkout from github
cd ~
git clone {github-url}
cd {repository}

# add heroku remote & push
git remote add {remote-name} [email protected]:{app-name}.git
git push {remote-name} {branch}:master

# tweak any files?  set your git config, then commit & push as normal
git config --global user.email [email protected]
git config --global user.name "Your Name"
# git commit -a, etc

NB. Replace the curly bracket info with your own data.

End result? you can push your repo to Heroku but with a ~4MiB/s connection. Once the initial push is done, you can spin-down the EC2 instance and push the deltas from your local machine.

Reminder: EC2 resources cost money. Remember to spin-down when you’re done to avoid paying for something you’re not actively using.


One comment on “Working around ‘error: failed to push some refs’ on Heroku