Components of Github Actions

Understanding the fundamental components of Github Actions

Github Actions | Nodejsera



What are GitHub actions

It is a CI/CD tool which helps us to

  • Build Automations: Workflows to create build on every commit or on every PR.
  • Testing our code: Workflows can be created which can run test cases on every commit or whenever any PR is created.
  • Deployment pipelines: We can create a workflow which can deploy our code on every PR or which can deploy our code to dev on every commit.

Components of Github Actions

  1. Workflows:
    • Configurable automated process that will run one or more jobs.
    • We write workflows in YAML files. Create a .github/workflows directory and add YAML file there with any name.
    • Running workflows- They are 3 ways to trigger workflows run:
      1. They can be triggered by an event in our repository. Some common examples of events includes creating a pull request, pushing our code, adding a label, etc
      2. They can be scheduled to trigger at a particular time. For example running a workflow at 12 PM everyday, etc
      3. They can be triggered manually to run. On the main page of repository, We can manually go to Actions under the repository name and run our workflow manually anytime.
      4. Please note that Additionally we can also trigger workflows using Rest APIs: We can also run the workflows via posting to a REST API.
    • Multiple Workflows: Yes, A single repository can have multiple workflows with different set of steps to perform.
    • Example of an Workflow:

                                                      
      name: Nodejsera first workflow
      
      on:
          pull_request:
          branches: [ $default-branch ]
      
      jobs:
          build:
          runs-on: ubuntu-latest
      
          steps:
              - uses: actions/checkout@v2
      
              - name: Print Something
              run: echo Hello from Nodejsera									
                                                      
                                                  

  2. Events: Any Specific activity in a repository in an event which triggers a workflow run. Some common example of events includes Creating a PR, Pushing code to repo, creating a new branch, opens/close an issue, etc.
    In the above example:

                                            
    //This is Event
    on:
        pull_request:
        branches: [ $default-branch ]
                                                                        
                                            
                                        

  3. Jobs:
    1. Set of steps in a workflow that run on a same runner are termed as jobs.
    2. Steps in the job are executed in order and are dependent upon each other
    3. Also, as we are running all the steps of the job on a single runner, data can be shared from one step to another.
    4. One job can be dependent or independent of other jobs as per our requirements.
    5. By default; Jobs run in asynchronous manner.
    6. When first job is dependent on second job, In that case it will wait for the second job to complete before it can start its execution.
    7. Example:

                                                      
      steps:
      - uses: actions/checkout@v2
      
      - name: Print Something
          run: echo Hello from Nodejsera
                                                                                  
                                                      
                                                  

  4. Actions:
    1. It is a custom application for github actions platform.
    2. Actions, similar to functions in coding, can be used to perform complicated but often repeated tasks.
    3. We can have multiple actions in our workflow to reduce the repetitive code.
    4. Example:

                                                      
      //Preexisting action which we are using
      steps:
          - uses: actions/checkout@v2                                       
                                                      
                                                  

  5. Runners: 1. 2. 3. 4. 5.
    1. Whenever a workflow is triggered, it requires a server to run it and these servers are known as Runners.
    2. Each Runner can run a single job at a time.
    3. Github Provides macOS, windows and ubuntu linux runners to run our workflows. Apart from this, We can host our own runners as well.
    4. A new VM is provisioned for each workflow run.
    5. We are using the ubuntu runner here:

                                                      
      //We are using the ubuntu runner here
      build:
          runs-on: ubuntu-latest
                                                                                  
                                                      
                                                  

Summary

In this article we learned about the components of github actions. We also learned the terminology required to understand github actions from a begineers perspective.