CI/CD with GitHub Actions – Concepts

Welcome to the first part of our blog on GitHub Actions!

This blog will delve into fundamental concepts essential for understanding GitHub Actions. Get ready to embark on a journey through the basics, paving the way for the upcoming implementation part of our blog. Stay tuned for hands-on demonstrations and practical applications in the next blog. Let’s dive in!

What is GitHub Actions?

GitHub Actions enables users to automate various GitHub events, such as cloning a repository, generating Docker images, and testing scripts. Developers use it to automate workflows across issues, pull requests, and more. This means that users can build, test, and deploy code written in different languages and running on different platforms, all using the same automation tool.

How do GitHub Actions work?

GitHub Actions uses YAML files to define workflows. These YAML files are stored in a .github/workflows directory in the user’s repository. Each workflow consists of one or more jobs, and each job consists of one or more steps.

GitHub Actions provides a wide range of tools and features that make it easy for users to configure and manage their workflows. as i mentioned earlier in the first paragraph, GitHub Actions allows automation of your software development workflows.

Why should you use Github Actions?

There are several benefits to using GitHub Actions for automation.

1. Simplified workflows: GitHub Actions simplifies the process of building, testing, and deploying code.

2. Integration with GitHub: GitHub Actions is tightly integrated with GitHub, which means that users can trigger actions based on events that occur within their repositories.

4. Supports a wide range of platforms and languages: GitHub Actions supports many platforms and languages, means users can use the same automation tool for different projects and languages.

5. Secure and reliable: GitHub Actions also provides built-in security features, such as the ability to store secrets securely and to control access to workflows.

6. Cost-effective: GitHub Actions is a cost-effective automation tool. Users can use it for free for public repositories, and there is a generous free tier for private repositories.

7. Easy to get started: GitHub Actions is easy to get started with. Users can use the visual editor to create workflows.

How to set up GitHub action in GitHub repository :

Till now, I have explained how one can add Github Action to the GitHub repository. I have taken an example of my Github repository which already has some code pushed into the main branch. In the GitHub repository, one must create (.github/workflows/directory) and add a .yml file.

There are two ways to create a .yml file. In this blog, I have explained (Through Web Application (Github) directly)

Mention all the steps to create a .yml file:-

a. Go to repository

b. Click on Add File > Create New File

c. Enter .github/workflows/main.yml ( It has to be in the same sequence)

d. Now, you can add the code to the main.yml file and commit changes to your GitHub repository.

name: My GitHub Action Workflow 
on:
push:
pull request:
branches:
- main
- feature
- stg
- dev
workflow dispatch:
inputs:
environment:
type: environment
description: Select the environment.
Boolean:
type: choice
description: Make a choice.
options:
- prod

How to view GitHub action activity:

Once the GitHub actions are configured and start running.

  1. Go to your repository on Github.com
  2. Under the repository name, click on the Actions tab

3. Select the workflow(My GitHub Action Workflow) you want to see from the left sidebar.

4. Under “Workflow ” click the name of the run you want to see.

5. One can view the result of each step by clicking on the workflow.

Here are some components of GitHub Actions or key concepts of GitHub Actions that I used.

The relationship between these components is illustrated below:

Using the flow diagram of GitHub action, I’ll try to explain how GitHub action works.

In practice, workflows are more generalized than a CD pipeline, but they are closely related:

  1. Workflows = pipelines
  2. Jobs = stages
  3. Steps = the series of procedures that make up a stage

1. Name

A name for your workflow to help you identify it.

name: My GitHub Action Workflow 

2. Trigger

Specifies when the workflow should be executed. This can be based on events, schedules, or manual triggers. Trigger on a push or pull request to the main or features branch.

on: 
push:
pull request:
branches:
- main
- feature
- stg
- dev

3. Steps

Specify the individual tasks or commands that should be executed within a job. Steps run sequentially, and you can use built-in or custom actions.

    steps:
- name: code checkout
uses: actions/checkout@v2.
- name: Set up Java version
uses: actions/setup-java@v1.
with:
java-version: '11'

4. Jobs

A workflow must have at least 1 job and can consist of multiple jobs that work together sequentially or in parallel. Each job is a series of steps that runs under the same runner (virtual environment).

jobs:
build:
runs-on: ubuntu-latest.
steps:
- name: code checkout
uses: actions/checkout@v2.
- name: Set up Java version
uses: actions/setup-java@v1.
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install.
- name: Static Code Analysis with checkstyle
run: mvn checkstyle:checkstyle

5. Secrets

You can access secrets (encrypted environment variables) in your workflow, typically used for sensitive information like API keys or passwords.

    steps:
- name: Install rsync
run: Sudo apt-get install rsync -y
- name: Deploy to Server 1
uses: easingthemes/ssh-deploy@main
env:
SSH_PRIVATE_KEY: ${{secrets.EC2_SSH_KEY}}
REMOTE_HOST: ${{ secrets.HOST_DNS }}
REMOTE_USER: ${{ secrets.USERNAME }}
TARGET: ${{ secrets.TARGET_DIR }}
aws-region: ${{ env.AWS_REGION }}
ARGS: -r
SCRIPT_BEFORE: |
whoami
ls -al

6. Workflow

  1. The workflow is an automated procedure that you add to your repository.
  2. They are made up of one or more jobs and can be scheduled or triggered by an event.
  3. It can be used to build, test, package, release, or deploy a project on GitHub.

7. Runners

  1. A runner is a server that has the GitHub Action Runner application installed.
  2. you can use a runner hosted by GitHub, or you can host your own.

GitHub Actions can use two types of runners: hosted and self-hosted.

  • Hosted runners are provided by GitHub and run on virtual machines in the cloud.
  • Self-hosted runners are machines that you set up and manage yourself. They run on your infrastructure.

8. Actions

  1. Actions are standalone commands that are combined into steps to create a job.
  2. Actions are the smallest portable building block of a workflow.
  3. You can create your own actions, or use actions created by the GitHub community to use actions in a workflow.

9. Artifacts

  1. Artifacts are files or outputs generated by a job or workflow run.
  2. You can archive artifacts for later use or share them between jobs within the same workflow.
      -  name: Login to Docker using jfrog
uses: docker/login-action@v2.
with:
registry: ${{ secrets.REGISTRY_URL }}
username: ${{ secrets.JFROG_USERNAME }}
password: ${{ secrets.JFROG_TOKEN }}
- name: push on a jfrog
uses: docker/build-push-action@v5.
with:
context: .
file: ./Dockerfile
push: true
tags: |
bhanu007.jfrog.io/docker/${{GitHub. Sha}}

10. Notifications

GitHub Actions provides notifications and status reporting, allowing you to receive alerts and updates on the status of your workflow runs via email, Slack, or other communication channels.

Conclusion

GitHub Actions is a powerful automation tool that allows developers to build, test, and deploy their code directly from GitHub. It simplifies the process of building, testing, and deploying code, and is tightly integrated with GitHub.

Overall, GitHub Actions is a valuable addition to the GitHub platform and is a great option for developers who want to automate their workflows.

That’s it!

I hope you found this tutorial helpful, and that you will be able to set up and manage this GitHub action workflow.

Blog Pundits:  Sajal Jain and Sandeep Rawat

OpsTree is an End-to-End DevOps Solution Provider.

Connect with Us

Leave a Reply