Introduction to GitHub Actions

Introduction to GitHub Actions

A blog covering major topics for CI/CD using GitHub Actions, explaining workflows and deployment.

Lights! Camera And ACTIONS! 📸

What are GitHub Actions?- A basic question aroused for handling the concept. It is a simple platform used to automate developer workflows.

Wait! Then what about CI/CD? CI/CD is one of the many workflows that we can automate using GitHub Actions.

Now, what are workflows? 🤔

A workflow is the ‘thing’ (or all the things) that happen after a specific other ‘thing’ (ie. event) occurs.

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named .github/workflows.

Simply- It will be an action or collection of actions that will be taken or triggered when an event has occurred.

For example — Let us consider a situation when you collect milk from the doorstep after being kept by the milkman.

---
name: Milk Collection
on: [when kept on door by milkman]  
jobs:  
  milkCollection:    
    runs-on: my-home    
    steps:      
     - uses: actions/checkout@v2      
     - name: milk-collector        
       uses: milk/action-KeepItInTheFridge@v3

Here, it can be understood that the Milk Collection executor will be executed.

But when?
When it will be kept on the door by the milkman.

Now it seems that an event has been triggered making it follow the jobs specified. Well, there is one job that is specified, milkCollection.

Note: There can be multiple jobs that will be followed in creating their workflow.

milkCollection will be running however, here we specify the environment into which it should be running. Of-course!! quite possible it might start working for a neighbor’s home, so precisely specifying my-home as the environment.

Also, specifying the environment helps the further steps to be executed as per the requirement, making the job to be finished properly.

Starting with the steps, several steps are considered before doing any of the work, so here we are going to collect milk from our doorstep, so before that we should be checking out from our room, wearing slippers and then moving forward to the gate and opening it.

Right? On reaching the gate and being able to collect the milk can now act by keeping it in the fridge.

BOOOMMM!! 💥

Milk succesfully kept into the fridge

Before moving ahead, we should be learning a bit about Events!

An event is something that happens in a GitHub repository. It includes things like opening an issue, creating a pull request, making a commit, merging a PR, or joining a contributor.

So the response of which automatic ACTIONS are executed.

But How GitHub Actions are taking part in this?

Let us build some pipelines and workflows in our repository to have a practical demonstration.

Situation: We have a repository that is serving for open-source and there we wish to greet every new contributor.

actions-opensource

💡 Here we go with a repository, ready for open-source contribution. People will be forking it to contribute to it so we are trying to set up a workflow into a YAML file which will do the required actions.

For setting up a workflow file .yml extension, follow these steps —

Step 1: Click on the Actions tab, which is present in your repo.

Step 2: Simply go to ‘set up a workflow yourself’.

Creating a workflow for greeting on PR and Issues

NOTE: GitHub will automatically create a .github/workflows directory for handling the YAML files.

---
name: Greetingson: [pull_request_target, issues]jobs:
  greeting:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
    steps:
    - uses: actions/first-interaction@v1
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        issue-message: "You can be a problem solver! Go for it."
        pr-message: "Congo for your PR! Your PR may create an  
        impact."

Here you can use this workflow for your setup. You can understand from here that for triggering the workflow, a pull_request_target or issues event should take place.

Point to note: actions/first-interaction@v1 is being used. ‘actions’ has its document which is a script helping in quickly writing a script in your workflow that uses the GitHub API and the workflow run context.

Read more about it here 📖: https://github.com/actions/github-script

Step 3: Making it available to the public and being ready for a Pull Request.

Someone dropping a PR for a contribution

Step 4: Wishing them to use the workflow we have created. And it’s a success!!

Step 5: The Pull Request might be accepted or updated by the solution you have come up with.

Conclusion in some collections of words

  1. CI/CD is just a set of practices that application development teams use to deliver code changes more frequently and reliably.

  2. With GitHub Actions, you can trigger CI/CD workflows and pipelines of webhooks.

  3. Still, for CI/CD a bunch of tools are available to configure and execute the workflows.

  4. There are several events defined by GitHub to trigger the workflow. They can be listed: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows

References:

  1. Docs for GitHub Action: https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions

  2. About workflows: https://docs.github.com/en/actions/using-workflows/about-workflows

  3. About CI/CD: https://www.redhat.com/en/topics/devops/what-cicd-pipeline