<?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>DEV Community: Jan Szczepanski</title>
    <description>The latest articles on DEV Community by Jan Szczepanski (@janszczepanski_).</description>
    <link>https://dev.to/janszczepanski_</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%2F457456%2F1a7eb058-21bb-40d4-bf85-9014757a5154.jpg</url>
      <title>DEV Community: Jan Szczepanski</title>
      <link>https://dev.to/janszczepanski_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/janszczepanski_"/>
    <language>en</language>
    <item>
      <title>Deploy a React App to Heroku with Bitbucket Pipelines</title>
      <dc:creator>Jan Szczepanski</dc:creator>
      <pubDate>Mon, 24 Aug 2020 19:44:58 +0000</pubDate>
      <link>https://dev.to/janszczepanski_/deploy-a-react-app-to-heroku-with-bitbucket-pipelines-3876</link>
      <guid>https://dev.to/janszczepanski_/deploy-a-react-app-to-heroku-with-bitbucket-pipelines-3876</guid>
      <description>&lt;h1&gt;
  
  
  Deploy a React App to Heroku with Bitbucket pipelines
&lt;/h1&gt;

&lt;p&gt;In preparation of a talk for an &lt;a href="https://ace.atlassian.com/hamburg/"&gt;Atlassian Community Event&lt;/a&gt; in Hamburg I struggled and stumbled upon some documentations and tutorials for using Bitbucket and Heroku together. So I’ve decided to write my own first developer article about&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Building a basic CI/CD pipeline for deploying a &lt;strong&gt;React App&lt;/strong&gt; to &lt;strong&gt;Heroku&lt;/strong&gt; using &lt;strong&gt;Bitbucket&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1 — Create React app
&lt;/h3&gt;

&lt;p&gt;In this tutorial we will focus on deploying a React app to Heroku, so we will just use the React starter app. To run the App in Heroku we’ll add a basic express server setup later on.&lt;/p&gt;

&lt;p&gt;Prerequisite for creating and running a React app make sure you have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nodejs.org/en/"&gt;Node.js&lt;/a&gt; and &lt;a href="https://www.npmjs.com/"&gt;npm&lt;/a&gt; installed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; installed (optional)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now with that basic setup we are ready to create our React app:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app name-of-your-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Depending on your development environment grap yourself a coffee, the next 2–5 minutes are not that interessting, except you like watching how all the project dependencies are downloaded and bundled together.&lt;/p&gt;

&lt;p&gt;After you are back, type the following lines into your terminal to start the app&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd name-of-your-app
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can now view the app in a browser by open the URL &lt;a href="https://localhost:3000"&gt;http://localhost:3000&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2 — Create Bitbucket Repository
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KJTG_Ub7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AAcZj0XMWhsJQhbhGYJwaXg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KJTG_Ub7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AAcZj0XMWhsJQhbhGYJwaXg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our app up and running on our local machine we should create our git repository. Therefore we head over to &lt;a href="https://bitbucket.org-."&gt;https://bitbucket.org&lt;/a&gt;. If you don’t have an account yet, get one its for free (important nowadays)!&lt;/p&gt;

&lt;p&gt;When creating a new repository you need to at least give it a name and you want to set the access level to private. Most of the settings here can be changed afterwards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y0df4Pfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2352/1%2AtOBzFBAwR7MxPyihGo_xyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y0df4Pfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2352/1%2AtOBzFBAwR7MxPyihGo_xyg.png" alt="Create dialog at bitbucket"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well done! You’ve created your — maybe first — git repository. Time to explore the menu which you’ll now see on the lefthand side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Egat17h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2136/1%2APvvny02RIdvfu0vfnrf3Qw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Egat17h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2136/1%2APvvny02RIdvfu0vfnrf3Qw.gif" alt="Menu of your bitbucket repository"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to know more about Bitbucket head over to the &lt;a href="https://confluence.atlassian.com/bitbucket/bitbucket-cloud-documentation-221448814.html"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3 — Create Heroku App
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AzBry9l---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2Ao92hHoWIi64nX7XYaN1CwQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AzBry9l---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2Ao92hHoWIi64nX7XYaN1CwQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are pretty close to deploying our app now. Head over to &lt;a href="https://heroku.com"&gt;https://heroku.com&lt;/a&gt; - again create an account if you don’t have one (IT’S FREE!).&lt;/p&gt;

&lt;p&gt;On your dashboard click &lt;strong&gt;New&lt;/strong&gt; and choose &lt;strong&gt;Create new app&lt;/strong&gt;. In the dialog&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Set a &lt;strong&gt;App Name&lt;/strong&gt; - this one is unique world-wide, so it might happen that some else already picked &lt;strong&gt;test123&lt;/strong&gt; or &lt;strong&gt;demoapp&lt;/strong&gt; - be creative!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the &lt;strong&gt;Region&lt;/strong&gt; where your app should run&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ocYCunRd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A8FYmS3Ci_hLJJPcEG9nyLA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ocYCunRd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A8FYmS3Ci_hLJJPcEG9nyLA.png" alt="Create dialog at heroku"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your app is now created, but before you head back to your code and git repository we need to tell our app which &lt;a href="https://devcenter.heroku.com/articles/buildpacks"&gt;Buildpack&lt;/a&gt; we’d like to use.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to &lt;strong&gt;Settings&lt;/strong&gt; in your App&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under &lt;strong&gt;Buildpacks&lt;/strong&gt; click &lt;strong&gt;Add buildback&lt;/strong&gt; and choose &lt;a href="https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nodejs"&gt;**heroku/nodejs&lt;/a&gt;**&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BPzzput---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2456/1%2AhP_1134FuZXN-axt6qKYHw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BPzzput---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2456/1%2AhP_1134FuZXN-axt6qKYHw.png" alt="Buildpacks configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For deploying the app later on we also need an API Key.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your Profile go to &lt;a href="https://dashboard.heroku.com/account"&gt;Account Settings&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Section &lt;strong&gt;API Key&lt;/strong&gt; click &lt;strong&gt;Reveal&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Safe the key for later configuration&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Deploy with Bitbucket pipelines
&lt;/h2&gt;

&lt;p&gt;Now comes the fun part… for you actually, I learned the hard way so you don’t have to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 — Setup an express Server
&lt;/h3&gt;

&lt;p&gt;Go to Visual Studio Code and add a new folder called &lt;strong&gt;server&lt;/strong&gt; to the root of the repository. Within that folder create a file called &lt;strong&gt;server.js&lt;/strong&gt; and install express by running this command in your terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Within the &lt;strong&gt;server.js&lt;/strong&gt; file copy &amp;amp; paste these lines of code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, '..', 'build');

const port = process.env.PORT || 3000;

app.use(express.static(publicPath));

app.get('*', (req, res) =&amp;gt; {
    res.sendFile(path.join(publicPath, 'index.html'));
});

app.listen(port, () =&amp;gt; {
    console.log('Hello World I run on PORT ' + port);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1C0sfGpf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACknmspYBmkoCp1eWVvOuUw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1C0sfGpf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACknmspYBmkoCp1eWVvOuUw.png" alt="Capture of my vscode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your &lt;strong&gt;package.json&lt;/strong&gt; file change the script line for start your app to:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "node server/server.js",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h3&gt;
  
  
  Step 2 — Upload everything to your git repository
&lt;/h3&gt;

&lt;p&gt;Open a terminal windows make sure you are in the root directory of your React app and type the following lines:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
git add .
git commit -m "initial commit"
git remote add origin [https://username@bitbucket.org/username/name-of-your-repository.git](https://username@bitbucket.org/username/name-of-your-repository.git)
git push -u origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When you refresh the browser page you should see all the files and folders of your app we’ve just commited the the git repository.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3 — Setup and configure Bitbucket pipelines
&lt;/h3&gt;

&lt;p&gt;Now that our code is in our repository we need to enable Pipelines for our repository.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to Repository Settings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under the Section &lt;strong&gt;Pipelines&lt;/strong&gt; click &lt;strong&gt;Settings&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enable Pipelines&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Right above Settings, go to &lt;strong&gt;Repository variables&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a variable with name HEROKU_API_KEY and your key from above&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a variable with name HEROKU_APP_NAME and the app name as value&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we can finally create our Pipeline configuration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oPVrRkP0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Adkz6Vib9eZHH9jJI6qAJXg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oPVrRkP0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Adkz6Vib9eZHH9jJI6qAJXg.gif" alt="[https://i.imgur.com/UhGt0uR.gif](https://i.imgur.com/UhGt0uR.gif)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case we want (not true, the author wants it!) to build a pipeline with following rules and steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We don’t have branches yet, so everytime someone commits something to the master branch we want to run our pipeline&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We want to test our app first&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to create a Zip file for the heroku pipline add-on&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We want to finally push our repository to Heroku&lt;/p&gt;

&lt;blockquote&gt;
&lt;h1&gt;
  
  
  Author’s note: YES, I could push the repository to Heroku using git, but NO that was not my intention for writing this article!
&lt;/h1&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;strong&gt;bitbucket-pipelines.yml&lt;/strong&gt; file holds all the build configurations for your repository. There is really a lot you can do with this file, so if you want to dig deeper take a look at the &lt;a href="https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html"&gt;official documentation&lt;/a&gt;. Here the basics of the &lt;a href="https://yaml.org/"&gt;YAML&lt;/a&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pipelines&lt;/strong&gt;: contains all your pipeline definitions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;default&lt;/strong&gt;: contains the steps that run on every push if one of the following criteria is not met.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;step&lt;/strong&gt;: each step starts a new Docker container with a clone of your repository, then runs the contents of your **script **section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;script&lt;/strong&gt;: a list of commands that are executed in sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pipe&lt;/strong&gt;: a pipe is more like an add-on or function, it takes some input and does stuff for you — like uploading your repository to Heroku&lt;/p&gt;

&lt;p&gt;Our final &lt;strong&gt;bitbucket-pipelines.yml&lt;/strong&gt; file looks like this:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image: node:10.15.3

&lt;p&gt;pipelines:&lt;br&gt;
  branches:&lt;br&gt;
    master:&lt;br&gt;
      - step:&lt;br&gt;
          name: Test App&lt;br&gt;
          caches:&lt;br&gt;
            - node&lt;br&gt;
          script:&lt;br&gt;
            - rm -rf package-lock.json&lt;br&gt;
            - rm -rf node_modules&lt;br&gt;
            - npm install&lt;br&gt;
            - npm run test&lt;br&gt;
       - step:&lt;br&gt;
           name: Create artifact&lt;br&gt;
           script: &lt;br&gt;
             - git archive --format=tar.gz master -o                   application.tar.gz &lt;br&gt;
         artifacts: &lt;br&gt;
           - application.tar.gz&lt;br&gt;
       - step:&lt;br&gt;
           name: Deploy to heroku&lt;br&gt;
           deployment: production&lt;br&gt;
           caches:&lt;br&gt;
             - node&lt;br&gt;
           script:&lt;br&gt;
             - pipe: atlassian/heroku-deploy:1.1.4&lt;br&gt;
               variables:&lt;br&gt;
                 HEROKU_API_KEY: $HEROKU_API_KEY&lt;br&gt;
                 HEROKU_APP_NAME: $HEROKU_APP_NAME&lt;br&gt;
                 ZIP_FILE: "application.tar.gz"&lt;br&gt;
                 WAIT: 'true'&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  The END&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Hope you liked it, even more I hope it helps you! Please let me know! :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>devops</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
