Skip to main content

GitHub Actions

GitHub Actions is GitHub's built-in CI/CD platform that runs YAML workflows from .github/workflows/ on repository events using hosted or self-hosted runners.

Understanding GitHub Actions

What Is GitHub Actions and How Does a Workflow Run

GitHub Actions runs automation defined in YAML files inside your repository. A workflow listens for events such as a push, a pull request, or a schedule, then runs one or more jobs on runners. Results appear on the pull request as status checks, so build, test, and deploy feedback lives next to the code.

Core Building Blocks

Concept Meaning
Workflow One YAML file in .github/workflows/
Event What starts it (push, pull_request, schedule)
Job A set of steps on one runner
Step A shell command or a reusable action
Runner The machine that executes a job
flowchart LR A[Push or PR event] --> B[Workflow starts] B --> C[Job: build] C --> D[Job: test] C --> E[Job: lint] D --> F[Job: deploy] classDef ev fill:#F97316,color:#FFFFFF classDef job fill:#326CE5,color:#FFFFFF classDef done fill:#10B981,color:#FFFFFF class A,B ev class C,D,E job class F done

Jobs run in parallel by default. Use needs to order them.

Workflow Example

YAML
name: Build and Deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
permissions:
contents: read
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
env:
ECR_REGISTRY: 123456789.dkr.ecr.ap-south-1.amazonaws.com
IMAGE_NAME: payment-api
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v5
with:
push: false
tags: ${{ env.ECR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: npm
- run: npm ci
- run: npm test -- --coverage
deploy:
needs: [build, test]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production # waits for approval
steps:
- run: echo "deploy ${{ github.sha }}"

Authenticate to AWS Without Stored Keys

YAML
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789:role/github-actions-deploy
aws-region: ap-south-1

Troubleshooting

Symptom Check What to Look For
Workflow not triggering Actions tab and on: filters Branch or path filter mismatch
OIDC auth fails IAM trust policy Repo or branch in the sub condition
Cache never hits Cache key Lockfile hash changes each run
Job queued forever Runner labels No runner with matching labels
Tip

Cache dependencies with a lockfile-based key, for example ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}. It often saves several minutes per run.

Remember

Set permissions explicitly at workflow or job level and grant only what is needed. The default token can be broader than you want.

Security

Never combine pull_request_target with checking out the fork's head commit. That runs untrusted code with access to your secrets. Use pull_request for untrusted contributions.

Common Mistake

Referencing third-party actions by a mutable tag such as @v2. Pin to a full commit SHA, and store cloud access through OIDC instead of long-lived keys.

Frequently Asked Questions

How is GitHub Actions different from Jenkins?

Actions is built into GitHub, so workflows trigger on native repository events, show status inline on pull requests, and need no separate server. Jenkins is self-hosted and extremely flexible through plugins, but you operate and patch it yourself.

Why should I pin actions to a commit SHA?

Tags like v2 can be moved by the action owner, or by an attacker who compromises their repository. A full commit SHA is immutable, so the code that runs in your pipeline cannot change silently.

How do I stop two deploys running at once?

Use the concurrency key with a group name such as the branch or environment. New runs queue or cancel the older run depending on cancel-in-progress.

Found a mistake, an outdated step or a broken link?