{{announcement.body}}
{{announcement.title}}

ReactJS App on Oracle Kubernetes Engine with Travis CI and GitHub

DZone 's Guide to

ReactJS App on Oracle Kubernetes Engine with Travis CI and GitHub

In this article, we discuss how to deploy a React application on an Oracle Kubernetes Engine with Travic CI and GitHub.

· Web Dev Zone ·
Free Resource

Introduction

This article demonstrates the steps required to host a Node application with Oracle Kubernetes Engine (OKE) using Travis Continuous Integration Server. Travis CI is very tightly coupled with GitHub, so any public repository created by you will be visible automatically in Travis. 

This is a development/sample code demo only. You need to optimize the code (given in this article) or write extra code to make it production-quality. You can access the sample code used in this article from here: https://github.com/kumarsirish/sample-travis-oke.

Prerequisites

  1. You should have an account in Oracle Cloud Infrastructure (OCI) and should have access to Developer Services, like Oracle Container Registry (OCIR) and OKE. Get an account from https://www.oracle.com/index.html if you don't have one.
  2. You should have an OKE cluster configured and running (https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/oke-full/index.html).
  3. You should have OCI CLI installed (https://oracle.github.io/learning-library/oci-library/DevOps/OCI_CLI/OCI_CLI_HOL.html).
  4. You should have an account in GitHub. 
  5. You should have registered on Travis.
  6. JavaScript runtime environment Node.js is installed in your development environment (https://nodejs.org/en/).

Pipeline Overview

Any check-in in the GitHub project code will trigger an automated build in Travis, which will create the project image and push it to the OCIR. In the deployment step of the build, the image will be pulled from OCIR and deployment to OKE.

Pipeline workflow

Pipeline workflow

GitHub and Travis can be configured for the feature branch-based builds or master branch-based builds. Any branch when merged with master automatically triggers a build in Travis.

Bootstrap the ReactJS Project

Bootstrap ReactJS sample-travis-oke project.

Shell
 


x
 
1
$ npx create-react-app sample-travis-oke 
2
$ cd sample-travis-oke 
3
# Change src/App.js for any custom message
4
# Run optimized build
5
$ npm run build
6
$ npm install -g serve
7
# ReactJS Apps can be hosted by Nginx or lightweight "serve" Application. For this tutorial we will be using nginx server but to check the created application you can run "serve" command below to verify the application.
8
$ serve -s build


Containerize the Project

The project will be containerized, along with the following tools:

  • OCI CLI.
  • kubectl command.

Make sure that you have OCI tenancy's COMPARTMENT_ID value, and I am assuming that your development environment is behind the corporate firewall so set variables HTTP_PROXY and NPM_REGISTRY accordingly.

Below is a sample Dockerfile, which creates the application and also deploys it to the OKE.  
Create the Dockerfile in project directory root.

Dockerfile
 




xxxxxxxxxx
1
38


 
1
FROM oraclelinux:7-slim as builder
2
ARG NPM_REGISTRY
3
ARG HTTP_PROXY
4
ARG COMPARTMENT_ID
5
WORKDIR '/app'
6
 
          
7
COPY package.json .
8
# optional line below
9
COPY ol7_developer_nodejs8.repo /etc/yum.repos.d/
10
RUN echo proxy=${HTTP_PROXY} >>/etc/yum.conf
11
 
          
12
RUN yum -y update && \
13
    rm -rf /var/cache/yum && \
14
    yum -y install nodejs
15
 
          
16
RUN  yum -y install python3
17
 
          
18
RUN npm config set registry ${NPM_REGISTRY}
19
RUN npm install
20
COPY . .
21
RUN npm run build
22
 
          
23
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl \
24
        && chmod +x ./kubectl \
25
        && rm -rf /tmp/*
26
 
          
27
ENV PATH="/app:/bin:/root/bin:${PATH}"
28
ENV KUBECONFIG="/root/.kube/oci_config"
29
ENV LC_ALL="en_US.utf8"
30
ENV C ${COMPARTMENT_ID}
31
 
          
32
RUN curl -LO https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh \
33
        && chmod +x ./install.sh  \
34
        && ./install.sh --accept-all-defaults
35
 
          
36
EXPOSE 5000
37
ENTRYPOINT ["/bin/bash"]
38
CMD []



Verify that the image is getting built with the following command:

Shell
 




xxxxxxxxxx
1


 
1
#create the docker image
2
$ docker build --build-arg http_proxy=<proxy> --build-arg NPM_REGISTRY="https://registry.npmjs.org/" --build-arg COMPARTMENT_ID="$COMPARTMENT_ID" -t sample-travis-oke:travis  



Verify the Container locally using the below command:

Shell
 




xxxxxxxxxx
1


 
1
#Run the container
2
$ docker run -v "<local_oci_config_path>/.oci":"/root/.oci" -v "<local_kube_config_path>/.kube":"/root/.kube" -p 5000:3000 sample-travis-oke:travis /bin/serve -s build



The application should now be up and running. Access the application at port 3000 of localhost (http://localhost:3000)

Push to GitHub

This step is required to integrate the project with Travis CI. Create the project repository "sample-travis-oke" in GitHub and push the existing code to this repository.

Shell
 




xxxxxxxxxx
1


 
1
$ cd sample-travis-oke
2
$ git init
3
$ git remote add origin https://github.com/<your_account>/sample-travis-oke.git
4
$ git add .
5
$ git commit -m "First Commit"
6
$ git push origin master
7
 
          



Feel free to check out GitHub's information page for more details: https://help.github.com/en/github/importing-your-projects-to-github/adding-an-existing-project-to-github-using-the-command-line

Setup Travis CI

Lets set up Continuous Integration in Travis CI for this project. Here is what we require from Travis:

  1. Build to kick off automatically whenever code is merged-in into the "master" branch.
  2. Build should create the Docker image and push the Docker image to the OCI container registry (OCIR).
  3. Pull the image from OCIR and push to OCI OKE (Oracle Kubernetes Engine).

To do all these, first, you need to login with Travis CI with your GitHub account. This way, all your GitHub public repositories will automatically be visible on Travis. You can check the newly created sample-travis-oke repository from https://travis-ci.org/account/repositories. Click on "Sync account" and refresh the page if you do not see the repository. 

Once synced up with GitHub, a Webhook will automatically appear in the GitHub project like below:

Webhook in response to GitHub sync

Webhook in response to GitHub sync

Set up OCIR  in your tenancy if you haven't already done so. Refer to this link for more details: https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html.

In your project settings (e.g https://travis-ci.org/<user_name>/sample-travis-oke/settings), add OCIR_USER, OCIR_PASSWORD, and COMPARTMENT_ID as secret keys. Your Travis project setting screen should be similar to below:

Project settings page

  Project Setting Page  

Every Travis project requires a .travis.yml file in the root of the project. Currently, OKE deployment is not supported out-of-the box from Travis, so we need to write our own deployment steps. Make sure that OCI config and Kube config are part of the image. 

A sample Travis file for building the image and pushing to the OKE is given below:

YAML
 




xxxxxxxxxx
1
16


 
1
language: generic
2
sudo: required
3
services:
4
- docker
5
before_install:
6
- docker build --build-arg NPM_REGISTRY=${NPM_REGISTRY} --build-arg COMPARTMENT_ID=$C
7
  -t ${OCIR_URL}/sample-travis-oke:travis .
8
- docker login ${OCIR_URL} -p ${OCIR_PASSWORD} -u ${OCIR_USER}
9
- docker push ${OCIR_URL}/sample-travis-oke:travis
10
script:
11
- docker run ${OCIR_URL}/sample-travis-oke:travis -c /bin/serve -s build
12
deploy:
13
- kubectl cluster-info
14
- kubectl apply -f k8s
15
- kubectl get pods
16
- kubectl get services



Notice the kubectl commands used under deploy section. This section is different for different cloud providers. OCI config files and Kubernetes config file need to be protected. Travis provides a mechanism to encrypt such files and use it internally in build without making its content public. To do so, you need to install Travis CLI in your development environment and use it to encrypt the config files. Refer to https://docs.travis-ci.com/user/encrypting-files#Encrypting-multiple-files for more details.

Shell
 




xxxxxxxxxx
1


 
1
$ sudo gem install travis
2
$ tar cvf secrets.tar .oci/* .kube/*
3
$ travis encrypt-file secrets.tar --add



The --add option automatically updates the .travis.yml file. You can verify that keys used for encryption are already part of your project settings in Travis.

Checking environment variables

Checking environment variables

Configure OKE Deployment 

Create pod creation and service creation files for the project for OKE. These are just like any other Kubernetes files, and there is nothing OKE specific here. However, make sure that you have OCI config set up, your private key set up, or authentication token and Kubernetes config set up for OKE. Steps to do that are already given above. Sample Kubernetes files for this project will be like the following:

YAML
 




xxxxxxxxxx
1
25


 
1
apiVersion: v1
2
kind: Pod
3
metadata:
4
   name: app-oci-oke-pod
5
   labels:
6
      component: web
7
spec:
8
   containers:
9
      - name: app-oci-oke
10
        image: [OCIR_URL]/sample-travis-oke:travis
11
        command: ["/bin/serve -s build"]
12
        ports:
13
        - containerPort: 5000
14
------------------------------------
15
apiVersion: v1
16
kind: Service
17
metadata:
18
    name: oci-oke-app-service
19
spec:
20
    type: LoadBalancer
21
    selector:
22
       component: web
23
    ports:
24
       - port: 3000
25
         targetPort: 5000



Deploy on OKE

As part of the Deploy step of Travis build, the following steps are executed within the container:

Shell
 




xxxxxxxxxx
1


 
1
kubectl cluster-info
2
kubectl apply -f k8s
3
kubectl get pods
4
kubectl get services



Keep on giving the kubectl get services command until the external-IP is exposed:

Plain Text
 




xxxxxxxxxx
1


 
1
NAME                  TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)    AGE
2
oci-oke-app-service   LoadBalancer   1x.x.x4.x   1x.x.x.198   3000:31305/TCP   110m



Use the external IP to load the application.

React project running on OKE

React project running on OKE

Congrats! Your ReactJS application is now running on OKE deployed via Travis.

Topics:
ci/cd ,ci/cd pipeline ,devops ,docker ,git and github ,kubernetes ,reactjs ,travis ci

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}