
Learning GitHub Actions Step by Step
M. Zakyuddin Munziri
@zakiego
Originally written in Bahasa Indonesia.
Background
There are many things you can do with GitHub Actions. Among them, I can run a script to scrape the KPU (Indonesian Election Commission) website, every day, automatically, without having to run it manually (zakiego/data-pemilu-2024). Another thing you can do is deploy a website every time code is pushed to the repository (cloudflare/pages-action).
Prerequisites
The assumption for readers of this article is that they already understand git, to create a repository and push to a repository. It would also be very helpful if you frequently use the terminal.
Introduction
Very simply put, GitHub Actions is a server owned by GitHub that we can borrow to run code. We can run code anytime, for example, we want to run it once every hour. Or we can also run it every time there's a push to a GitHub repository.
In a more complex sense, GitHub Actions is a platform for running continuous integration and continuous delivery (CI/CD) to automate the build, test, and deployment process.
Structure
First, we need to prepare a project folder locally, for example, let's name it belajar-github-action. To create a GitHub Actions script, we need to place it inside the folder /belajar-github-action/.github/workflows/{place it here}.
Let's create a file called run.yml, this file has the yml format. We can freely name the file anything, and we can even create multiple GitHub Actions files in one repository.
βββ README.md
βββ .github
β βββ workflows
β βββ run.yml
There are 3 important components in a GitHub Actions file:
- What is the name of the workflow?
- When should the workflow be run?
- What things need to be run?
These questions are then outlined in a simple GitHub Actions file as follows:
name: Belajar Github Action
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Hello, world!"
- run: echo "This is my first Github Action"
- run: echo "I'm learning how to use Github Action"
How the action runs can be seen at zakiego/belajar-github-action/actions.
The source code for the action can be seen at zakiego/belajar-github-action/.github/workflows/run.yml
The name of this workflow is "Belajar Github Action".
When will this workflow be run? It's when there's a "push". Simply put, it means when code is pushed to the repository, this workflow will be run.
jobs is a group of jobs that we create. In one group, we can create multiple child jobs, but in this example, we only create one child job, which is 'build'.
Inside build there are runs-on and steps.
runs-on is the machine used to run the workflow, GitHub provides Ubuntu Linux, Microsoft Windows, and macOS. Additionally, we can also run GitHub Actions on our own server using self-hosted mode (docs).
Finally, steps are the steps or the core of the script we want to run.
For example, if we want to run a script index.ts using bun, the workflow would look like this (source):
name: my-workflow
jobs:
my-job:
name: my-job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun index.ts
- run: bun run build
Or another example, if we want to run the build and test process for Golang code, every time code is pushed to the repository (source):
name: Go
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "1.21.x"
- name: Install dependencies
run: go get .
- name: Build
run: go build -v ./...
- name: Test with the Go CLI
run: go test
Additional Notes
If you notice, there's the use of
actions/checkout@v4oven-sh/setup-bun@v1actions/setup-go@v4
What are their functions?
In short, uses is a process where we use a kind of ready-to-use library to simplify the work process.
oven-sh/setup-bun@v1 functions to set up bun and actions/setup-go@v4 functions to set up Golang automatically, so we don't have to bother with curl or long steps to install them.
Meanwhile, actions/checkout@v4 is interesting.
For example, if we have a file index.ts in the repository. If we want to run this code, we need to checkout using actions/checkout@v4, then we can run it. If we don't use actions/checkout@v4, then GitHub Actions doesn't have access to our repository, meaning it doesn't know what the index.ts file we want to run is.
Closing
There's still much more to learn. For example, schedule (#schedule) to run at certain time intervals. Or pull_request (#pull_request) to run every time there's a pull request. We can even run GitHub Actions via API, namely repository_dispatch (#repository_dispatch).
References
Originally written on March 14, 2024 in Banjarmasin
Completed on March 30, 2024 CE/19 Ramadan 1445 H in Pelaihari, 7:53 AM
Edited on April 1, 2024 in Banjarmasin, 16:22


