GitHub Actions
A complete guide to GitHub Actions: workflows, events, jobs, runners, composite actions, matrix builds, secrets, and OIDC cloud auth.
For many years, setting up a CI/CD pipeline meant managing a separate build server. Teams had to configure virtual machines, install Jenkins or TeamCity, update software packages, maintain security patches, and pay hosting costs even when no code was being built. GitHub Actions changed this by integrating automation directly into GitHub repositories. With GitHub Actions, your CI/CD workflows live right alongside your code as YAML files inside the .github/workflows/ directory. Whether you need to run unit tests on every pull request, build and push Docker images, or deploy cloud infrastructure, GitHub Actions provides an easy, managed way to automate these tasks.
Definition
GitHub Actions is a workflow automation and CI/CD platform that lets developers automate, customize, and execute software development lifecycle tasks directly inside a GitHub repository.
Why It Matters
GitHub Actions removes the need to maintain dedicated build servers. Because it lives directly within GitHub, it responds naturally to pull requests, issue labels, releases, and branch events. It also features a large marketplace of reusable actions created by the community and cloud providers (for AWS, Docker, Kubernetes, Slack, and Terraform), making it straightforward to build multi-step pipelines without writing hundreds of lines of custom bash scripts.
How It Works
GitHub Actions follows a clear structure of concepts: 1. Workflows: Automated processes defined in YAML files located in .github/workflows/. A repository can have multiple workflows (such as ci.yml for tests and release.yml for deployments). 2. Events / Triggers (on:): The specific activity that causes the workflow to run. Common examples include push, pull_request, release, scheduled cron jobs, or manual triggers (workflow_dispatch). 3. Jobs: A workflow contains one or more jobs. By default, jobs run in parallel to save time. You can use the needs: keyword to make jobs run sequentially (such as having a deploy job wait for a test job to succeed). 4. Runners: The virtual machine or container that executes the job. GitHub provides hosted runners (Ubuntu, Windows, macOS) that are fresh and isolated for each run. Teams can also connect self-hosted runners on their own infrastructure when custom hardware or private network access is required. 5. Steps & Actions: Each job consists of steps that run in order. A step can run a shell command (run: npm test) or use a pre-built action (uses: actions/checkout@v4). 6. Secret Management with OIDC (OpenID Connect): Instead of storing permanent cloud access keys in GitHub Secrets, GitHub Actions can use OpenID Connect (OIDC) to exchange short-lived, secure tokens with cloud providers like AWS or Google Cloud, improving security.
GitHub Actions Workflow Execution Structure
Key Points
- Workflow files are stored directly in your Git repository under `.github/workflows/*.yml`.
- Jobs run in parallel by default; use `needs:` to set up sequential dependencies.
- Each job runs on an independent, freshly prepared virtual runner.
- Pinning third-party actions to full commit hashes helps protect against supply-chain risks.
- Set minimal permissions using the `permissions:` block to follow the principle of least privilege.
- Use OIDC authentication to connect to cloud providers without saving permanent access keys.
Steps
Create the Workflows Directory
In your repository root, create the folder path: `.github/workflows/`.
Create a YAML Workflow File
Create a file like `.github/workflows/ci.yml` and define the workflow name, trigger events (`on:`), and jobs.
Check Out Code and Set Up Runtimes
Use standard official actions like `actions/checkout@v4` and `actions/setup-node@v4` to initialize the project environment.
Store Secrets in Repository Settings
Keep sensitive API tokens in GitHub Repository Secrets and reference them in YAML using `${{ secrets.MY_SECRET }}`.
Check Results in the Actions Tab
Push your commit to GitHub and view streaming logs, test summaries, and build artifacts under the Actions tab.
Examples
The Recipe and Kitchen Analogy
A GitHub Actions workflow is like a recipe card. The trigger (`on: push`) is the dinner order coming in. The runner is a clean, rented commercial kitchen. The jobs are the courses being prepared. And pre-built actions (`uses:`) are appliances like food processors that save you from writing custom scripts for routine steps.
Real-World Examples
Open-Source Cross-Platform Testing
Many popular open-source projects use GitHub Actions matrix builds to test their code across Ubuntu, Windows, and macOS with multiple programming language versions simultaneously on every pull request, ensuring compatibility across operating systems.
Technical Examples
name: Cloud Deployment via OIDC
on:
push:
branches: [main]
# Follow least-privilege permissions
permissions:
id-token: write # Required for requesting temporary OIDC tokens
contents: read # Required for checking out repository code
jobs:
deploy:
name: Authenticate & Deploy to AWS
runs-on: ubuntu-latest
environment: production
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Configure AWS credentials via OIDC
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsProductionRole
aws-region: us-east-1
# Notice: Zero static AWS_ACCESS_KEY_ID or SECRET_ACCESS_KEY stored in GitHub!
- name: Verify AWS identity
run: aws sts get-caller-identity
- name: Update Kubernetes deployment
run: |
aws eks update-kubeconfig --name prod-cluster --region us-east-1
kubectl set image deployment/payment-api \
payment-api=${{ secrets.AWS_ECR_REGISTRY }}/payment-api:${{ github.sha }} \
-n payments
kubectl rollout status deployment/payment-api -n payments --timeout=120sGitHub Actions vs Self-Managed Jenkins
| Aspect | GitHub Actions | Self-Managed Jenkins |
|---|---|---|
| Infrastructure | Managed, serverless cloud runners | Requires dedicated virtual machines, updates, and maintenance |
| Configuration | YAML files stored in `.github/workflows/` with your code | Groovy Jenkinsfile or web-based UI configuration |
| Ecosystem | GitHub Marketplace with thousands of pre-built actions | Jenkins plugins that require ongoing security updates |
| Scalability | Scales to run jobs in parallel automatically | Limited by your server or VM agent capacity |
| Setup Time | Minutes (commit a YAML file to your repository) | Hours or days to provision servers and configure plugins |
Security Tips for Workflows
Advantages
- No Server Overhead: No build servers to patch, maintain, or pay for when idle.
- Deep GitHub Integration: Reacts directly to pull requests, reviews, releases, and issue comments.
- Rich Action Ecosystem: Community actions make it simple to connect with cloud providers and developer tools.
- Built-in Free Tier: Includes free monthly build minutes for public and private repositories.
Limitations
- Platform Specific: Workflows written in GitHub Actions syntax do not run directly on GitLab CI or Jenkins without modification.
- Concurrency Limits: Free tiers have limits on how many jobs can run at the same time.
Interview Questions
Explain the main components of GitHub Actions (Workflows, Events, Jobs, Steps, Runners, Actions).
A Workflow is an automated process defined in a `.github/workflows/*.yml` file. An Event is a trigger (like a push or pull request) that starts the workflow. A Workflow contains one or more Jobs, which run in parallel by default on fresh Runners (virtual machines or containers). Each Job contains an ordered sequence of Steps, which either execute shell commands (`run:`) or use modular components known as Actions (`uses:`).
How does OpenID Connect (OIDC) improve security in CI/CD workflows?
Traditionally, deploying to a cloud provider required saving long-lived access keys in repository secrets, which created risk if credentials leaked. OIDC eliminates static credentials: the GitHub runner requests a short-lived, signed JWT token and exchanges it with the cloud provider for temporary credentials valid for only a short window (typically one hour).
Practice Questions
- Create a `.github/workflows/ci.yml` file in a repository that runs your tests whenever a pull request is opened.
- Try using dependency caching in a workflow with `actions/setup-node` or your language's setup action and observe the difference in run time.
