visit
Building a basic CI/CD pipeline for deploying a React App to Heroku using Bitbucket
npx create-react-app name-of-your-app
cd name-of-your-app
npm start
Well done! You’ve created your — maybe first — git repository. Time to
explore the menu which you’ll now see on the lefthand side.
On your dashboard click New and choose Create new app. In the dialog:
1. Set a App Name - this one is unique world-wide, so it might happen that some else already picked test123 or demoapp - be creative!
2. Choose the Region where your app should run
Go to Settings in your AppUnder Buildpacks click Add buildback and choose
2. In the Section API Key click Reveal
3. Safe the key for later configurationGo to Visual Studio Code and add a new folder called server to the root of the repository. Within that folder create a file called server.js and install express by running this command in your terminal:
npm install express --save
Within the server.js file copy & paste these lines of code:
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) => {
res.sendFile(path.join(publicPath, 'index.html'));
});app.listen(port, () => {
console.log('Hello World I run on PORT ' + port);
});
In your package.json file change the script line for start your app to:
"start": "node server/server.js",
git init
git add .
git commit -m "initial commit"
git remote add origin https://[email protected]/username/name-of-your-repository.git
git push -u origin master
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.
Go to Repository SettingsUnder the Section Pipelines click SettingsEnable PipelinesRight above Settings, go to Repository variablesAdd a variable with name HEROKU_API_KEY and your key from aboveAdd a variable with name HEROKU_APP_NAME and the app name as value
Now we can finally create our Pipeline configuration.Author’s note: YES, I could push the repository to Heroku using git, but NO that was not my intention for writing this article!
The bitbucket-pipelines.yml 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 . Here the basics of the file:
pipelines: contains all your pipeline definitions.
default: contains the steps that run on every push if one of the following criteria is not met.
step: each step starts a new Docker container with a clone of your repository, then runs the contents of your script section.
script: a list of commands that are executed in sequence.
pipe: 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
Our final bitbucket-pipelines.yml file looks like this:
image: node:10.15.3pipelines:
branches:
master:
- step:
name: Test App
caches:
- node
script:
- rm -rf package-lock.json
- rm -rf node_modules
- npm install
- npm run test
- step:
name: Create artifact
script:
- git archive --format=tar.gz master -o application.tar.gz
artifacts:
- application.tar.gz
- step:
name: Deploy to heroku
deployment: production
caches:
- node
script:
- pipe: atlassian/heroku-deploy:1.1.4
variables:
HEROKU_API_KEY: $HEROKU_API_KEY
HEROKU_APP_NAME: $HEROKU_APP_NAME
ZIP_FILE: "application.tar.gz"
WAIT: 'true'
Jan Szczepanski specializes in project and process management and has been an Atlassian specialist since the beginning of his professional
career. He was in charge of technical as well as non-technical teams in
different companies and was responsible for the optimization of business
processes. Today he uses his specialist knowledge, which he has
acquired over many years, in to improve communication and collaboration
structures.