AB
The first part of our comprehensive GitHub Actions guide covers key concepts, benefits, and how to set up your first workflow for automated CI/CD.
In today’s fast-paced development environments, automation is no longer a luxury—it’s a necessity. GitHub Actions has emerged as a powerful solution that allows developers to automate workflows directly within their GitHub repositories. This three-part guide will take you from zero to hero with GitHub Actions, covering everything from basic concepts to advanced implementations.
GitHub Actions is an automation platform built directly into GitHub. It allows developers to automate their workflows, perform CI/CD tasks, and streamline repetitive processes, like testing, building, or deploying code.
When you commit code to your repository, you can automatically trigger a series of commands to build, test, and deploy your application. It’s like having a dedicated assistant who watches your repository and performs tasks whenever something happens.
To understand GitHub Actions, it’s essential to grasp its core concepts:
.yml
file stored in the .github/workflows/
directory. It consists of one or more jobs.Here’s an example workflow that runs a “Hello World” script whenever you push code to the repository:
name: Hello World Workflow # Name of the workflow
on: # Events that trigger the workflow
push:
branches:
- main # Trigger on pushes to the main branch
jobs: # Define jobs
greet: # Name of the job
runs-on: ubuntu-latest # Virtual environment for the job
steps: # Steps for this job
- name: Checkout code
uses: actions/checkout@v3 # Action to check out repository code
- name: Print Hello World
run: echo "Hello, World!" # Command to execute
name
: Defines the name of the workflow. This is for display purposes in the GitHub Actions tab.on
: Specifies the event that triggers the workflow. In this case, it’s triggered when code is pushed to the main
branch.jobs
: Contains all the jobs that are part of the workflow.runs-on
: Defines the environment where the job will run. Here, it uses ubuntu-latest
.steps
: Each job is broken into steps.actions/checkout@v3
) to check out the repository code.echo "Hello, World!"
) and prints “Hello, World!” to the workflow logs.When you push code to the main
branch:
GitHub Actions workflows are defined using YAML. YAML is a human-readable data serialization format.
You can trigger workflows on multiple events. Here’s an example:
name: Multi-event Workflow
on:
push: # Trigger on push
branches:
- main
pull_request: # Trigger on pull request creation
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Run tests
run: echo "Running tests!"
Explanation:
main
branch.test
, a simple command is executed to simulate running tests.Imagine you’re developing a JavaScript application and want to ensure your code is always linted (checked for errors) before merging into the main
branch. Here’s a workflow:
name: Lint JavaScript Code
on: pull_request
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "18"
- name: Install dependencies
run: npm install
- name: Run linter
run: npm run lint
What this workflow does:
npm run lint
) to check the code for issues.Outcome: If the linter finds any issues, the workflow will fail, ensuring that only clean, linted code is merged.
Before diving into workflows, let’s set up the basics.
Once you have a repository:
To create your first workflow:
main.yml
file under .github/workflows/
. Edit it to define your workflow.Here’s an example of a basic workflow:
name: Basic CI Workflow
on: push # Trigger the workflow on code pushes
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run a simple script
run: echo "Hello, GitHub Actions!"
Explanation:
name
: The name of the workflow. This helps identify it in the GitHub Actions dashboard.on
: Specifies when the workflow will run. Here, it triggers on every push
event.jobs
: Defines tasks (jobs) to be run.runs-on
: Specifies the environment for the job (e.g., ubuntu-latest
).steps
: Lists individual tasks in the job.echo "Hello, GitHub Actions!"
, which prints the text in the logs..github/workflows
This directory is where all your workflow files are stored. Let’s break it down:
.github/workflows
?.github/workflows
directory doesn’t exist, create it manually..github/workflows/<workflow-name>.yml
..yml
or .yaml
extension.build-and-test.yml
or deploy.yml
to indicate the workflow’s purpose.your-repository/
├── .github/
│ ├── workflows/
│ │ ├── build.yml
│ │ ├── deploy.yml
├── README.md
├── src/
Here’s a sample directory structure with a build.yml
file:
name: Build Workflow
on: pull_request # Trigger on pull request creation
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository code
uses: actions/checkout@v3
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
What it does:
npm install
.npm test
..github/workflows
?on
or jobs
, GitHub will flag the YAML as invalid.Imagine you’re building a Python project and want to ensure your code is tested automatically. Here’s a setup:
.github/workflows/python-tests.yml
.name: Python Tests
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.8"
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest
Explanation:
requirements.txt
.pytest
.Outcome: Whenever code is pushed, the workflow tests the Python application, ensuring it’s error-free.
GitHub Actions workflows are defined in YAML files and dictate what tasks run, when they run, and how. Let’s explore the components in detail.
A workflow file has a specific structure. Here’s an example:
name: CI Workflow # Name of the workflow
on: # Events that trigger the workflow
push:
branches:
- main # Trigger on pushes to the 'main' branch
jobs: # Defines jobs to execute
build: # A job named 'build'
runs-on: ubuntu-latest # Environment to run the job
steps: # Steps within the job
- name: Checkout code # Step 1: Check out code from the repo
uses: actions/checkout@v3
- name: Print message # Step 2: A custom command
run: echo "Hello, GitHub Actions!"
Let’s break it down:
name
name: Build and Test Workflow
on
What it does: Specifies events that trigger the workflow.
Common Events:
push
: Runs the workflow whenever new code is pushed to the repository.pull_request
: Runs when a pull request is opened, updated, or merged.schedule
: Runs at specific intervals (like a cron job).workflow_dispatch
: Runs manually via the GitHub UI.Example:
on:
push:
branches:
- main
Outcome: This workflow triggers only when you push changes to the main
branch.
jobs
What it does: Defines tasks to run. Each job runs in its own environment and can be executed in parallel or sequentially.
Example:
jobs:
build:
runs-on: ubuntu-latest
Outcome: Creates a job named build
that runs on an Ubuntu environment.
steps
What it does: Specifies individual tasks within a job.
Each step can:
Example:
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install dependencies
run: npm install
Outcome: The first step checks out your code. The second installs dependencies using npm install
.
Triggers specify when a workflow should run. Let’s explore the most common types:
What it does: Runs the workflow whenever new code is pushed to the repository.
Example:
on:
push:
branches:
- main
- feature/*
Outcome: The workflow triggers on any push to the main
branch or branches starting with feature/
.
What it does: Runs when a pull request is opened, updated, or merged.
Example:
on:
pull_request:
branches:
- main
Outcome: Ensures that code changes are tested before merging into main
.
workflow_dispatch
)What it does: Allows you to trigger workflows manually via the GitHub UI.
Example:
on:
workflow_dispatch:
inputs:
environment:
description: "Environment to deploy to"
required: true
default: "staging"
Outcome: Adds a button in the Actions tab to manually start the workflow. The user can specify inputs like the environment.
What it does: Runs workflows at specific intervals using cron syntax.
Example:
on:
schedule:
- cron: "0 0 * * *"
Outcome: Runs the workflow daily at midnight UTC.
In this first part of our GitHub Actions guide, we’ve covered:
GitHub Actions is a powerful tool that can significantly streamline your development process. By automating repetitive tasks, you can focus on what really matters—writing great code.
In Part 2, we’ll dive deeper into building practical workflows, customizing them with environment variables and secrets, and implementing matrix builds for testing across multiple environments.
Continue to Part 2: Building Advanced Workflows