Understanding GitHub Actions and github workflow

github-action-github-workflow

How to Create Github Workflows or github actions with YAML

github
github workflows
yml
github actions
yml file

GitHub actions are automated tools that can run some user-specified tasks based on some event that triggers those tasks. So in simple words, a specific event triggers a workflow which then runs some job that consists of a couple of functions written in a .yaml file. An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task. The job runs on a server called a runner.

You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

GitHub requires the user to place the action file (.yml file) in a specific directory (.github\workflows) of the repository. This file (.yml) runs the tasks specified inside the file.

So to understand GitHub actions we must learn about (events, workflow, jobs, runner, yaml files)

a diagram explaining github workflows

Events

An event is a specific activity in a repository that triggers a workflow run. This is what initiates the GitHub actions.

For example, an activity can originate from GitHub when someone creates a pull request, push to any specific branch, opens an issue, or pushes a commit to a repository.

Workflows

A workflow is an automated process that you can set up to run one or more tasks. It is configured using a YAML file in your repository. **Workflows **can be triggered by events like a push to your repository, manually by a user, or even on a scheduled basis.

Jobs

A job is a set of steps in a workflow that is executed on the same runner. Each step is either a shell script that will be executed or an action that will be run. Steps are executed in order and are dependent on each other.

Runners

A runner is a server that runs your workflows when they're triggered. Each runner can run a single job at a time. GitHub provides Ubuntu Linux, Microsoft Windows, and macOS runners to run your workflows. Each workflow run executes in a fresh, newly-provisioned virtual machine.

YML or YAML file

YAML, originally known as Yet Another Markup Language, was created in 2001 but now stands for YAML Ain't Markup Language. A yml is just like a JSON file but more human-readable in nature. YAML can do everything that JSON can and more.

Write a Github action example in the Hello world YML File

  • Create a repository or you can use an existing repository
  • Create a .github folder in the root of your project (This is a required step and the folder name must be .github)
  • Create a folder named workflows inside the .github folder (This is a required step and the folder name must be .github)
  • Create a *.yml file, you can name it anything you wish

.yml file

  1. Check this Hello World workflow, you can copy/ paste the code into your .yml file
    name: Hello World Workflow
    
     on:
         push:
             branches:
                 - main
         pull_request:
             branches:
                 - main
         workflow_dispatch:
    
     jobs:
         hello:
             runs-on: ubuntu-latest
             steps:
                 - uses: actions/checkout@v2
                 - name: hello world
                   run: echo "Hello World"
                   shell: bash
    
         thankyou:
             runs-on: ubuntu-latest
             steps:
                 - name: thank you
                   run: echo "Thank you"
                   shell: bash

    Indentation, space, tab are very cruical in .yml file. So formate the code as is.

    As I told you before everything on this file is very readable except the actions/checkout@v2 may be. Here we have used one action that is already available in the GitHub marketplace, we can write our own action too but for this simple example let’s use what is already available to usactions/checkout@v2 Checkout is an action that is available to us on the GitHub marketplace. https://github.com/actions/checkout https://github.com/marketplace/actions/checkoutWe can write our actions too.
  2. Commit and push the code to the Github After you commit and push the changes to GitHub you can notice a dark yellow circle which means the workflow is running. Let’s navigate to the Actions taban image representing github workflow is runningInside the Actions tab, you can notice the workflow is already added as we named earlier in our yml file **Hello World Workflow. **Let’s navigate to the **Update sitemap.xml **task that is already completed and marked as a dark green checked markan image representing github workflow is enlisted on the repositoryYou can notice now that both job is completed here tooan image representing github workflow is completed its jobWe can navigate to each job and see that our command ran successfullyan image representing github workflow succefully ran our commandIf we take a look even further, we can notice the actions that we specified were downloaded at first. Then rest of our code executedan image representing github workflow downloaded the checkout action