DEV Community

Cover image for GitHub Actions : Zero to Hero
RITIKA SINGH
RITIKA SINGH

Posted on

5 1

GitHub Actions : Zero to Hero

What Are Github Actions?

GitHub Actions are packaged scripts to automate tasks in a software development workflow in GitHub.

That is all the processes (or GitHub Events) like the creation of a pull request, committing your code, pushing into the production/ any branch, etc can be monitored by GitHub actions, and hence when an event occurs the workflow is triggered.

Github Actions Workflow

A GitHub Actions Workflow is a process that you set up in your repository to automate software development life cycle tasks. It is a configurable automated process made up of one or more jobs.

Each repository has this actions tab in which the defined actions run :

Each repository has this actions tab in which the defined actions run

One must create a YAML file to define the workflow configuration. If you’re new to YAML and want to learn more, see Learn YAML in Y minutes.

GitHub actions are categorized as Container actions and JavaScript actions.

Container actions
As the name suggests in container actions, the environment is part of the action’s code. These actions can only be executed in Linux-based runners that GitHub hosts. Or if one wants to create a Self -hosted runner (read about runners) make sure it uses Linux OS and has Docker installed. Container actions support many different languages for execution.

JavaScript actions
They do not include the environment in the code. This means that one has to specify the environment to execute these actions. They can run in a VM in the cloud or on-premises. They not only support Linux but macOS and Windows environments also.

Let’s Start by Creating A Simple Workflow:

  1. Create a new repository and then create a workflow (say action.yml) in the workflows folder so the final path of your workflow will be like .github/workflows/action.yml

  2. Now, our first job is to add the on and name attribute to our workflow.

name: defines the name of your workflow.
on: defines the event on which the workflow has to be triggered.

There are plenty of Github events that trigger any workflow (read about GitHub events that trigger workflows)

# Define the name of your workflow its optional if you don't then Github creates it automatically.
name: Action
on: # this attribute defines that at which event the workflow has to be triggered
push:
branches: # defining branches for the event is optional you can also do like [push,pull_request]
- master # if you do not define the branch then if there is this "push" occuring in any branch , the workflow will be triggered
pull_request:
branches:
- master
schedule: # this attributes enables that the workflow is run at a time interval (shortest interval is every 5 min)
- cron: "*/15 * * * *" # to run the workflow every 15 min
workflow_dispatch: # for running the workflow manually
inputs:
logLevel:
description: "Log level"
required: true
default: "warning"
tags:
description: "Test scenario tags"
  1. Next essential requirement of a workflow is the job that it has to do (a workflow has to have a minimum of one job!)

  2. One can define multiple jobs for a workflow. Each job has sundry attributes the major are name, runs-on, steps.

name: defines the name of that particular job in a workflow.
runs-on: defines the runner of that job. (runner can be a Github Hosted Runner or a self-hosted Runner)
steps: defines the steps to be taken in that job.

jobs: # you can define multiple jobs in a workflow but atleast there has to be one job else error will be there.
job1:
name: Custom-1
runs-on: ubuntu-latest # this attribute defines the runner for this job here "ubuntu-latest"
steps:
- name: Step-1
uses: actions/checkout@v2 # this is a custom checkout action repo hosted by github
- name: Step-2
run: env | sort # for the output in the alphabetical order so piped the env print
job2:
name: Custom-2
runs-on: windows-latest
steps:
- name: Step -1
uses: actions/checkout@v2
- name: Step-2
run: "Get-ChildItem Env: | Sort-Object Name" # quoted this because the command has : which may be interpreted as a command of the workflow
job3:
name: Custom-3
runs-on: ubuntu-latest
steps:
- name: Step -1
uses: actions/checkout@v2
- name: Step-2
run:
| # whenever you want to run multiple commands sequentially use this pipe char
echo "DATE::$(date +'%Y-%m-%dT%H:%M:%S')"
echo $DATE
job4:
name: Custom-4
runs-on: ubuntu-latest
needs: [job2, job3] # needs attribute defines that job4 needs job2 and job3 for its execution (dependency).
steps:
- name: Step-1
run: |
echo $DATE

5 . Commit the code, open the Actions tab in your repository and see your actions running!

Actions tab

So here you can see that all your jobs in the action.yml file ran successfully. And our job named custom3 (line 38) ran and gave us a DateTime output!

And Done! Aren’t GitHub Actions cool 😎!

Let me know in the comments if you have any doubts or face issues, hope it helps you. 😀

Do check out my session on Git & GitHub actions : (Jump to 17:17 for the real fun 😉)

You can also reach out to me on LinkedIn , Twitter , GitHub.

Top comments (0)