Subscribe

GitHub basics: What are actions?

✍️

What are GitHub actions and what can we use them for?

23 Nov, 2021 · 5 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.

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 an 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 * * *'

Many more events 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 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 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 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 workflow possibilities are endless!

You can also view my demo on GitHub.

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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Undo wrong Git changes

30 Jul, 2022 · 3 min read

Undo wrong Git changes

Git basics: Changing your last commit message

19 Jul, 2022 · 2 min read

Git basics: Changing your last commit message

Join 2097 devs and subscribe to my newsletter