Skip to content

Latest commit

 

History

History
executable file
·
114 lines (66 loc) · 4.18 KB

02-04 api.md

File metadata and controls

executable file
·
114 lines (66 loc) · 4.18 KB
sectionid sectionclass title parent-id
ratingsapi
h2
Deploy Ratings API
lab-ratingapp

The rating-api is a NodeJS application that connects to mongoDB to retrieve and rate items. Below are some of the details that you'll need to deploy this.

Fork the application to your own GitHub repository

To be able to setup CI/CD webhooks, you'll need to fork the application into your personal GitHub repository.

Fork

Use the OpenShift CLI to deploy the rating-api

Note You're going to be using source-to-image (S2I) as a build strategy.

{% collapsible %}

oc new-app https://github.com/<your GitHub username>/rating-api --strategy=source

Create rating-api using oc cli

{% endcollapsible %}

Configure the required environment variables

{% collapsible %}

Create the MONGODB_URI environment variable. This URI should look like mongodb://[username]:[password]@[endpoint]:27017/ratingsdb. You'll need to replace the [usernaame] and [password] with the ones you used when creating the database. You'll also need to replace the [endpoint] with the hostname acquired in the previous step

Hit Save when done.

Create a MONGODB_URI environment variable

{% endcollapsible %}

Verify that the service is running

{% collapsible %}

If you navigate to the logs of the rating-api deployment, you should see a log message confirming the code can successfully connect to the mongoDB.

Verify mongoDB connection

{% endcollapsible %}

Retrieve rating-api service hostname

{% collapsible %}

Find the rating-api service.

oc get svc rating-api

The service will be accessible at the following DNS name over port 8080: rating-api.workshop.svc.cluster.local:8080 which is formed of [service name].[project name].svc.cluster.local. This resolves only within the cluster.

{% endcollapsible %}

Setup GitHub webhook

To trigger S2I builds when you push code into your GitHib repo, you'll need to setup the GitHub webhook.

{% collapsible %}

Retrieve the GitHub webhook trigger secret. You'll need use this secret in the GitHub webhook URL.

oc get bc/rating-api -o=jsonpath='{.spec.triggers..github.secret}'

You'll get back something similar to the below. Make note the secret key in the red box as you'll need it in a few steps.

Rating API GitHub trigger secret

Retrieve the GitHub webhook trigger URL from the build configuration.

oc describe bc/rating-api

Rating API GitHub trigger url

Replace the <secret> placeholder with the secret you retrieved in the previous step to have a URL similar to https://openshift.9729df58f18c47bab789.eastus.azmosa.io:443/apis/build.openshift.io/v1/namespaces/workshop/buildconfigs/rating-api/webhooks/1inS0TVIN-Zw92xxtIXr/github. You'll use this URL to setup the webhook on your GitHub repository.

In your GitHub repository, select Add Webhook from SettingsWebhooks.

Paste the URL output (similar to above) into the Payload URL field.

Change the Content Type from GitHub’s default application/x-www-form-urlencoded to application/json.

Click Add webhook.

GitHub add webhook

You should see a message from GitHub stating that your webhook was successfully configured.

Now, whenever you push a change to your GitHub repository, a new build will automatically start, and upon a successful build a new deployment will start.

{% endcollapsible %}

Resources