Categories
Automation

GitHub Actions Tutorial Part-1: Hello World

Github actions are highly useful to automate your daily workflows like deployment, testing and what not!

The basic step to start with this tutorial would be creating a repository. You can also use any repository that you already have.

For all actions that we will create will be in a repository named action-demo.

Every workflow that you want to execute on an action taken on GitHub starts with Workflow defined at action-demo/.github/workflows/action_name.yml

It is also not advisable that we run commands from directly the workflow file. So in this tutorial, we will use Dockerfile. It will also give you an overview of the Docker environment and Dockerfile.

Hello World

1. Create a workflow file

action-demo/.github/workflows/hello_world.yml

on:
  push: # Run this action when push event triggers.
    branches: # Run this action on specified branches
      - master # Branch name

name: Hello World # Name of the action
jobs:
  Hello-World:
    name: Print Hello World #
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Printing Hello World
      uses: './' # Location of Dockerfile

Now we have to create a Dockerfile in the root of the directory. It is not necessary to create it in the root of the directory. You can change the path and update the yml file accordingly.

2. Create Dockerfile

Now we have to create a Dockerfile in the root of the directory. It is not necessary to create it in the root of the directory. You can change the path and update the yml file accordingly.

action-demo/Dockerfile

FROM ubuntu

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

3. Create entrypoint

Now as you can see out Dockerfile is pretty basic. It just executes a script called entrypoint.sh.

action-demo/entrypoint.sh

#!/bin/bash

echo "Hey There"

Don’t forget to make the entrypoint.sh executable. If you don’t know how to do that click me.

Now push the code to your repository with a nice commit message.

Checking the action on GitHub

As soon as you push the code to GitHub, GitHub actions will start to execute the action.

Follow these steps for seeing your action running.

Open your repository page on GitHub and click on Actions as in the below image.

Open repository page and click on Actions

You can see all your actions that triggered on the Actions page.

As you can see in the below image, actions successfully executed are with and which failed will be with . Any executing action will be with yellow circle with a spinner.

Click on the first “Hello World”.

GitHub Actions page

Click on “Print Hello World”

GitHub actions Jobs page

Click on “Printing Hello World” and you can see all the lines executed from your Printing Hello World step in the hello_world.yml file. Let’s call it the debug window.

As you can see in the below image the box with green border is our result Hey There.

GitHub actions debug page

Awesome! You just made your first GitHub action! In later parts I will be discussing Environment variables in the actions, accessing GitHub repository in the action code and much more.

Leave a Reply

Your email address will not be published. Required fields are marked *