<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Matthew Burfield</title>
    <description>The latest articles on Forem by Matthew Burfield (@matthewburfield).</description>
    <link>https://forem.com/matthewburfield</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F334891%2F185908a3-a4d6-426c-b517-18d750896fc5.jpeg</url>
      <title>Forem: Matthew Burfield</title>
      <link>https://forem.com/matthewburfield</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/matthewburfield"/>
    <language>en</language>
    <item>
      <title>Auto deployment to now.sh and Heroku from Github</title>
      <dc:creator>Matthew Burfield</dc:creator>
      <pubDate>Thu, 13 Feb 2020 03:38:31 +0000</pubDate>
      <link>https://forem.com/chingu/auto-deployment-to-now-sh-and-heroku-from-github-2p8</link>
      <guid>https://forem.com/chingu/auto-deployment-to-now-sh-and-heroku-from-github-2p8</guid>
      <description>&lt;p&gt;In Voyage 16, my team wanted to create a React app using the &lt;a href="https://nextjs.org/"&gt;next.js&lt;/a&gt; framework, and a node server using &lt;a href="https://expressjs.com/"&gt;express&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is how we wanted to setup our project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have both frontend and backend in the same Github repo&lt;/li&gt;
&lt;li&gt;Have our next.js react app automatically deploy to now.sh on merge to the master branch&lt;/li&gt;
&lt;li&gt;Have our express server automatically deploy to Heroku on merge to the master branch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I managed to get this working, but ran into a number of challenges along the way so I decided to document the process.&lt;/p&gt;

&lt;p&gt;First clone your repo so you have it locally. You should have just a README.md in the directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;a href="https://nextjs.org/blog/create-next-app"&gt;create-next-app&lt;/a&gt; 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 &lt;code&gt;package.json&lt;/code&gt; file after creation.&lt;/li&gt;
&lt;li&gt;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
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hhuxTCG---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/er7yp337wozuc1ym97k5.png" alt="now.sh menu image"&gt;
&lt;/li&gt;
&lt;li&gt;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.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HVzo3zlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t9cwf40dn2zli701oq6v.gif" alt="finding the invite link for a team"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;While you're waiting for the Github organization to be linked, you can deploy your project to now.sh.&lt;br&gt;&lt;br&gt;
With the &lt;a href="https://zeit.co/download/"&gt;now.sh CLI&lt;/a&gt;; In your repo's root directory, run:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; now frontend
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;And answer the following questions:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Which scope do you want to deploy to?: (choose the team you just setup)
&amp;gt; Link to existing project [y/N]: N
&amp;gt; What's your project's name?: (type in the project name you want)
&amp;gt; In which directory is your code located?: frontend/
..
..
&amp;gt; Want to override the settings? [y/N]: N
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now that your project has been deployed, you should be able to see it in now.sh under your team name's dashboard.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QJfpRlnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pyc96hnwckj97ppl9kuq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QJfpRlnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pyc96hnwckj97ppl9kuq.png" alt="linked Github integration"&gt;&lt;/a&gt;&lt;br&gt;
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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s7gupk_Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ncsve0b68oms0asflxf0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s7gupk_Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ncsve0b68oms0asflxf0.gif" alt="Linking repo for auto-deployment"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Congratulations! Your frontend is now linked to now.sh and will auto deploy whenever you push code to your master branch!&lt;/p&gt;

&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a new folder in your repo's root directory called "backend".&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the backend folder, create an &lt;a href="https://expressjs.com/en/starter/hello-world.html"&gt;express server&lt;/a&gt;&lt;br&gt;
Note: make sure you aren't assigning a port value directly, do this instead.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Setup the &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;Heroku CLI&lt;/a&gt; if you haven't done so already. Then login with the command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; heroku login
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make sure you have committed your express app to git.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; heroku apps:create project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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 &lt;code&gt;git subtree&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; git subtree push --prefix backend heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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 &lt;code&gt;heroku logs --tail&lt;/code&gt; to debug!).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In your Heroku dashboard, go to your project's settings and add a config variable of PROJECT_PATH and set it to backend.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4p440tXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tvfj172i9txkh0m32juh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4p440tXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tvfj172i9txkh0m32juh.png" alt="Heroku config var"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also in settings, add a build pack and enter this url: &lt;code&gt;https://github.com/timanovsky/subdir-heroku-buildpack.git&lt;/code&gt; Don't worry about selecting a supported buildpack, just add the url and click save changes.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ron1hW6d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u9y5jlrfgub14oev4mcc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ron1hW6d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u9y5jlrfgub14oev4mcc.png" alt="Adding Heroku buildpack"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the drag handles, drag the buildpack you just created, to above the &lt;code&gt;heroku/nodejs&lt;/code&gt; buildpack that was already there.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dGvczYdV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nl4alnhc268foz0x06fg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dGvczYdV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nl4alnhc268foz0x06fg.png" alt="Reordered buildpacks"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, on the deploy tab. Select your repo from the integrations dashboard, and then click the "Enable Automatic Deploys" button!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i3r_3lcw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xjntemukm9nzifxdxwzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3r_3lcw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xjntemukm9nzifxdxwzt.png" alt="Heroku integration step"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r86Na_mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mumng9wxww8yot4z3l4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r86Na_mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mumng9wxww8yot4z3l4e.png" alt="Heroku enable auto deploy"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;..&lt;br&gt;
..&lt;br&gt;
..&lt;br&gt;
..&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>ci</category>
      <category>todayilearned</category>
      <category>newproject</category>
    </item>
  </channel>
</rss>
