GitHub basics: What are actions?

GitHub basics: What are actions?

Β·

4 min read

Now that we have a broad understanding of what Git is, let's dive deeper into some more specific topics.

IN this article, I'll explain what GitHub actions are and how you can use them.

I choose GitHub as the platform to highlight because I love to use it, and it's the biggest one out there.

What are GitHub actions

Actions are actions that take place on certain activities on GitHub. You can use these actions to automate specific tasks within your development cycle.

These actions can trigger specific events. Some examples might be: After each commit, when a new PR is created, etc.

Let's have a look at the high level overview of a action:

  • Event
    • Job
      • Step
        • Action

Let's go through these elements as they are pretty essential to understand.

GitHub action event

The event is the actual trigger for the workflow. There can be multiple triggers for one action.

Let's say we want to trigger the action when a new push is made to:

on: push

Or run the same action on multiple actions:

on: [push, pull_request]

You can also use a cronjob as an event I use this in my automated deployment process.

on:
  schedule:
    - cron:  '0 4 * * *'

There are many more events that can trigger these actions, including comments, labels, and more.

You can find the complete event list on GitHub.

Jobs inside your action

A job is a list of steps that are run on the same runner. If you have multiple jobs in your action, they will run parallel by default. (You can change this behavior)

We could, for instance, run a lint job, a test job, and a build job.

Jobs also need to run on a specific system. GitHub comes with a complete set of already made runners. They are virtual machines.

For instance, we can run Windows, Ubuntu, or Mac!

Jobs can also have specific names. Let's set up the three jobs we described above.

jobs:
    runs-on: ubuntu-latest
    lint:
        # steps
    test:
        # steps
    build: 
        # steps

Steps inside the action job

A step is a group of actions inside a specific job. Each item inside a step can share data between them.

An example of a step would be:

jobs:
    runs-on: ubuntu-latest
    build:
        steps:
            step-1:
                # Actions

Actions inside the action job

Actions are the brains behind the operation. They start to do something.

Let's say we want to add an action that says hello.

jobs:
    runs-on: ubuntu-latest
    build:
        steps:
            step-1:
                run: echo "Hi there! πŸ‘‹"

How does a complete workflow look like?

Let's take the last example and make this into a complete workflow.

The first thing we need to do is set a name for our workflow.

name: Our very first GitHub Action

Then let's determine when our action must run. I want it only to run when we manually tell it to.

on: workflow_dispatch

And the last part is to add jobs and action to it.

jobs: 
    Testing-Actions:
        runs-on: ubuntu-latest
    steps:
      - name: A action is born
        run: |
          echo "I'm walking! πŸšΆβ€β™€οΈ"
          echo "Actually, I'm running on ${{ runner.os }} πŸƒ"
          echo "I'm done! My status: ${{ job.status }} πŸŽ‰"

Open a new repo on GitHub.

Then click the actions tab on top to add your first action.

Add a new GitHub action

Paste the complete workflow as we created above:

name: Our very first GitHub Action
on: workflow_dispatch
jobs: 
    Testing-Actions:
        runs-on: ubuntu-latest
    steps:
      - name: A action is born
        run: |
          echo "I'm walking! πŸšΆβ€β™€οΈ"
          echo "Actually, I'm running on ${{ runner.os }} πŸƒ"
          echo "I'm done! My status: ${{ job.status }} πŸŽ‰"

To run this workflow, click on the specific workflow and press the "Run workflow" button.

Run workflow action

Once it's done, you can open up the workflow and see what went on. You should be able to see the job we defined and the steps it took.

GitHub action done running

And that's it. We have a working GitHub action. This is a super basic setup, and the possibilities for workflows are endless!

You can view my demo on GitHub as well.

What kind of workflow would you like to see?

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Did you find this article valuable?

Support Chris Bongers by becoming a sponsor. Any amount is appreciated!