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)
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
- Check this
Hello World
workflow, you can copy/ paste the code into your.yml
filename: 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
As I told you before everything onIndentation, space, tab are very cruical in .yml file. So formate the code as is.
this file is very readable
except theactions/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. Commit
andpush
the code to theGithub
After youcommit
andpush
the changes toGitHub
you can notice adark yellow circle
which means theworkflow
isrunning
. Let’s navigate to theActions
tabInside theActions
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 markYou can notice now that bothjob
is completed here tooWe can navigate to each job and see that our command ran successfullyIf we take a look even further, we can notice the actions that we specified were downloaded at first. Then rest of our code executed