CHAPTER 02
Understanding GitHub Workflows
Updated: May 15, 2026
20 min read
# CHAPTER 2
Understanding GitHub Workflows
1. Introduction
In Chapter 1, we saw GitHub Actions print "Hello World" to a console. But how did GitHub know *when* to print it? How did it know *where* to run the command? To build complex automation, you must understand the core architecture of a GitHub Action. The entire system is built around a hierarchy of five specific components: Workflows, Events, Jobs, Steps, and Runners. In this chapter, we will dissect this architecture and understand how these pieces fit together to form a CI/CD pipeline.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the five core components of GitHub Actions.
- Explain how "Events" trigger automation.
- Understand the hierarchical relationship between Jobs and Steps.
- Describe what a "Runner" is.
- Conceptualize how multiple jobs execute in parallel.
3. Beginner-Friendly Explanation
Imagine a professional restaurant kitchen.- 1. The Event (Trigger): A waiter hands a ticket to the kitchen: "Table 4 wants a burger." This triggers the whole process.
- 2. The Workflow (The Process): The master plan for fulfilling the order. "We need to cook the meat and prepare the salad."
- 3. The Jobs (The Stations):
- *Job 1:* The Grill Station (Cooks the meat).
- *Job 2:* The Prep Station (Chops the salad).
- 4. The Steps (The Tasks): At the Prep Station, the chef must follow steps sequentially: Step 1: Wash lettuce. Step 2: Chop tomatoes. Step 3: Add dressing.
- 5. The Runner (The Chef/Kitchen): The physical person and equipment actually doing the work.
4. The Five Core Components
Let's map the restaurant analogy directly to GitHub Actions:-
1.
Events (
on:): A specific activity that triggers the workflow. The most common ispush(someone uploads code), but it can also bepull_request,schedule(like a cron job), or evenissues(when someone opens a bug report).
-
2.
Workflows: The automated procedure added to your repository. Defined by a YAML file inside
.github/workflows/. A repository can have multiple workflows (e.g., one for testing, one for deploying).
-
3.
Jobs (
jobs:): A set of steps that execute on the *same runner*. By default, if a workflow has multiple jobs, they run in parallel (at the same time) on completely different cloud servers to save time.
-
4.
Steps (
steps:): Individual tasks within a job. Steps run sequentially (one after the other). A step can be a simple shell command (likeecho "Hello") or a pre-built Action downloaded from the Marketplace.
-
5.
Runners (
runs-on:): A server provided by GitHub (or hosted by you) that runs your workflow. When a workflow is triggered, GitHub provisions a brand new, clean Virtual Machine, runs your job, and then destroys the machine.
5. Visualizing the Hierarchy
Here is how the hierarchy looks in code:
yaml
6. Mini Project: Create an Automated Workflow on Push Event
Let's create a workflow that demonstrates the hierarchy and runs two jobs at the same time.Step-by-Step Walkthrough:
-
1.
In your GitHub repository, create or edit
.github/workflows/main.yml.
- 2. Paste the following YAML code:
yaml
- 3. Commit the code.
- 4. Go to the Actions tab. Click on the running workflow.
-
5.
Observation: You will see both
backend-testsandfrontend-testsrunning simultaneously! Even though each job takes 10 seconds, the entire workflow only takes 10 seconds total, because GitHub spun up two separate Ubuntu servers (Runners) to do the work in parallel.
7. Real-World Scenarios
A development team had a massive test suite. Testing the database took 20 minutes, and testing the user interface took 20 minutes. In their old system, these ran sequentially (one after the other), meaning developers had to wait 40 minutes for feedback. By migrating to GitHub Actions, they split the tests into two separatejobs. GitHub executed them in parallel on two separate runners. The total feedback time dropped to 20 minutes, doubling the team's productivity.
8. Best Practices
-
Keep Steps Small: Do not put a massive 50-line Bash script inside a single
runstep. Break it out into multiple steps with clearnameattributes. If the workflow fails, you want GitHub to highlight exactly which tiny step broke, rather than forcing you to read through a massive wall of text.
9. Security Recommendations
-
Beware of Public Pull Requests: If you maintain an open-source public repository, be very careful about triggering workflows on
pullrequest. A malicious user could submit a Pull Request containing code designed to mine cryptocurrency using your free GitHub Action minutes! (GitHub has built-in protections for this, but awareness is key).
10. Troubleshooting Tips
-
Sequential Jobs: What if Job 2 *needs* Job 1 to finish first? (e.g., You shouldn't "Deploy" until "Test" passes). By default, they run at the same time. To fix this, use the
needskeyword. Example:needs: backend-tests. We will cover this heavily in deployment chapters.
11. Exercises
-
1.
If a workflow contains two
jobs, and each job contains threesteps, which parts run in parallel and which parts run sequentially?
- 2. What is the operational benefit of GitHub destroying the Runner (the Virtual Machine) immediately after your job finishes?
12. FAQs
Q: Can a workflow be triggered by clicking a button manually, rather than pushing code? A: Yes! You can use the event triggeron: workflowdispatch. This creates a "Run Workflow" button in the GitHub UI, allowing you to trigger deployments manually.
13. Interview Questions
- Q: Explain the architectural hierarchy of GitHub Actions. How do Workflows, Jobs, and Steps relate to one another?
- Q: A developer complains that their CI pipeline takes too long because it compiles the application, runs unit tests, and runs security scans sequentially. How would you restructure the YAML file to optimize execution time?