1

I have an application set up on heroku that is automatically deployed via GitHub. That setup is great and I don't want to change anything.

There is a use case now where I need to deploy the application from time to time without code changes (the build process fetches some recent data).

I can easily redeploy an app by going to the user interface and clicking "deploy a branch". However I would love to do this via an API. I could not find anything that helped achieve this in the Heroku Platform API Documentation.

I can create a Deployment via the GitHub deployment API but that doesn't seem to trigger a deployment on Heroku.

Any leads on how I can trigger a deployment for a specific app on Heroku?

Edit: I can't really make use of the answer Redeploy Heroku app without code changes as there is no answer that offers this functionality via an HTTP API.

leifg
  • 8,668
  • 13
  • 53
  • 79

1 Answers1

3

Heroku doesn't currently have a public API for GitHub Sync. So you will need to use their Platform API to create a build.

GitHub gives you a tar.gz under the URL https://github.com/<organization>/<repository>/archive/master.zip (you need to pass an authorization token in the headers of course).

Using curl, you can do the following:

curl -n -v https://github.com/<organization>/<repository>/archive/master.zip

That URL will be a redirection to a URL on GitHub authenticated to allow the download. Reuse that URL to create an Heroku build:

curl -n -X POST https://api.heroku.com/apps/<app name>/builds \
  -d '{
  "source_blob": {
    "url": "<the URL fetched before>",
    "version": "<the version of the code you're trying to deploy>"
  }
}' \
  -H "Content-Type: application/json" \
  -H "Accept: application/vnd.heroku+json; version=3"

That will trigger a new build, downloading the code from GitHub. Effectively doing the same as GitHub Sync internally does.

You can also see this tutorial: https://devcenter.heroku.com/articles/build-and-release-using-the-api

Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • The solution worked for me. However the generated github URL expires after a while so I wonder if there is a way to pass in the authentication via URL. – leifg Feb 11 '19 at 15:45
  • There is no way to get a permanent generated URL (that's basically an S3 URL with a timed credential). You need to follow the redirection first every time. – Damien MATHIEU Feb 11 '19 at 17:31
  • Turns out there is a way to do this without fetching the redirect. With this URL pattern you can use your github token every time: https://api.github.com/repos/:user/:repo/tarball/:branch?access_token=:token – leifg Feb 15 '19 at 11:03
  • @leifg nope, with the new github api, you must pass the access token as a header. And after you do that, it will return the `redirect` link which is temporary – Gzim Helshani Nov 06 '21 at 19:56