Creating a CI/CD Pipeline With GitHub API and Actions (r)

Sep 30, 2023

GJHtwAWwSTnwTtHNwZYV

-sidebar-toc>

What are the benefits of using CD/CI?

One reason could be the fact that in a collaborative setting that has several developers involved within the same team automated deployments initiated due to changes made by one developer to the repository may cause instability and unexpected issues. In the absence of adequate validation and testing, every single change made to the code could cause disruption to the live site which can cause downtime and an unpleasant experience for customers.

This is why an CD/CI pipeline is essential. By implementing a carefully orchestrated CI/CD workflow, developers are in a position to ensure that changes to their code are tested and validation before going live on sites. There are numerous tools for the CI/CD implementation within software development. This tutorial will use one of these tools The GitHub Actions to guide us through this.

What is GitHub actions?

GitHub Actions is a efficient automated program offered by GitHub. It helps developers automate different workflows and processes for their software development efforts. It is compatible with GitHub repositories. This makes it a breeze to use.

Through GitHub Actions as well as API API it is possible to define specific workflows to fulfill the specifications of your application. It is possible to set up the CI pipeline to run tests for your app and then trigger the deployment .

Starting by implementing GitHub Actions

GitHub Actions operates on the concept of workflows. It's a set of automated tasks which are driven by certain conditions or are scheduled to be executed on a regular schedule. The events could include pull calls, codes pushes and also issues creation and many more. In the event that one of these occurs, GitHub Actions automatically runs the workflow associated with it, then executes an array of steps pre-defined.

Every step of the workflow follows a distinct method, like developing codes, testing and deployment or possibly making the required notifications. Let's create a workflow using three steps:

  1. Make sure you are using the correct syntax by using ESLint.
  2. Run tests
  3. Reinstall the application

Step 1: Set Up Your GitHub Repository

For you to begin using GitHub Actions it is necessary to have a GitHub account GitHub. GitHub repository.

It is possible to connect to the repository by accessing it through GitHub using the following link: Use this template > Begin with a fresh repository.

In this React application, unit tests are created for every component to verify the functionality of each component. ESLint is also able to help verify that the syntax is correct and to enforce code formatting. The CI pipeline blocks any release in the event that an pull request or merge code, or merge the repository fails testing of the process.

Step 2: Make an Workflow Document

Set up your workflow using the YAML file that is located in your repository's .github/workflows directory. The directory must be located at the root of your repository. The naming convention for workflow files is name-of-the-workflow.yml.

  1. In your repository, make an .github directory.
  2. Within of the .github directory, start a brand-new directory named workflows.
  3. Inside the workflows directory, create a new file with a name like build-test-deploy.yml.

Step 3: Create the Workflow for CI/CD.

Now that you have created the file for your workflow, design an appropriate workflow, which contains all the steps needed to confirm the syntax with ESLint. Test the workflow, and then finally, launch the software.

Create CI Event

When creating an CI pipeline, the initial step is to give the workflow a name and determine the trigger events to initiate the process. For this example there are two events that could include push or pull to the branch that is at the helm.

name, make test, and deploy it on the pushbranches "main" request for pullbranches "main"

If you prefer scheduling regular jobs (CRON jobs) for specific tasks, you can add them into your process. In particular, you might require setting up certain tasks like backups of databases, data cleaning and other regular maintenance task.

Here's an example of how to incorporate CRON tasks in the process.

on: # Existing triggers that trigger events like requests for pull or push. Set a schedule of CRON jobs that use"0 zero. * "0 zero. * *"

The daily routine around midnight (UTC time) because the schedule of cron scheduling is configured as this 1: 0.. The cron schedule is altered according to your specific needs. The schedule can be adjusted to your specific needs.

Another scenario is that you wish to establish the C/C process for every Monday morning, starting around 8 a.m. There is a way to make a CRON task with an event within the timetable which includes:

name Create, Test and deployon the following branches: pushbranches "main" pull_request:branches "main" branches "main"The workflow should be scheduled to start every Monday morning starting at 8:30 a.m. (UTC time) schedule:"cron: "0 8 * * 1" jobs:# Create jobs

The syntax used to create the schedule employed to generate the event of scheduling the event in the GitHub Actions workflows is based upon the UNIX string syntax known as cron. The syntax lets you specify specific intervals and times to allow your workflow to be automated. The syntax consists of five fields, each of which is diverse aspects of the scheduling. Every field can be separated by the space. The syntax used to define scheduling syntax is:

* * + Day in the week (0 seven) (Sunday until Saturday, in which 7 and 0 symbolize the Saturday)  +--- month (1 12) +----- Day (1 1 - 31) | +------- Hour (0 23) Minute (0 between 59 and)

Let's then take a glance at every field:

  • Minute (0 until the 59th minute): The minute in which the cron job will be initiated. In this case, 15 means that the workflow will be running in the 15th minute of the hour.
  • Time (0 23): The hour in which the cron task starts to start running. In this instance, 8 means the workflow begins around 8 a.m.
  • A day in the calendar month (1 1 until 31): The day of the month when the cron task becomes running. For example, 1 means that the workflow is started at the beginning of the month.
  • Month (1 12): The month that the cron job will be activated. In this case, 6 means that the workflow has been planned to be activated in June.
  • A day in the week (0 7, 7): The day that falls on a Monday during the time when the cron task begins to trigger. This is because zero 7 and Zero 7 occur on Sundays. 1 indicates Monday, and so further. For instance, 4 means that the procedure will start at the time of Thursday.

Characters with special characteristics:

  • * (asterisk): is a match for any value that appears in the field. In this example, * in the field for minute means that the workflow is active each minute.
  • */n (slash): It defines an interval. In this instance, */5 in the minute field signifies that the workflow will trigger every five minutes.
  • , (comma): Specifies multiple particular values. In this case, 1,15,30 in the field for minutes indicates that the workflow starts on the 1st, 15th and 30th minutes of the hour.
  • (hyphen) (hyphen) (hyphen) (hyphen) The"hyphen" signifies a particular number of values. In this particular case, 1-5 in the weekday field means that the workflow starts between Monday through Friday (1 from 1 to 5).
  • ? (question mark) It's utilized to indicate the absence of a specific value. This is typically used to identify the day of the week in which the day of the month is mentioned. In this case, ? in the field that is for what day it is, as well being 15 for the month's day field, means it will be active on the 15th day of the month, regardless of the date of the week.

Create an CI Job to Check Syntax By Using ESLint

In order to begin the CI process, we'll make the assignments necessary or tasks. Each task should be given the clearest and most understandable title. For instance, let's say that the task is the eslint job because it requires reviewing the syntax of codes using ESLint.

jobs: ESLint name to test Syntax by using ESLint runs-on. Ubuntu's latest strategy: matrix Node-version [18.x, 20.x]

Next, identify the procedures for which are required for the "ESLint" job is expected to execute. This involves checking your code to verify that you have the correct Node.js version for ESLint as well as cache NPM packages and setting up dependencies for your project before finally using ESLint to determine how your code has been structured.

 steps: - name: Checkout code uses: actions/checkout@v3 - name: Use Node.js $ matrix.node-version to Check Lint uses: actions/setup-node@v3 with: node-version: $ matrix.node-version cache: 'npm' - name: Install Dependencies run: npm ci - name: Run ESLint run: npm run lint

The process described in the above example has each step is given the description and a number, which makes it simple to identify the source of the bug or issue by examining the workflow with GitHub Actions. Particularly, when we are in the third step, we use the the npm the ci command to install dependencies. This is preferable to the npm install due to the fact that it facilitates an easy installation. Furthermore, the third procedure, performing ESLint by using the npm run lint command is dependent on having added this command to your package.json file.

Here is the complete task of examining syntax with ESLint.

jobs: eslint: name: Check Syntax with ESLint runs-on: ubuntu-latest strategy: matrix: node-version: [18.x, 20.x] steps: - name: Checkout code uses: actions/checkout@v3 - name: Use Node.js $ matrix.node-version to Check Lint uses: actions/setup-node@v3 with: node-version: $ matrix.node-version cache: 'npm' - name: Install Dependencies run: npm ci - name: Run ESLint run: npm run lint

Create CI Job to run Tests

For adding the CI job that runs tests, first define the work, before assigning the task a descriptive title like tests. Also, we'll specify the fact that the job depends on the tests of the ESLINT job. This implies this test job is run ahead of the time when tests are completed. tests job is finished. The test code is examined for syntax mistakes prior to performing the tests.

test Name"Run Tests"needs to eslint can be run on Ubuntu-latest

The next step is to establish the specific steps to follow for the test test. Like the initial job we'll look through the source code and set up Node.js Version 18.x to run the tests. Install the dependencies on the application using the command npm ci. Then, you can run the tests using the "run test" command.

 steps: - name: Checkout code uses: actions/checkout@v3 - name: Use Node.js 18.x to run Test uses: actions/setup-node@v3 with: node-version: 18.x cache: 'npm' - name: Install Dependencies run: npm ci - name: Run Tests run: npm run test

Create CI Jobs To deploy Using API

 deploy: name: Re-Deploy Application needs: [eslint, tests] runs-on: ubuntu-latest

The API: Learning API. API The API: Learning API

Create an API key: API key

  1. Log in into your My Dashboard.
  2. Navigate to your API Keys page ( Your name > Your company's settings >> API Keys).
  3. Click here to Make API Keys.
  4. Select an expiration date or choose a custom start date and the time period for will expire the key.
  5. The key must have distinct name.
  6. Click to make.
Create API Key on My
Make your API Key by logging into My.

How do I best to start deployment with API

In order for an app to be published via the API There are 2 conditions which must be met, including the ID number of the app as well as the branch. The ID of your application using a search engine to find the list of your apps that will give you information regarding every application and its ID.

curl -i -X POST \ https://api..com/v2/applications/deployments \ -H 'Authorization: Bearer ' \ -H 'Content-Type: application/json' \ -d ' "app_id": "", "branch": "main" '

Trigger Deployment with cURL the CI/CD Pipeline

In order to trigger deployment, you must use the API API In order for deployments to be triggered using the API Just include the command into the running command in the CI pipeline. However, it's important to store your API key as well as the application's ID securely.

  1. Go to the repository where you'd like to post your information to.
  2. Visit the Settings Tab in the menu that is available for the repository.
  3. The sidebar on the left is where you can go to Secrets in the option category.
  4. Click here to access The latest repository secrets.
  5. Choose a distinctive name to identify your private key (like _API_KEY) and then enter the API key in the value field.
  6. After you've entered the name and the value, hit the"Add secret" button. "Add secret" option to store the information.
  7. Continue the exercise to gain more ways.
Storing secrets on GitHub
Keep secrets secure with GitHub. GitHub.

Once you have added the secrets, you can reference them in your GitHub Actions workflow by using the $secrets.SECRET_NAME |syntax.|secrets.SECRET_NAME} syntax.

It's time to wrap up the deployment job to the Actions on GitHub CI/CD pipeline. Perform the steps in the previous step however, with just one way to deploy it to . In the beginning, you must specify the key in the command env command. After that, add the cURL command in order to execute the operation.

 steps: - name: Deploy to env: _API_KEY: $ secrets._API_KEY APP_ID: $ secrets.APP_ID run: | curl -i -X POST \ https://api..com/v2/applications/deployments \ -H "Authorization: Bearer $_API_KEY" \ -H "Content-Type: application/json" \ -d ' "app_id": "'"$APP_ID"'", "branch": "main" '

If you use the command cURL, you'll be able to see that any variables relevant to the current environment are added to the command. This allows confidential data to be kept secure during the process of deployment.

The following is the final workflow process for CI/CD will be in the following format:

name: Build, Test, and Deploy on: push: branches: "main" pull_request: branches: "main" jobs: eslint: name: Check Syntax with ESLint runs-on: ubuntu-latest strategy: matrix: node-version: [18.x, 20.x] steps: - name: Checkout code uses: actions/checkout@v3 - name: Use Node.js $ matrix.node-version to Check Lint uses: actions/setup-node@v3 with: node-version: $ matrix.node-version cache: 'npm' - name: Install Dependencies run: npm ci - name: Run ESLint run: npm run lint tests: name: Run Tests needs: eslint runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Use Node.js 18.x to run Test uses: actions/setup-node@v3 with: node-version: 18.x cache: 'npm' - name: Install Dependencies run: npm ci - name: Run Tests run: npm run test deploy: name: Re-Deploy Application needs: [eslint, tests] runs-on: ubuntu-latest steps: - name: Deploy to env: _API_KEY: $ secrets._API_KEY APP_ID: $ secrets.APP_ID run: | curl -i -X POST \ https://api..com/v2/applications/deployments \ -H "Authorization: Bearer $_API_KEY" \ -H "Content-Type: application/json" \ -d ' "app_id": "'"$APP_ID"'", "branch": "main" '

Copy the given workflow and paste it into your build-test-deploy.yml file. Then, create an open pull-request to add the file to the initial part of the repository. It is important to note the pull request that initiates the process.

It lets you review any changes made in your repository. You can ensure that any new change in the pull request is in line with the required requirements prior to deciding whether it is appropriate to integrate the changes in your codebase.

Storing secrets on GitHub
Save secrets to GitHub.

When you merge the pull request. You'll be able to open the actions tab in your GitHub repository. It will allow you to view the workflow of CI/CD moving.

GitHub Actions summary
GitHub Summary of Actions.

For more information, click on each job. details on the task (this is why that you must describe every phase of your work in short descriptions of the task).

CI steps details
CI steps details.

Making sure that workflows for Pull Requests are enforced on GitHub

In order to ensure the proper control of code, and to ensure collaboration among GitHub repositories, you must implement a pull request process and stop the direct execution of commits on branches that are not related to the primary branch. This creates a controlled and controlled development procedure that will require that any changes are scrutinized and pulled prior to merging with the branch which is the primary branch.

When they stick to this method, developers will improve the efficiency of their software and decrease the risk of creating bugs. Additionally, they can maintain undocumented evidence of changes implemented.

This is the method to configure a workflow for pull requests that enforces:

  1. Click on the settings tab in the repository you have created in GitHub.
  2. In the menus under Automation and Coding Choose The Branches menu is located within the menus in the sidebar options.
  3. If you don't have a rule being implemented, then you can click to Add Branch Protection Rule.
  4. Name the rule. Then, you may choose to verify whether there is a need for a pull request prior to merging. After that, you'll see other options for setting up.
  5. Additionally, make sure to make sure to mark the box with "Checks on status" which must be cleared prior to merging.
  6. Create additional options that you can customize based on your preferences and requirements.
  7. Press the button to activate the button to keep the rule in place.
Enforcing pull request workflow on GitHub
Pull request workflows need to be enforced by GitHub.

If you follow these steps, you've set up a rulebook to regulate the method of pulling pull requests from the GitHub repository. The rule ensures that each modification will be scrutinized in conjunction with automated verification prior to its integration into the branch that is the main one. It results in an productive and efficient setting for the development.

Summary

Through the use of GitHub Actions and the API for streamlined development workflow and foster the environment of collaboration and efficiency for your team of developers.

Developers can feel confident about the submission of their software knowing they will be thoroughly test before releasing the code for production. Additionally, all those who will be affected is at ease knowing that the deployment process is safe and properly controlled.

     How do you use the API? What API endpoints would you like to see to be included within the API? Which API-related tutorials do you want to see in the very near time?

Joel Olawanle

Joel is an Frontend developer who works as a Technical Editor. Joel is a passionate educator, who is committed to open source. He has published more than 200 technical papers mostly about JavaScript and the frameworks used by it.

This article was originally posted here

Article was first seen on here