GitLab Advanced CI/CD - Hands-On Lab: Deployment Strategies
The next step in your development process is to determine an appropriate deployment strategy for your application. Rolling out changes to all users at once is a risky strategy, since any errors will impact all of your users and potentially cause outages. To mitigate this, you can take advantage of GitLab’ deployment features. In this section, you will learn how to implement a feature flag in your application to allow for a gradual rollout of features.
Estimate time to complete: 15 minutes
Objectives
By the end of this lab, you will be able to:
- Use GitLab’s feature flag functionality
Task A. Implement a Feature Flag
In this task, you will implement a feature flag in your application using GitLab’s feature flag functionality. This will allow you to gradually roll out new features to a subset of users, reducing the risk of widespread issues if problems arise.
Follow these steps to set up and use a feature flag:
-
Navigate to Deploy > Feature flags.
-
Select New Feature Flag.
-
For the name, input
test
. For the Type, selectPercent rollout
. -
Set the percentage to 50%, and set the Based on select to Random.
-
Select Create feature flag.
-
After creating the feature flag, select Configure. Take note of the API URL and Instance ID fields. You will need these for your code changes.
-
Select your
index.js
file. -
Select Edit > Edit in single file.
-
In your
index.js
file, remove all of your existing code and replace it with the following:const { initialize } = require('unleash-client'); const unleash = initialize({ url: 'your-instance-url', appName:'production', instanceId: 'your-instance-id' }); setInterval(() => { if (unleash.isEnabled('test')) { console.log('Toggle enabled'); } else { console.log('Toggle disabled'); } }, 1000);
-
Select Commit changes.
This code will continually run, attempting to check if the feature flag toggle is enabled or disabled. Since it is enabled for 50% of users, you should see it around half the time when this runs.
-
To test running this, we will test running the script.
-
Open you
.gitlab-ci.yml
file. -
Select Edit > Edit in pipeline editor.
-
Replace your whole
.gitlab-ci.yml
file with the following code:default: image: node:latest stages: - test test_flag: stage: test script: - npm i unleash-client - node index.js
-
Select Commit changes.
-
After committing your changes, in the left sidebar, select Build > Pipelines.
-
Select the
test_flag
job. -
Observe that sometimes this job outputs true and sometimes it outputs false. This is because the feature flag is active 50% of the time.
Lab Guide Complete
You have completed this lab exercise. You can view the other lab guides for this course.
Suggestions?
If you wish to make a change to the Hands-On Guide for GitLab CI/CD, please submit your changes via Merge Request.
6f6d0996
)