Back to Blog
Lesson 3 of the CI/CD: Continuous Integration from Scratch course
DevOpsJuly 3, 20264 min read

Anatomy of a Workflow: Mastering GitHub Actions YAML Structure

Learn the structure of a GitHub Actions YAML file and how to organize your repository to trigger automated tasks. Build your first workflow today.

GitHub ActionsYAMLCI/CDDevOpsAutomation

Previously in this course, we discussed The CI/CD Philosophy: Automating Your Software Delivery and ensured you have your Environment Setup: Preparing your local development workspace ready for action.

Now that your local environment is configured, it is time to shift from manual operations to automation. We are going to build the foundation of your CI/CD engine by creating your first GitHub Actions workflow.

Understanding the Anatomy of a Workflow

In the world of GitHub Actions, a workflow is a configurable automated process made up of one or more jobs. These workflows are defined by YAML files stored in your repository.

Think of a workflow as a recipe. The YAML file lists the ingredients (the environment) and the steps (the commands) required to complete a task, like running tests or deploying code. GitHub looks for these recipes in a specific location: the .github/workflows directory.

Why YAML?

YAML (YAML Ain't Markup Language) is the industry standard for configuration files because it is human-readable and maps cleanly to data structures. In our case, it allows us to define complex pipelines in a declarative way—we tell GitHub what we want to happen, and GitHub handles the "how" of provisioning servers and running commands.

Creating Your First Workflow Directory

To get started, navigate to the root of the repository you initialized in our previous lesson. You need to create a directory structure that GitHub recognizes as the home for your automation.

  1. Open your terminal in your project root.
  2. Run the following commands:
    Bash
    mkdir -p .github/workflows
  3. Inside that directory, create a file named manual-trigger.yml.

Your file structure should now look like this:

TEXT
.
├── .git/
├── .github/
│   └── workflows/
│       └── manual-trigger.yml
└── README.md

Writing Your First YAML Workflow

Open manual-trigger.yml in your code editor. We will define a basic workflow that can be triggered manually via the GitHub interface using the workflow_dispatch event.

Copy this content into your file:

YAML
name: Manual Workflow

# This event allows you to trigger the workflow from the GitHub UI
on: [workflow_dispatch]

jobs:
  say-hello:
    runs-on: ubuntu-latest
    steps:
      - name: Print a message
        run: echo "Hello! The workflow is running."

Breakdown of the YAML components:

  • name: The display name of your workflow in the GitHub Actions tab.
  • on: Defines the trigger. workflow_dispatch is the secret sauce that adds a "Run workflow" button to your repository on GitHub.
  • jobs: A workflow can contain multiple jobs; here, we have one named say-hello.
  • runs-on: Specifies the type of machine (runner) to execute the job. ubuntu-latest is the standard choice for most Linux-based tasks.
  • steps: A sequential list of tasks. Each step can run shell commands using the run keyword.

Hands-on Exercise: Triggering the Workflow

Now that you have written the file, let's get it into GitHub:

  1. Stage and commit the file:
    Bash
    git add .github/workflows/manual-trigger.yml
    git commit -m "Add manual workflow"
    git push origin main
  2. Navigate to your repository on GitHub in your browser.
  3. Click the Actions tab.
  4. On the left sidebar, click Manual Workflow.
  5. You should see a banner saying "This workflow has a workflow_dispatch trigger." Click the Run workflow dropdown and confirm by clicking the green button.

You have successfully defined and triggered your first automated task!

Common Pitfalls

  • Indentation Errors: YAML is extremely sensitive to whitespace. Never use tabs; always use spaces (two spaces per indentation level is the community standard).
  • File Extension: Ensure your file ends in .yml or .yaml. GitHub will ignore files with incorrect extensions.
  • Hidden Directories: On some operating systems, the .github folder might be hidden by default. Use ls -a in your terminal to verify it exists.
  • Commiting to the Wrong Branch: If you aren't seeing your workflow in the Actions tab, ensure you pushed your changes to the default branch (usually main or master).

FAQ

Q: Do I need to learn the entire YAML specification to use GitHub Actions? A: Absolutely not. You only need to learn the specific keys (on, jobs, steps, run) defined by the GitHub Actions schema.

Q: Can I have multiple workflow files? A: Yes. You can have as many files as you like in the .github/workflows directory. They will all run independently based on their own triggers.

Q: What happens if my YAML has a syntax error? A: GitHub will usually flag the file with a warning in the Actions tab, and the workflow will fail to trigger. Always check the "Actions" tab for feedback on your file structure.

Recap

We have successfully organized our repository, created our first workflow file using YAML, and triggered it manually. You are now ready to start building more complex pipelines that interact with your code.

Up next: Hello World Pipeline — we will expand on this setup to output logs and verify execution in the GitHub UI.

Similar Posts