Product
|
Cloud costs
|
released
November 8, 2023
|
3
min read
|

Trunk Based Development with Feature Flags

Updated

Hey all you wonderful developers out there! In this post I am going to take you through creating a feature branch and using the Harness React SDK to implement a flag in your project. This post is an introduction to getting a feature setup using Vite, React, and Sass to change the look of a logo in a navbar.

Project Setup

The world of web and software development changes every day. It is hard to keep up with, we all know that. The days of using Create React App seem to be over. In its place has stepped Vite. You can head over to the Vite site and see what it is all about, but if I had to put a description on it, it would be; CRA with superpowers. Extremely fast installs, and load times make it a great tool to start your project with. You can also choose what language you want to use when prompted in the terminal.

We are going to install Vite as the base setup for our project. Fire up your terminal and install Vite using this command:

npm create vite@latest

If you are using Yarn, use this command:

yarn create vite

You can manually install Vite with the framework of your choice or you can select the prompts in the terminal.

Great, now that we have that setup, go back to your terminal, change directories into my-project and fire up your editor of choice. If VS Code is in your PATH, you can type code . and that should open it. Open your terminal back up and run:

npm install
npm run dev

This will install all your dependencies and fire up your server which should be `localhost:5173`. The rest of this article is going to assume you have installed the Harness React SDK. If not, there will be a follow up post to this on installing and implementing that specific SDK. Stay tuned for that!

Component Wrapping


In this example, I created a very basic navbar using semantic markup and jsx. There is a logo that is left aligned, and a navigation that is right aligned using flexbox. Please keep in mind that this navigation I built is a desktop view only and is not using any type of routing. The “links” are list items within an unordered list. It is up to you the developer how you approach the routing for your component/application. YMMV.

The feature we want to ship changes the look of the logo. In the example we are only going to be wrapping the logo component in a flag, and making sure to put that in a feature branch so we can immediately keep it disabled/hidden, while merging it back into the main branch. We can always verify that the feature works by switching to our feature branch and toggling the feature on/off in the Harness app.

import { useFeatureFlag, useFeatureFlags } from "@harnessio/ff-react-client-sdk";

const Logo = () => {
 const isEnabled  = useFeatureFlag("awesomelogoheadergradient");
 const FeatureFlags = useFeatureFlags();
 console.log(FeatureFlags);
 if (isEnabled) {
   return (
     

       YourLogo      

   );  }  return

YourLogo

; }; export default Logo;

We are going to use CSS to control the feature. I am using Sass for this, you can use vanilla CSS. Create a feature branch according to your branch naming convention. I like to stick with lastname/whatever-the-feature-is.

Feature branch creation

Once your branch is created, add the CSS to change the look of the component and then add the class to the component itself.

.logo-title.gradient {
	background: linear-gradient(0deg, rgba(121,238,3,1) 0%, rgba(0,231,255,1) 65%);
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 5px 0;
}

Markup

YourLogo

You can validate this works on save if you flip the switch in the Harness UI and enable the flag. This post is going to assume you have the flag setup in the Harness UI already and can navigate the interface. If the feature is doing what you expect when you toggle the flag on, you have successfully created a feature branch with a feature flag. Now you can merge this into main and keep it disabled as to not release it until you are ready. You also have the option of continuing to work on said feature if you choose, just open another branch.

Showing the flag is enabled in the Harness UI

LGTM!

Everything works as expected, tests pass, the PR has been reviewed and approved, now let’s get this merged. Make sure you stick to your merging process. I personally like to squash and merge my branch but your mileage may vary. Now that we have our code with the feature in main and it is still hidden behind a flag, no one can see it until you flip the switch or someone in charge of the release flips the switch.

The great thing about using feature flags is that you can designate a target(s) in order to test the feature. This is especially useful when having someone QA the feature. Another great thing about using feature flags is separating deploy from release. The code can be deployed 100% to production and until the flag is toggled, it will stay hidden. Once you and your team are ready to release it to the world, flip the switch and voila, your feature is live!

Sign up now

Sign up for our free plan, start building and deploying with Harness, take your software delivery to the next level.

Get a demo

Sign up for a free 14 day trial and take your software development to the next level

Documentation

Learn intelligent software delivery at your own pace. Step-by-step tutorials, videos, and reference docs to help you deliver customer happiness.

Case studies

Learn intelligent software delivery at your own pace. Step-by-step tutorials, videos, and reference docs to help you deliver customer happiness.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback?
Share your thoughts by creating a new topic in the Harness community forum.

Sign up for our monthly newsletter

Subscribe to our newsletter to receive the latest Harness content in your inbox every month.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Feature Flags