GitHub Actions: CI/Cd of GitHub

GitHub Actions: CI/Cd of GitHub

GitHub Actions as a Dedicated CI/CD Solution

GitHub Repository: https://github.com/ajinkyak423/ContextCli.git

What is the Problem?

Organizations that rely on GitHub for code hosting and collaboration need a dedicated CI/CD solution tailored specifically for the GitHub platform. While Jenkins is a widely recognized integration tool, its setup process and server requirements make it less suitable for seamless integration with GitHub. As GitHub serves as the primary platform for many projects, there is a need for a specialized CI/CD solution that can optimize and streamline workflows for GitHub-based development.

GitHub Actions as a Dedicated CI/CD Solution:

GitHub Actions provides a dedicated CI/CD solution designed explicitly for GitHub repositories. Unlike Jenkins, GitHub Actions eliminates the need for separate server setups, making it more efficient and well-suited for GitHub workflows. By leveraging GitHub Actions, organizations can streamline their development processes, enhance collaboration, and maximize the benefits of using GitHub as their primary platform.

Improved Workflow Streamlining:
With GitHub Actions, organizations can automate various tasks such as building, testing, and deploying applications directly within the GitHub environment. This eliminates the need for manual intervention and reduces the chances of errors. By defining custom workflows using YAML files, teams can precisely tailor the CI/CD process to meet their project requirements, ensuring smooth integration and seamless execution.

Pre-built Actions for Efficiency:

GitHub Actions provides a vast library of pre-built actions, which are reusable components that perform specific tasks. These actions can be easily integrated into workflows, significantly reducing development time and effort. With pre-built actions available for common operations like code checkout, dependency installation, and deployment, developers can focus on writing code rather than setting up complex build configurations.

Scalability and Parallel Processing:

GitHub Actions offers scalability by allowing workflows to run on different platforms and distribute tasks across multiple runners. This enables parallel processing, enhancing the overall speed and efficiency of the CI/CD pipeline. Organizations can seamlessly scale their workflows to accommodate increasing project demands without compromising performance.

Self-hosted Runners:

GitHub Actions allows you to use your own hardware or virtual machine to run workflows. This gives you more control over the execution environment and enables you to meet specific requirements or use specialized tools.

To set up a self-hosted runner in GitHub Actions, follow these general steps:

  1. Prepare your environment:

    • Set up a machine or virtual machine that meets the system requirements for self-hosted runners. The requirements vary depending on the operating system you plan to use.

    • Make sure the machine has a stable internet connection and can access the necessary resources, such as your GitHub repository. In the case of an EC2 instance don't forget to modify inbound and outbound rules.

  2. Create a new runner:

    • Go to your GitHub repository and navigate to the "Settings" tab.

    • In the left sidebar, click on "Actions".

    • Scroll down to the "Self-hosted runners" section and click on "Add runner".

    • Follow the provided instructions to download and configure the runner for your operating system.

    • Open CLI and peats given commands in order to set up a runner in the host platform.

    • Start the runner, and it will connect to GitHub and become available for use in your workflows.

  3. Configure workflows to use the self-hosted runner:

    • In your workflow files (e.g., .github/workflows/main.yml), set the runs-on parameter to self-hosted specify that the workflow should run on a self-hosted runner.

    • Push your workflow file to your repository, and GitHub Actions will automatically start using the self-hosted runner when executing the workflows.

With the self-hosted runner set up, GitHub Actions will now utilize your specified machine or virtual machine to run workflows. This allows you to customize the execution environment, install specific dependencies, and leverage resources that are not available in the default GitHub-hosted runners.

Remember to monitor and maintain your self-hosted runner, ensuring it stays connected and up to date with any necessary software updates or security patches.

Ubuntu runner

Secure and Encrypted Secrets:

GitHub Actions provides a secure environment for executing workflows. Sensitive information such as API keys, access tokens, or passwords can be securely stored using GitHub secrets. These secrets are encrypted and can only be accessed at runtime, ensuring the protection of sensitive data.

GitHub secrets are encrypted environment variables that you can store in your GitHub repository. They allow you to securely store sensitive information, such as API keys, access tokens, or passwords, that your workflows or actions need to use.

To use GitHub secrets, follow these steps:

  1. Go to your GitHub repository and navigate to the "Settings" tab.

  2. In the left sidebar, click on "Secrets".

  3. Click on "New repository secret" to create a new secret.

  4. Enter a name for the secret, such as "API_TOKEN" or "SHOWW_TOKEN".

  5. Enter the value of the secret, which is the sensitive information you want to store.

  6. Click on "Add secret" to save it.

Once you have added a secret, you can access it in your workflows or actions using the secrets.<secret_name> syntax. For example, to use the API_TOKEN secret, you would use secrets.API_TOKEN it in your workflow file.

Note that secrets are encrypted and cannot be viewed directly. They can only be accessed by your workflows or actions at runtime. Also, make sure to be cautious and follow security best practices when working with sensitive information.

By utilizing GitHub secrets, you can securely store and access sensitive information in your GitHub workflows, without exposing them in your code or configuration files.

Insights and Monitoring:

GitHub Actions generates detailed logs and insights for workflow executions, providing visibility into the CI/CD process. This helps in troubleshooting, identifying issues, and monitoring the overall performance of the workflows. Developers can easily track the progress of their workflows and gain valuable insights into the execution process.

Actions file example:

In this case, we are trying to write an actions file for the nodeJS application which I build called ContextCLI following is the starting portion of yaml file for it:

name: Node.js CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: self-hosted

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

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '12'

This GitHub Actions workflow file is named "Node.js CI" and it is triggered when there are push events or pull requests on the main branch of your repository.

Here's how this workflow file works:

  1. The workflow starts by checking out the repository using the actions/checkout action. This action fetches the contents of your repository in the runner environment, allowing subsequent steps to access the code.

  2. Next, the workflow sets up Node.js using the actions/setup-node action. It specifies the Node.js version as 12 using the node-version parameter.

  3. After setting up Node.js, you can include additional steps to build, test, or deploy your Node.js application. These steps are not included in the provided workflow file, but you can add them based on your requirements. For example, you could include steps to install dependencies, run tests, build the application, or deploy it to a server. To see ContextCLI setup for GitHub actions visit the repository mentioned in this blog head.

  4. The workflow is configured to run on a self-hosted runner. This means that you have set up your own machine or environment as a runner and registered it with your repository to execute the workflow.

By configuring this workflow, any changes pushed to the main branch or pull requests targeting the main branch will trigger the workflow. The workflow will then run on the self-hosted runner, performing the specified steps.

GitHub Actions benefits:

  1. Automation: Automate tasks like building, testing, and deploying your applications.

  2. Customization: Customize workflows to fit your specific project requirements.

  3. Integration: Seamlessly integrates with GitHub for streamlined collaboration.

  4. Pre-built Actions: Access a library of pre-built actions to save time and effort.

  5. Scalability: Run workflows on different platforms and distribute tasks for faster execution.

  6. Security: Securely store and use sensitive information with encrypted secrets.

  7. Insights and Monitoring: Gain detailed logs and insights into workflow execution.

Conclusion:

GitHub Actions serves as a dedicated CI/CD solution for GitHub repositories, offering streamlined workflows, automation capabilities, pre-built actions, scalability, a secure execution environment, and detailed monitoring insights. By leveraging GitHub Actions, organizations can optimize their development processes, enhance collaboration, and seamlessly integrate CI/CD functionalities into their GitHub-based workflows.