CI/CD pipeline with GitHub Actions
In this blog we are going to learn step-by-step how to setup a Serverless CI/CD pipeline on Nimbella using GitHub Actions.
- What is CI/CD?
- CI/CD with GitHub Actions on Nimbella
- Build the App
- Write an Automated Test for the App
- Set-up CI/CD using GitHub Action
- In Brief and to Sum-up
What is CI/CD?
Continuous Integration refers to the practice of creating a build as new code is committed and running automated tests against that build to verify that everything works correctly.
Github-ci-cd: https://docs.github.com/en/actions/guides/about-continuous-integration
Continuous Delivery is the practice of automatically deploying code to production.
CI/CD with GitHub Actions on Nimbella
GitHub Actions let you build, test, and deploy your code right from GitHub.
Nimbella is a modern serverless platform in which CI/CD can be implemented with modern CI/CD Pipeline GitHub actions that results in saving developers time and effort.
We can implement CI/CD pipeline on Nimbella using GitHub Action with the below steps.
- Build the App.
- Write an Automated Test for the App.
- Set-up CI/CD using GitHub Action.
- Build the App
Please consider that we are going to build a real-time URL shortener app. We can create our shortener app having Node.js as backend and React.js as front end which can be deployed into Nimbella serverless Platform as mentioned in the article below.
How to deploy Node.js functions on Nimbella.
We may set up a free mongo DB cluster as Database for our app by referring to get Started with Atlas.
Please note that to run this application, you’ll need to have Git, Node.js and NPM already installed.
Let’s clone the code into our local directory using the below command.
git clone https://github.com/bolt-hub/github-ci-cd-nimbella-shortener.git
The directory having the code for our shortener app looks as below.
2.Write an Automated Test for the App
Automated testing enables continuous testing and ensures that bugs get found early and are fixed before the end users experience any breaks.
Please consider that we are going to write our automated test with popular Jest Framework. Let’s execute below commands from the root of the project directory to install npm packages for Jest framework.
npm init -ynpm install --save-dev jest mongoose nanoid dotenv
Create Jest configuration files i.e, jest.config.js & jest.setup.js and change the test script defined in package.json to run the Jest binary. Now when we run the test script, this Jest command is going to run.
After updating the Jest test run command in package.json it looks as below.
Create a test file that validates test cases for the app using Jest Framework as below.
const { Mongoose } = require("mongoose");const app = require("./index");const mongoose = require('mongoose')describe("Validates Response when URL Shortener is Requested", () => {test("Validates Response URL shortener for Positive flow", async () => {var requestBody = {actual_url:"https://nimbella.com/blog/how-to-deploy-node-js-functions-on-nimbella",};var appResponse = await app.main(requestBody);expect(appResponse.body.success).toBe(true);expect(appResponse.statusCode).toBe(200);});});afterAll(async done => {// Closing the DB connection allows Jest to exit successfully.mongoose.connection.close();done();});
After writing test validation for our serverless action, directory will look as below:
3.Set-up CI/CD using GitHub Action
Create a new Repository in GitHub. Navigate to Actions tab on this repository and set up a basic workflow.
Let’s clone our new repo which has default GitHub actions and move the code for Shortener into this.
We can get a non-expiring token to deploy our app into nimbella by running below command on nim CLI.
nim auth export --non-expiring
Copy the token generated from the above command and add it as a GitHub secret. Also, Add the environment variables of serverless action as GitHub secrets as below.
The final step of configuring CI/CD pipeline on Nimbella using GitHub Action, is to modify the YAML file under ./GitHub/workflows as per our deployment specifications as below.
The final step of configuring CI/CD pipeline on Nimbella using GitHub Action, is to modify the YAML file under ./GitHub/workflows as per our deployment specifications as below.
Once the above configuration is specified, whenever code changes are pushed to the repository, GitHub Action will trigger the configured CI/CD Pipeline/workflow to achieve CI/CD.
Let’s test the CI/CD workflow by pushing the code to the repository. After pushing the code changes, if we check-out the “Actions” tab of the repository , it will look as below which indicates that CI/CD workflow got triggered.
In Brief and to Sum-up:
- Build your code as per Nimbella’s serverless framework structure
- Create Automated Test to Validate Serverless action
- Create a GitHub Repo and Navigate to Actions tab of the Repo and set up a sample GitHub workflow.
- Configure Nimbella’s Auth token & environment variables under GitHub Secrets of the same Repo.
- Modify the GitHub action’s YAML file to test deploy the app using nim CLI
- Push/Pull(as mentioned in YAML file) the code to trigger the workflow.
GitHub Source: https://github.com/bolt-hub/github-ci-cd-nimbella-shortener.git
Please feel free to experiment CI/CD workflow on Nimbella using the above example with below few steps.
- Create a Nimbella Account and install nim CLI .
- 2.Create a new Github Repo and configure the environment variables & Nimbella’s Auth token as GitHub secrets on the same Repo.
3.Clone the above code from Git and push to your newly created Repo. Once the workflow run has completed, you may check-out your live URL shortener as below.
Guest Author: Athithan Raj P.
Follow him on Twitter: @raj_athithan