DEV Community

Cover image for Auto deployment to now.sh and Heroku from Github
Matthew Burfield for Chingu

Posted on

Auto deployment to now.sh and Heroku from Github

In Voyage 16, my team wanted to create a React app using the next.js framework, and a node server using express.

This is how we wanted to setup our project:

  • Have both frontend and backend in the same Github repo
  • Have our next.js react app automatically deploy to now.sh on merge to the master branch
  • Have our express server automatically deploy to Heroku on merge to the master branch

I managed to get this working, but ran into a number of challenges along the way so I decided to document the process.

First clone your repo so you have it locally. You should have just a README.md in the directory.

Frontend

  1. Use create-next-app to create a next.js app. Use "frontend" as the project name so it creates the app inside a folder called "frontend" and then update the project name inside the package.json file after creation.
  2. Before deploying to now.sh, it's important you setup a new team so you can link it to the Github organisation (e.g. chingu-voyages). This is important for using the Github integration. The way to create a new team is through the menu in now.sh now.sh menu image
  3. After creating the team, you'll need to invite a GitHub organization owner to the team so they can create the Github integration link. This is important as only the organization owner on Github can link it. To do this, select your team via the menu and follow the gif below to get the invite link. Send the link to one of the owners of the Github organization so they can join the team and setup the integration. finding the invite link for a team
  4. While you're waiting for the Github organization to be linked, you can deploy your project to now.sh.

    With the now.sh CLI; In your repo's root directory, run:

    > now frontend
    
  5. And answer the following questions:

    > Which scope do you want to deploy to?: (choose the team you just setup)
    > Link to existing project [y/N]: N
    > What's your project's name?: (type in the project name you want)
    > In which directory is your code located?: frontend/
    ..
    ..
    > Want to override the settings? [y/N]: N
    
  6. Now that your project has been deployed, you should be able to see it in now.sh under your team name's dashboard.

  7. You'll now need to wait for the GitHub organization owners to finish linking the Github integration. Once they are finished, it should look like this:
    linked Github integration
    Note that you will see the edit button, and it will look like you can link the integration yourself, but it won't work. You need to be the owner of the organisation that your Github repo is in.

  8. Once the Github integration has been linked, you'll finally be able to link your project for auto deployment! I created a test organisation and test repo so you can see what that looks like:
    Linking repo for auto-deployment

Congratulations! Your frontend is now linked to now.sh and will auto deploy whenever you push code to your master branch!

Backend

  1. Create a new folder in your repo's root directory called "backend".
  2. Inside the backend folder, create an express server
    Note: make sure you aren't assigning a port value directly, do this instead.

    const port = process.env.PORT || 3000
    
  3. Setup the Heroku CLI if you haven't done so already. Then login with the command:

    > heroku login
    
  4. Make sure you have committed your express app to git.

  5. Create your app in Heroku by running the command below in your repo's root directory. Obviously replace "project-name" with whatever you want your backend project to be called.

    > heroku apps:create project-name
    
  6. Now instead of pushing up the entire repo, we only want to push up the contents of the backend folder. To do that, we can use the git subtree command.

    > git subtree push --prefix backend heroku master
    
  7. Your server should now be deployed to Heroku and you should be able to see it working by opening the URL that Heroku provides you. (If something went wrong, run the command heroku logs --tail to debug!).

  8. In your Heroku dashboard, go to your project's settings and add a config variable of PROJECT_PATH and set it to backend.
    Heroku config var

  9. Also in settings, add a build pack and enter this url: https://github.com/timanovsky/subdir-heroku-buildpack.git Don't worry about selecting a supported buildpack, just add the url and click save changes.
    Adding Heroku buildpack

  10. Using the drag handles, drag the buildpack you just created, to above the heroku/nodejs buildpack that was already there.
    Reordered buildpacks

  11. Finally, on the deploy tab. Select your repo from the integrations dashboard, and then click the "Enable Automatic Deploys" button!
    Heroku integration step
    Heroku enable auto deploy

..
..
..
..

And that's it! You now have your skeleton frontend and backend in the same repo and auto deploying to now.sh and heroku whenever you push your code to the master branch!

Happy coding!

Top comments (1)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Now and Netlify have their CLI, so you can deploy without setting up a Git.

For Heroku, you need Git, but you can hack with Branches / Subtree. My solutions are in patarapolw/qsearch/web/heroku, and also web/backend/webpack.config.js, web/frontend/vue.config.js. I might write up on How-To someday.

I also have Now.sh with serverless solution, here, and here. Indeed, the problem is now.sh trying to be too smart.