Introduction to CI/CD Pipelines in React Native

Aexomir
4 min readDec 20, 2023

--

Using CI/CD pipelines in RN provides a streamlined and efficient workflow specially for teams. By automating key processes such as code integration, testing, and deployment, pipelines ensure that code changes are thoroughly validated before reaching the shared repository. This not only enhances collaboration among developers but also accelerates the development cycle through quick feedback loops and automated builds.

Key Benefits of having CI/CD Pipelines:

  1. Automation: Pipelines automate repetitive tasks such as code compilation, testing, and deployment. This reduces the risk of human error and accelerates the development process.
  2. Code Quality: Continuous Integration facilitates the integration of code changes into a shared repository, triggering automated builds and tests. This ensures that new code additions do not break existing functionality, maintaining overall code quality.
  3. Faster Feedback: Developers receive rapid feedback on the success or failure of their code changes. Early detection of issues allows for quicker resolution, promoting a faster development cycle.
  4. Consistent Deployments: Continuous Deployment streamlines the process of releasing updates to various environments, ensuring consistency between development, testing, and production environments.
  5. Collaboration: CI/CD pipelines encourage collaboration among development teams. Developers can confidently make changes, knowing that automated tests and builds validate their contributions.

so now that we know it’s a MUST, how to implement it? :)

Step 1: Set Up Version Control

Before diving into pipelines, ensure your React Native project is under version control. Initialize a Git repository if you haven’t already:

git init

Create a .gitignore file to exclude unnecessary files from version control. You can find React Native-specific .gitignore templates online.

Step 2: Choose a CI/CD Service

Select a Continuous Integration/Continuous Deployment (CI/CD) service to automate the build and deployment process. Popular choices include GitHub Actions, GitLab CI, and Travis CI.

Step 3: Configure CI Pipeline

Based on which service you decide to use, syntax might vary! but main use-cases include:

  • lint
  • deploy
  • sign
  • scan
  • notify

While the overall structure and concepts might be similar, the details, syntax, and available features can differ. It’s essential to refer to the documentation of the specific CI/CD service you are using to understand and correctly implement the configuration for your pipelines.

Github Actions (.github/workflows/main.yml)

name: React Native CI/CD

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: '14'

- name: Install Dependencies
run: npm install

- name: Lint
run: npx eslint .

- name: Deploy to CodePush
env:
APP_NAME: 'YourAppName'
CODEPUSH_ACCESS_KEY: ${{ secrets.CODEPUSH_ACCESS_KEY }}
run: npx code-push release-react $APP_NAME android -d Production -k $CODEPUSH_ACCESS_KEY

- name: Notify Slack
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
if [ ${{ job.status }} == 'success' ]; then
curl -X POST -H 'Content-type: application/json' --data '{"text":"✅ CI/CD Pipeline succeeded for React Native project."}' $SLACK_WEBHOOK_URL
else
curl -X POST -H 'Content-type: application/json' --data '{"text":"❌ CI/CD Pipeline failed for React Native project."}' $SLACK_WEBHOOK_URL
fi

Gitlab CI (.gitlab-ci.yml)

stages:
- lint
- deploy
- notify

variables:
APP_NAME: 'YourAppName'

lint:
stage: lint
script:
- npm install -g eslint
- eslint .

deploy:
stage: deploy
script:
- npm install -g code-push-cli
- code-push release-react $APP_NAME android -d Production -k $CODEPUSH_ACCESS_KEY

notify:
stage: notify
script:
- apt-get update -qy
- apt-get install -y curl
- |
if [ "$CI_COMMIT_RESULT" == "success" ]; then
curl -X POST -H 'Content-type: application/json' --data '{"text":"✅ CI/CD Pipeline succeeded for React Native project."}' $SLACK_WEBHOOK_URL
else
curl -X POST -H 'Content-type: application/json' --data '{"text":"❌ CI/CD Pipeline failed for React Native project."}' $SLACK_WEBHOOK_URL
fi
only:
- main

Travis CI (.travis.yml)

language: node_js
node_js:
- '14'

jobs:ym
include:
- stage: lint
script: npm install -g eslint && eslint .

- stage: deploy
script:
- npm install -g code-push-cli
- code-push release-react YourAppName android -d Production -k $CODEPUSH_ACCESS_KEY

- stage: notify
script:
- |
if [ "$TRAVIS_TEST_RESULT" == "0" ]; then
curl -X POST -H 'Content-type: application/json' --data '{"text":"✅ CI/CD Pipeline succeeded for React Native project."}' $SLACK_WEBHOOK_URL
else
curl -X POST -H 'Content-type: application/json' --data '{"text":"❌ CI/CD Pipeline failed for React Native project."}' $SLACK_WEBHOOK_URL
fi

branches:
only:
- main

Safely manage environment-specific configurations using environment variables. Define environment variables in your CI/CD service and access them in your React Native app.

Step 4: Test the Pipeline & Configure Workers

Before relying on the pipeline, thoroughly test it with various scenarios, including feature branches, pull requests, and different environments. after that, by pushing it into your repository, you can configure the pipeline on your remote repository.

After configuring pipeline on your repository, you’d need to setup workers which is mainly done by DevOps.

Conclusion

In conclusion, implementing CI/CD pipelines in React Native brings a seamless and efficient development workflow. Embracing CI/CD not only streamlines the development process but also contributes to a more agile, reliable, and collaborative development environment.

I’m just starting to write write-ups. I’d very much appreciate your feedbacks and comments on how to make it super productive. :) 🚀

--

--

Aexomir
Aexomir

Written by Aexomir

Mobile Application Developer for Life / Bug Hunter for Fun

No responses yet