Heroku: run npm install and gulp build for a Django app - django

I have a Django app that I managed to deploy with Heroku. My Procfile file only contains :
web: gunicorn omegapp3.wsgi --log-file -
So when I run heroku local it works.
But when I deploy with heroku push master, the console detects a Node app because the app has a package.json and then the build fails.
What I would like to do is the following :
run npm install to install gulp etc.
run gulp build.
Do you know how I can do that ?

According to the official documentation, you should add multiple buildpacks to your setup, rather than a single multi buildpack.
For example, if you wanted to deploy an app that uses a Nodejs package (i.e. grunt, gulp, etc) to do some setup on your app, you would run this from your command line:
heroku buildpacks:add --index 1 heroku/nodejs
The add command adds the Nodejs buildpack as an additional buildpack, rather than replacing your current buildpack. Note the --index 1. This specifies that the Nodejs buildpack is the first in the order of buildpacks. This is important because the final buildpack is the one used for actual process type. You can call heroku buildpacks from the command line to verify the buildpack setup. I run a Python app, so my heroku buildpacks looks like this:
=== your_app_name_here Buildpack URLs
1. heroku/nodejs
2. https://github.com/heroku/heroku-buildpack-python
Then, as stated by rocketer, you can place this in your package.json file to be run on deploy:
"scripts": {
"postinstall": "./node_modules/.bin/gulp build"
}

Solved by using $ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git.
It allows to use node and python (you have to specify in a .buildpacks file).
In ordre to run gulp build, I added the following to my package.json :
"scripts": {
"postinstall": "./node_modules/.bin/gulp build"
}

Related

Can I download source code which has deployed with github from Heroku?

I have deployed my django-app on heroku with Github.
It is test server so I am using sqlitedb.
But because of the dyno manager, my sqlitedb resets everyday.
So I am going to download only db on heroku.
I tried this command.
heroku git:clone -a APP-NAME
But this clones empty repository.
And when I run $heroku run bash -a APP-NAME command, I got ETIMEOUT error.
Is there any other way to download the source code on heroku?
What you want to do with git is not possible because changes to the database is not versioned.
The command to run bash on Heroku is heroku run bash, not heroku bash run. You may have to specify the app using the -a flag: https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-run
I have solved with downloading the application slug.
If you have not used git to deploy your application, or using heroku git:clone has only created an empty repository, you can download the slug that was build when you application was last deployed.
First, install the heroku-slugs CLI plugin with heroku plugins:install heroku-slugs,
then run:
heroku slugs:download -a APP_NAME
This will download and compress your slug into a directory with the same name as your application.

Heroku build React app before deploying through Django

I successfully deployed my Django project to Heroku. But I want to be able to automatically build React before deploying.
The React app is served through Django, so I have the bundled files of React in my templates and static folders of the Django app.
The project structure looks something like this:
react/
build/
components/
App.js
package.json
...
django/
templates/
static/
...
Procfile
requirements.txt
Pipfile
I have both the React and Django projects in the same repository and I want to run npm run build automatically before Django is deployed.
I set up the build script to move the bundled files from React to Django.
From what I've read, I need to add the NodeJS buildpack, but I don't know how to set it up to run before the Python buildpack.
Basically, the NodeJS process should just build my React app and then terminate. After that, the Python process should start up and deploy the Django project.
One solution to do this would be to use Docker and deploy that image to Heroku.
But, is there an easier way?
You can add indexed buildpacks in heroku. Take a look here for adding multiple buildpacks to your project.
I wrote a simple tutorial on how one can deploy a React + Django app to Heroku. You can take a look if you're interested. In your case, you only need the instructions for adding the buildpacks.
$ heroku buildpacks:add --index 1 heroku/nodejs
$ heroku buildpacks:add --index 2 heroku/python
You don't need to setup any extra commands to build the React app. Heroku automatically installs and runs the build script from your package.json file(If it's in the project root).
I found a way to do this.
I added the nodejs buildpack as the first one and then I created a package.json file in the root directory:
{
"name": "myapp",
"version": "1.0.0",
"scripts": {
"build": "cd react && npm install && npm run build"
},
"dependencies": {},
"devDependencies": {},
"cacheDirectories": [
"node_modules",
"react/node_modules"
]
}
Heroku is looking for a package.json in the root directory and automatically runs the build command.

Install poppler onto Heroku Server django

I am trying to install poppler on my Heroku server because I am using pdf2image as a python package. However, I can't just run brew install poppler like I did on my Mac.
I have tried to add some heroku buildpacks off the internet but with no luck. Anytime pdf2image runs I get this error.
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?
Is there something I can do on the command line to get poppler installed while keeping heroku/python as my buildpack?
You can install APT packages with an experimental function on Heroku
Steps:
Add the buildpack to Heroku.
heroku buildpacks:add --index 1 heroku-community/apt
Make a file named Aptfile in your project folder and write poppler-utils inside.
Commit and push.
Is there something I can do on the command line to get poppler installed while keeping heroku/python as my buildpack?
Heroku lets you run multiple buildpacks. I haven't tried this buildpack, but I'd recommend adding this buildpack to your existing app:
heroku buildpacks:set heroku/python
heroku buildpacks:add --index 1 https://github.com/survantjames/heroku-buildpack-poppler.git
Then redeploy your application.

Heroku gettext installation

I want to install gettext in Heroku. I have added: https://github.com/lexifdev/heroku-buildpack-gettext.git into the buildpacks section of my app. However, when I run
heroku run python manage.py compilemessages -l en
I get the error message: CommandError: Can't find msgfmt. Make sure you have GNU gettext tools 0.15 or newer installed.
Had the same problem my friend. Here's how I fixed it:
From the heroku dashboard, add the following buildpack: https://github.com/grauwoelfchen/heroku-buildpack-gettext.git
On your next deployment to heroku (e.g. a git push), it and any other buildpacks you have will be built and installed and all should work.
Here is how I did it
type the following commands
heroku buildpacks: add https://github.com/grauwoelfchen/heroku-buildpack-gettext
git push heroku master
Now, the gettext will be added

Heroku installing outdated requirements file

Im attempting to deploy a Django app using Heroku.
When I first ran
$ git push heroku master
My original requirements.txt had outdated dependencies and Django failed to deploy. After I edited my dependencies and attempted to deploy I noticed that Heroku is still attempting to install dependencies that I erased. Am I doing something wrong here?